Save debug output to local file

34 views
Skip to first unread message

Ramesh AR

unread,
Jan 21, 2021, 12:47:37 PM1/21/21
to Ansible Project
Folks,

I'm trying to copy the debug output to a local file for next task. 

My host inventory has 2 hosts and I see the local file has only one host output. 
is something missing in playbook ? if I have 10 hosts how to get copied all the output to a file.

please help on this,  

   register: result
  - set_fact:
      user: "{{ result.configResolveClass.children|json_query(my_query) }}"
    vars:
      my_query: "[].outConfigs.children[].aaaUser.attributes[].{id: id, name: name, priv: priv, accountStatus: accountStatus}"
    
  - local_action: copy content="CIMC IP is {{ inventory_hostname.split() | list | to_yaml }} {{ user | rejectattr('accountStatus', 'eq', 'inactive')| list | to_yaml }}" dest=/root/user.txt


PLAY [cimc] ********************************************************************************************************************************

TASK [Checking service accounts] *****************************************************************************************************************
ok: [host1]
ok: [host2]

TASK [set_fact] **********************************************************************************************************************************
ok: [host1]
ok: [host2]

TASK [copy] **************************************************************************************************************************************
changed: [host1 -> localhost]
changed: [host2 -> localhost]

PLAY RECAP ***************************************************************************************************************************************
host1            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host2             : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


cat user.txt
CIMC IP is [host1]
 - {accountStatus: active, id: '1', name: admin, priv: admin}
- {accountStatus: active, id: '2', name:account1 , priv: admin}
- {accountStatus: active, id: '3', name:account2, priv: admin}
- {accountStatus: active, id: '4', name:account3 , priv: admin}
- {accountStatus: active, id: '5', name:account4 , priv: admin}
- {accountStatus: active, id: '6', name:account5 , priv: admin}
- {accountStatus: active, id: '7', name:account6 , priv: admin}

Dick Visser

unread,
Jan 21, 2021, 1:01:08 PM1/21/21
to ansible...@googlegroups.com
This is because the file gets overwritten for each host, and thus you
always end up with the last host.
You need to accumulate the results during the playbook and at the end
write to file.
> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f404c81-22b5-41b6-b59f-7a7b6e1f2147n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Ramesh AR

unread,
Jan 21, 2021, 1:49:32 PM1/21/21
to Ansible Project
Hi Dick Visser,

Can you explain little more on this ( accumulate the results during the playbook ) , sorry I may not understand it . but from your notes , yes the copy task to file mentioned at the end of the playbook.

---
- hosts: cimc
  connection: local
  gather_facts: no
  tasks:
  - name: Checking service accounts
    imc_rest:
      hostname: "{{ imc_hostname }}"
      username: "{{ imc_username }}"
      password: "{{ imc_password }}"
      protocol: https
      timeout: 60
      validate_certs: no
      content: |
         <configResolveClass inHierarchical="false" classId="aaaUser"/>
    register: result
  - set_fact:
      user: "{{ result.configResolveClass.children|json_query(my_query) }}"
    vars:
      my_query: "[].outConfigs.children[].aaaUser.attributes[].{id: id, name: name, priv: priv, accountStatus: accountStatus}"
  - debug:
      msg: "{{ user | rejectattr('accountStatus', 'eq', 'inactive')| list | to_yaml }}"
  - local_action:  copy content="CIMC IP is {{ inventory_hostname.split() | list | to_yaml }} {{ user | rejectattr('accountStatus', 'eq', 'inactive')| list | to_yaml }}" dest=/root/user.txt 


Thanks,
Ramesh
Reply all
Reply to author
Forward
0 new messages