Nested loop. Write var to file before moving on to next {{item}}

26 views
Skip to first unread message

John Harmon

unread,
Sep 29, 2017, 5:56:55 PM9/29/17
to Ansible Project
The following playbook is close to what I need, but I need to write the content of sudoers.stdout_lines to a file (append) before moving on to the next item.  Is it possible to nest these two pieces? or rewrite it to do what I need?

- name: Gather sudo users
  shell
: "/bin/cat /etc/{{item}} | grep -ve ^$ -e ^# -e ^Defaults"
 
register: sudoers
  with_items
:
   
- sudoers
   
- sudoers.d/*

- name: Write sudo users
  lineinfile:
    dest: "/tmp/{{ansible_nodename}}.sudoers"
    line: "{{sudoers.stdout_lines}}"
    insertafter: EOF
    create: yes
    state: present
  delegate_to: localhost


Message has been deleted

John Harmon

unread,
Sep 29, 2017, 6:12:44 PM9/29/17
to Ansible Project
Interesting.....  debug shows that the variable is being appended to.  I can't seem to reference anything under the var (sudoers.stdout_lines for instance).  Is it possible to grab all instances of stdout_lines with this var's output? I've been trying without luck . . .

ok: [ansibletest-oel6] => {
   
"sudoers": {
       
"changed": true,
       
"msg": "All items completed",
       
"results": [
           
{
               
"_ansible_item_result": true,
               
"_ansible_no_log": false,
               
"_ansible_parsed": true,
               
"changed": true,
               
"cmd": "/bin/cat /etc/sudoers | grep -ve ^$ -e ^# -e ^Defaults",
               
"delta": "0:00:00.004715",
               
"end": "2017-09-29 16:08:26.616661",
               
"invocation": {
                   
"module_args": {
                       
"_raw_params": "/bin/cat /etc/sudoers | grep -ve ^$ -e ^# -e ^Defaults",
                       
"_uses_shell": true,
                       
"chdir": null,
                       
"creates": null,
                       
"executable": null,
                       
"removes": null,
                       
"warn": true
                   
}
               
},
               
"item": "sudoers",
               
"rc": 0,
               
"start": "2017-09-29 16:08:26.611946",
               
"stderr": "",
               
"stderr_lines": [],
               
"stdout": "root\tALL=(ALL) \tALL",
               
"stdout_lines": [
                   
"root\tALL=(ALL) \tALL"
               
]
           
},
           
{
               
"_ansible_item_result": true,
               
"_ansible_no_log": false,
               
"_ansible_parsed": true,
               
"changed": true,
               
"cmd": "/bin/cat /etc/sudoers.d/* | grep -ve ^$ -e ^# -e ^Defaults",
               
"delta": "0:00:00.005332",
               
"end": "2017-09-29 16:08:26.782472",
               
"invocation": {
                   
"module_args": {
                       
"_raw_params": "/bin/cat /etc/sudoers.d/* | grep -ve ^$ -e ^# -e ^Defaults",
                       
"_uses_shell": true,
                       
"chdir": null,
                       
"creates": null,
                       
"executable": null,
                       
"removes": null,
                       
"warn": true
                   
}
               
},
               
"item": "sudoers.d/*",
               
"rc": 0,
               
"start": "2017-09-29 16:08:26.777140",
               
"stderr": "",
               
"stderr_lines": [],
               
"stdout": "gomer ALL=(ALL) NOPASSWD: ALL\npyle ALL=(ALL) NOPASSWD: ALL",
               
"stdout_lines": [
                   
"gomer ALL=(ALL) NOPASSWD: ALL",
                   
"pyle ALL=(ALL) NOPASSWD: ALL"
               
]
           
}
       
]
   
}
}



John Harmon

unread,
Sep 29, 2017, 6:33:32 PM9/29/17
to Ansible Project
I found this: https://groups.google.com/forum/#!msg/ansible-project/QUugEQRZT-g/bYOjM_k3BQAJ

If I change my line to this:
line: "{{sudoers.results|map(attribute='stdout_lines')|list}}"

Then I get the following results in my file:
[['root\tALL=(ALL) \tALL'], ['gomer ALL=(ALL) NOPASSWD: ALL', 'pyle ALL=(ALL) NOPASSWD: ALL']]

Is there a way to further refine the output to look like the following?
root ALL=(ALL) ALL
gomer ALL
=(ALL) NOPASSWD: ALL
pyle ALL
=(ALL) NOPASSWD: ALL


Zimidar Boy

unread,
Sep 29, 2017, 10:52:12 PM9/29/17
to Ansible Project
I don't know much but maybe this can help. http://docs.ansible.com/ansible/latest/playbooks_filters.html

John Harmon

unread,
Oct 2, 2017, 11:33:06 AM10/2/17
to Ansible Project
That did come in helpful, thank you.  It helped me to format the output nicely.
Reply all
Reply to author
Forward
0 new messages