lineinfile is skipped if one of the task ignored

48 views
Skip to first unread message

Hanumantha Reddy Basireddy

unread,
Jul 19, 2021, 3:47:35 AM7/19/21
to Ansible Project
Hi All,
I have a playbook as follows:

---

- 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}}"

TEST1: my inventory looks as follows:

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: inventory as follows:

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..........

Question1: In TEST1 since inventory doesn't contain test_servers1 group, so task is ignored. but why lineinfile is skipped eventhought file variable contain results?

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

Question3: In lineinfile module, Is it possible to check files2 contains results and if yes to check files2.results is not empty then only loop through it otherwise skip it?

someone please clarify my doubts. Thanks in Advance.

Thanks,
BHR.

Hanumantha Reddy Basireddy

unread,
Jul 20, 2021, 1:16:46 AM7/20/21
to Ansible Project
Hello All,

Any leads....

Reply all
Reply to author
Forward
0 new messages