#StackBounty: #ansible lineinfile is skipped if one of the task failed

Bounty: 50

This is my playbook:

- hosts: all
  gather_facts: no
  ignore_unreachable: true

  - command: "{{ item }}"
      - "ls" 
      - "df -h"
    register: files
    ignore_errors: true
  - name: delegate
    command: ls
    register: files2
    delegate_to: "{{item}}"
    ignore_errors: true
      - "{{groups['test_servers1']}}"
  - lineinfile:
      line: "{{ item.stdout }}"
      path: /tmp/list.log
      create: yes
      - item.changed is defined
      - item.changed
    loop: "{{ files.results + 

Using this inventory:


TASK [command] **************************************************************************************************
changed: [] => (item=ls)
changed: [] => (item=df -h)

TASK [delegate] **************************************************************************************************
fatal: []: FAILED! => {"msg": "'dict object' has no attribute 'test_servers1'"}
TASK [lineinfile] **************************************************************************************************
skipping: []

Using this inventory:


TASK [command] *************************************************************************************************
changed: [] => (item=ls)
changed: [] => (item=df -h)

TASK [delegate] *************************************************************************************************

TASK [debug] *************************************************************************************************
ok: [] => {
    "files2": {
        "changed": false,
        "results": [],
        "skipped": true,
        "skipped_reason": "No items in the list"

TASK [lineinfile] ************************************************************************************************************************************************************************************************
changed: [] => (item={'start': '2021-07-18 02:11:13.080046', 'stderr': '', 'rc': 0, 'invocation': {'module_args': {'_raw_par..........

Question 1:
Since the inventory in the first test doesn’t contain test_servers1 group, the task is ignored. But why is lineinfile skipped even though file contains results?

Question 2:
In TEST2, my delegate task contains only one command i.e., ls why has file2 results? As per my knowledge when looping through multiple commands only register variable contains results.

Question 3:
Is it possible to check if files2 contains results in the lineinfile module, and if yes to check if files2.results is not empty then only loop through it, but skip it otherwise?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.