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.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/728d1870-987f-4c11-8c18-b703c1a1ec16n%40googlegroups.com.

Reply via email to