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

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

`

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”
]
}
]
}
}

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

I don’t know much but maybe this can help. http://docs.ansible.com/ansible/latest/playbooks_filters.html

That did come in helpful, thank you. It helped me to format the output nicely.