#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

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

TEST 1:
Using this inventory:

192.168.153.31
#[test_servers1]

Output:

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

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

TEST2:
Using this inventory:

192.168.153.31
[test_servers1]

Output:

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

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

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

TASK [lineinfile] ************************************************************************************************************************************************************************************************
changed: [192.168.153.31] => (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.