Ansible retrieve stdout with items loop

Hi All

I want to retrieve multiple output from a command with a with items loop
the playbook is like this

  • name: test
    command: openssl rand -base64 20
    register: pwd
    delegate_to: localhost
    with_items:
  • test1
  • test

the result is like this with -vvv command

changed: [127.0.0.1 → localhost] => (item=test) => {
“changed”: true,
“cmd”: [
“openssl”,
“rand”,
“-base64”,
“20”
],
“delta”: “0:00:00.008819”,
“end”: “2019-04-16 08:47:32.782792”,
“invocation”: {
“module_args”: {
“_raw_params”: “openssl rand -base64 20”,
“_uses_shell”: false,
“argv”: null,
“chdir”: null,
“creates”: null,
“executable”: null,
“removes”: null,
“stdin”: null,
“warn”: true
}
},
“item”: “test”,
“rc”: 0,
“start”: “2019-04-16 08:47:32.773973”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “IWRXhTs1Q9dKxIKgaXXkYoYwn0s=”,
“stdout_lines”: [
“IWRXhTs1Q9dKxIKgaXXkYoYwn0s=”
]
}

I just want the stdout lines per items (test and test1 ) and register those last as variables for other command like this one

  • name: Test2
    command: “echo {{ item.stdout }} > /tmp/test”
    with_items: “{{ pwd.results }}”

Can t get rid of it this register the whole output with at the end my desired variable, how to do this ?

ok: [127.0.0.1] => (item={‘_ansible_parsed’: True, ‘stderr_lines’: , u’changed’: True, u’stdout’: u’Fkm9ulL1kMf9liKquTdECo6UynI=‘, ‘_ansible_delegated_vars’: {‘ansible_delegated_host’: u’localhost’, ‘ansible_host’: u’localhost’}, ‘_ansible_item_result’: True, u’delta’: u’0:00:00.009828’, ‘stdout_lines’: [u’Fkm9ulL1kMf9liKquTdECo6UynI=‘], ‘_ansible_item_label’: u’test1’, u’end’: u’2019-04-16 08:52:09.797091’, ‘_ansible_no_log’: False, ‘failed’: False, u’cmd’: [u’openssl’, u’rand’, u’-base64’, u’20’], ‘item’: u’test1’, u’stderr’: u’‘, u’rc’: 0, u’invocation’: {u’module_args’: {u’warn’: True, u’executable’: None, u’_uses_shell’: False, u’_raw_params’: u’openssl rand -base64 20’, u’removes’: None, u’argv’: None, u’creates’: None, u’chdir’: None, u’stdin’: None}}, u’start’: u’2019-04-16 08:52:09.787263’, ‘_ansible_ignore_errors’: None}) => {
“item”: {
“changed”: true,
“cmd”: [
“openssl”,
“rand”,
“-base64”,
“20”
],
“delta”: “0:00:00.009828”,
“end”: “2019-04-16 08:52:09.797091”,
“failed”: false,
“invocation”: {
“module_args”: {
“_raw_params”: “openssl rand -base64 20”,
“_uses_shell”: false,
“argv”: null,
“chdir”: null,
“creates”: null,
“executable”: null,
“removes”: null,
“stdin”: null,
“warn”: true
}
},
“item”: “test1”,
“rc”: 0,
“start”: “2019-04-16 08:52:09.787263”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “Fkm9ulL1kMf9liKquTdECo6UynI=”,
“stdout_lines”: [
“Fkm9ulL1kMf9liKquTdECo6UynI=”
]
},
“item.stdout”: “Fkm9ulL1kMf9liKquTdECo6UynI=”
}
ok: [127.0.0.1] => (item={‘_ansible_parsed’: True, ‘stderr_lines’: , u’changed’: True, u’stdout’: u’rQkmbSbF67S0RFKyOGq5NzzDy4o=‘, ‘_ansible_delegated_vars’: {‘ansible_delegated_host’: u’localhost’, ‘ansible_host’: u’localhost’}, ‘_ansible_item_result’: True, u’delta’: u’0:00:00.009044’, ‘stdout_lines’: [u’rQkmbSbF67S0RFKyOGq5NzzDy4o=‘], ‘_ansible_item_label’: u’test’, u’end’: u’2019-04-16 08:52:10.020054’, ‘_ansible_no_log’: False, ‘failed’: False, u’cmd’: [u’openssl’, u’rand’, u’-base64’, u’20’], ‘item’: u’test’, u’stderr’: u’‘, u’rc’: 0, u’invocation’: {u’module_args’: {u’warn’: True, u’executable’: None, u’_uses_shell’: False, u’_raw_params’: u’openssl rand -base64 20’, u’removes’: None, u’argv’: None, u’creates’: None, u’chdir’: None, u’stdin’: None}}, u’start’: u’2019-04-16 08:52:10.011010’, ‘_ansible_ignore_errors’: None}) => {
“item”: {
“changed”: true,
“cmd”: [
“openssl”,
“rand”,
“-base64”,
“20”
],
“delta”: “0:00:00.009044”,
“end”: “2019-04-16 08:52:10.020054”,
“failed”: false,
“invocation”: {
“module_args”: {
“_raw_params”: “openssl rand -base64 20”,
“_uses_shell”: false,
“argv”: null,
“chdir”: null,
“creates”: null,
“executable”: null,
“removes”: null,
“stdin”: null,
“warn”: true
}
},
“item”: “test”,
“rc”: 0,
“start”: “2019-04-16 08:52:10.011010”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “rQkmbSbF67S0RFKyOGq5NzzDy4o=”,
“stdout_lines”: [
“rQkmbSbF67S0RFKyOGq5NzzDy4o=”
]
},
“item.stdout”: “rQkmbSbF67S0RFKyOGq5NzzDy4o=”
}

Looking at the two tasks and what you seem to be trying to achieve, you could probably just use a password lookup to generate it:

https://docs.ansible.com/ansible/latest/plugins/lookup/password.html

Dick

You re right thanks

Jérome Denéchaud



From: dick.visser@geant.org
Sent: April 16, 2019 09:36
To: ansible-project@googlegroups.com
Reply-to: ansible-project@googlegroups.com
Subject: Re: [ansible-project] Ansible retrieve stdout with items loop

|

  • |

Looking at the two tasks and what you seem to be trying to achieve, you could probably just use a password lookup to generate it:

https://docs.ansible.com/ansible/latest/plugins/lookup/password.html

Dick