save stdout to file on localhost.

18 views
Skip to first unread message

Mario Garcia

unread,
Mar 27, 2020, 12:10:32 PM3/27/20
to Ansible Project
i want to query all the users on the hosts of my inventory and create afile for each hosts with all the privileges for each user 

I know that htere tools like freeipa but we do not have those in place 

so i am using the getent wrapper to get all the users on the hosts included the netgroups.. run sudo -lU on each of these hosts and save the output to file by using stdout or stdout_lines. 

the problems is only one username is saved to the file and not all of them 

this is the playbook: 

---
- name: get users from  hosts
  hosts: all
  gather_facts: no

  tasks:

  - name: get users
    getent:
      database: passwd


  - name: get sudo rights
    shell:
      cmd: sudo -lU "{{ item }}"
    loop: "{{ getent_passwd.keys()|flatten(levels=1) }}"
    register: sudo_rights


  - name: debug sudo_rights
    debug:
      msg: "{{item.stdout}}"
    loop: "{{ sudo_rights.results }}"

  - name: save to file
    copy:
      content: "{{item.stdout}}"
      dest: privs_{{inventory_hostnam}}
    loop: "{{ sudo_rights.results }}"
    delegate_to: localhost
    run_once: true


I suspect taht only the last user queried is on the file.. based on the debug output but how have them all. 
can copy concatenane should i use a jinja template and use the template module instead? 

thank you 


Kai Stian Olstad

unread,
Mar 27, 2020, 2:00:41 PM3/27/20
to 'Mario Garcia' via Ansible Project
This should work:

- name: save to file
copy:
content: "{{ sudo_rights.results | map(attribute='stdout') | join('\n') }}"
dest: privs_{{inventory_hostnam}}
delegate_to: localhost

You can't use run once, since you need the task to run one time for each host.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages