How to run mutiple include tasks asynchronously in ansible? - ansible

I use 'include' and 'with_items' to looping over a block of tasks:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1:
- { name: 'testuser1', groups: 'wheel', time: 10 }
- { name: 'testuser2', groups: 'root', time: 3 }
tasks:
- name: multiple tasks
include: multiple.yml item={{item}}
with_items: "{{ list1 }}"
and the multiple.yml is:
---
- name: time before
shell: date +"%H:%M:%S"
- name: run
shell: sleep {{ item.time }}
- name: time after
shell: date +"%H:%M:%S"
The task works well. However, the multiple.yml for each loop are executed serialisely:
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:19"]}
I want to execute the multiple.yml of each loop asynchronously, so I tried to use async and poll in main file:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1:
- { name: 'testuser1', groups: 'wheel', time: 10 }
- { name: 'testuser2', groups: 'root', time: 3 }
tasks:
- name: multiple tasks
include: multiple.yml item={{item}}
async: 20
poll: 0
with_items: "{{ list1 }}"
The expected result is:
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:08"]}
But the result of running new main.yml is the same as before, and the multiple.yml of the two loops is not executed asynchronously. I don't kown what the problem is. Is there any advice?

That is a known issue and is marked as an "improvement," rather than a bug. Feel free to drop a comment or +1 or whatever into the issue, but I would not expect it to be resolved in the near future since that issue has been open since 2017.
Separately, it may interest you to know that the "inline variable" syntax has been deprecated in favor of using vars:, but even in that case you don't need to because with_items: automatically exposes item and thus it is implicitly in vars: for you. The deprecation is in the "Note" in the manual

Related

Ansible - How to print out this list of dictionaries

I have this output from Windows auditpol.
I have difficulty to figure out how to print a certain output from the var query.
- name: check audit
win_command: auditpol /get /subcategory:{{ item }}
with_items:
- '"{{policy}}"'
register:
query
- debug: var=query.results
ok: [ipcprdixn201] => {
"query.results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "auditpol /get /subcategory:\"Other System Events\"",
"delta": "0:00:00.171888",
"end": "2022-11-24 06:04:11.903563",
"failed": false,
"item": "\"Other System Events\"",
"rc": 0,
"start": "2022-11-24 06:04:11.731675",
"stderr": "",
"stderr_lines": [],
"stdout": "System audit policy\r\r\nCategory/Subcategory Setting\r\nSystem\r\n Other System Events No Auditing\r\r\n",
"stdout_lines": [
"System audit policy",
"",
"Category/Subcategory Setting",
"System",
" Other System Events No Auditing",
""
]
}
]
}
- debug: msg="{{ query.results | type_debug }}"
ok: [ipcprdixn201] => {
"msg": "dict"
}
- debug: msg="{{ query | type_debug }}"
ok: [ipcprdixn201] => {
"msg": "list"
}
How do I print out one of the list, i.e. stdout_lines?
Thank you.
Reference: https://pythonexamples.org/python-list-of-dictionaries/
- debug: var=query.results[0]['stdout_lines']
ok: [ipcprdixn201] => {
"query.results[0]['stdout_lines']": [
"System audit policy",
"",
"Category/Subcategory Setting",
"System",
" Other System Events No Auditing",
""
]
}

How to run ansible playbook in active/passive cluster setup?

I have a need to run ansible on a bunch of servers which are active/passive clusters. So, I have an inventory file like
[QA:children]
QA_01_CLUSTER
[QA_01_CLUSTER]
ip-10-361-412-14
ip-10-361-412-30
So serv1qa01 and serv2qa01 could be active/passive in that cluster or the other way around.
I am trying to write a playbook where it will look at which server is secondary, execute the tasks before executing them on the primary.
I have an api running on these servers which returns whether the server is primary or secondary.
I tried something like this
---
- hosts: all
serial: 1
tasks:
- name: run curl command to determine primary/secondary
command: /tmp/status.sh
register: apis_op
- name: run curl command to determine primary/secondary
debug: var=apis_op.stdout_lines
- name: If node is primary add to primary
add_host:
name: "{{ ansible_hostname }}"
groups: temp_primary
when: apis_op.stdout == "primary"
- name: run curl command to determine primary/secondary
debug: var=groups
- name: If node is secondary add to secondary
add_host:
name: "{{ ansible_hostname }}"
groups: temp_secondary
when: apis_op.stdout == "secondary"
- name: run curl command to determine primary/secondary
debug: var=groups
- hosts: temp_secondary
tasks:
- name: Run schell script
command: /tmp/testthis.sh
become: yes
become_user: root
- hosts: temp_primary
tasks:
- name: Run schell script
command: /tmp/testthis.sh
become: yes
become_user: root
Here's the entire output with the -v option.
PLAY [all] **********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host ip-10-361-412-14 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [ip-10-361-412-14]
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"changed": true, "cmd": ["/tmp/status.sh"], "delta": "0:00:00.038672", "end": "2021-03-29 21:29:15.693979", "rc": 0, "start": "2021-03-29 21:29:15.655307", "stderr": "", "stderr_lines": [], "stdout": "\"secondary\"", "stdout_lines": ["\"secondary\""]}
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14] => {
"apis_op.stdout_lines": [
"\"secondary\""
]
}
TASK [If node is primary add to primary] ****************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"add_host": {"groups": ["temp_primary"], "host_name": "ip-10-361-412-14", "host_vars": {"when": "apis_op.stdout == \"primary\""}}, "changed": true}
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14] => {
"groups": {
"all": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"hack": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"temp_primary": [
"ip-10-361-412-14"
],
"ungrouped": []
}
}
TASK [If node is secondary add to secondary] ************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"add_host": {"groups": ["temp_secondary"], "host_name": "ip-10-361-412-14", "host_vars": {"when": "apis_op.stdout == \"secondary\""}}, "changed": true}
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14] => {
"groups": {
"all": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"hack": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"temp_primary": [
"ip-10-361-412-14"
],
"temp_secondary": [
"ip-10-361-412-14"
],
"ungrouped": []
}
}
PLAY [all] **********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host ip-10-361-412-30 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [ip-10-361-412-30]
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
changed: [ip-10-361-412-30] => {"changed": true, "cmd": ["/tmp/status.sh"], "delta": "0:00:00.038760", "end": "2021-03-29 21:29:17.776237", "rc": 0, "start": "2021-03-29 21:29:17.737477", "stderr": "", "stderr_lines": [], "stdout": "\"primary\"", "stdout_lines": ["\"primary\""]}
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-30] => {
"apis_op.stdout_lines": [
"\"primary\""
]
}
TASK [If node is primary add to primary] ****************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-30] => {"add_host": {"groups": ["temp_primary"], "host_name": "ip-10-361-412-30", "host_vars": {"when": "apis_op.stdout == \"primary\""}}, "changed": true}
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-30] => {
"groups": {
"all": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"hack": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"temp_primary": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"temp_secondary": [
"ip-10-361-412-14"
],
"ungrouped": []
}
}
TASK [If node is secondary add to secondary] ************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-30] => {"add_host": {"groups": ["temp_secondary"], "host_name": "ip-10-361-412-30", "host_vars": {"when": "apis_op.stdout == \"secondary\""}}, "changed": true}
TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-30] => {
"groups": {
"all": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"hack": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"temp_primary": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"temp_secondary": [
"ip-10-361-412-14",
"ip-10-361-412-30"
],
"ungrouped": []
}
}
PLAY [temp_secondary] ***********************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14]
ok: [ip-10-361-412-30]
TASK [Run schell script] ********************************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.040347", "end": "2021-03-29 21:29:20.079991", "rc": 0, "start": "2021-03-29 21:29:20.039644", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [ip-10-361-412-30] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.040470", "end": "2021-03-29 21:29:20.117223", "rc": 0, "start": "2021-03-29 21:29:20.076753", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
PLAY [temp_primary] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14]
ok: [ip-10-361-412-30]
TASK [Run schell script] ********************************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.041023", "end": "2021-03-29 21:29:22.107751", "rc": 0, "start": "2021-03-29 21:29:22.066728", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [ip-10-361-412-30] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.041487", "end": "2021-03-29 21:29:22.145928", "rc": 0, "start": "2021-03-29 21:29:22.104441", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
ip-10-361-412-14 : ok=11 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ip-10-361-412-30 : ok=11 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
I have simplified the playbook above but basically I want to run the tasks on secondary before running it on primary for HA reasons.
But this is not working. It's running the playbook in parallel and basically executing tasks on both the hosts at the same time.
How can I force it to run on secondary first before running on primary? I tried to get creative with the serial module or the inventory but is there a dynamic way of doing this?

Ansible task is run regardless of condition

If I store a string into an ansible register, and then have other tasks run only when that register contains a certain value, they are always run, regardless of the value in the register.
Why is ansible running tasks when a conditional should fail?
This is a simple example. It should only debug once (as test_foo.stdout == "Foo") but instead it will print both debug statements:
- name: foo
command: "echo \"Foo\""
register: test_foo
tags: [ 'foo-test' ]
- debug:
var: test_foo
when: test_foo.stdout == "Foo",
tags: [ 'foo-test' ]
- debug:
var: test_foo
when: test_foo.stdout == "Bar",
tags: [ 'foo-test' ]
Just to note, this also happens when another command call is triggered with the same conditional, not just debug calls. It will always execute both tasks.
Here's the output:
TASK [test : foo] ***********************************************************************************************************************************************************************************
changed: [localServer -> localhost] => {"changed": true, "cmd": ["echo", "Foo"], "delta": "0:00:01.002745", "end": "2020-11-17 11:22:25.733089", "rc": 0, "start": "2020-11-17 11:22:24.730344", "stderr": "", "stderr_lines": [], "stdout": "Foo", "stdout_lines": ["Foo"]}
TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:01.002745",
"end": "2020-11-17 11:22:25.733089",
"failed": false,
"rc": 0,
"start": "2020-11-17 11:22:24.730344",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:01.002745",
"end": "2020-11-17 11:22:25.733089",
"failed": false,
"rc": 0,
"start": "2020-11-17 11:22:24.730344",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
And just in case this something weirdly version specific, I'm on Ubuntu 20.04 and this is my version:
$ ansible --version
ansible 2.9.6
config file = /path/to/project/orchestration/ansible.cfg
configured module search path = ['/home/myUser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
The problem is the trailing comma. The task below works as expected and gives "msg: false"
- debug:
msg: '{{ "A" == "B" }}'
The trailing comma creates a tuple
- debug:
msg: '{{ "A" == "B", }}'
- debug:
msg: "{{ (false,)|type_debug }}"
gives
msg: (False,)
msg: tuple
Non-empty variables, including tuples, evaluate to true
- debug:
msg: OK
when: (A,)
gives
msg: OK
See CONDITIONAL_BARE_VARS
I have just copied your code into my lab VM and I noticed you are using a comma at the end of the "when" statement (Which I have never seen). I have removed them and get expected output.
When variable is Foo:
PLAY [localhost] ***************************************************************************************************************
TASK [foo] *********************************************************************************************************************
changed: [localhost]
TASK [debug] *******************************************************************************************************************
ok: [localhost] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:00.006168",
"end": "2020-11-17 12:50:11.863107",
"failed": false,
"rc": 0,
"start": "2020-11-17 12:50:11.856939",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
TASK [debug] *******************************************************************************************************************
skipping: [localhost]
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
When variable is Bar:
PLAY [localhost] ***************************************************************************************************************
TASK [foo] *********************************************************************************************************************
changed: [localhost]
TASK [debug] *******************************************************************************************************************
skipping: [localhost]
TASK [debug] *******************************************************************************************************************
ok: [localhost] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Bar"
],
"delta": "0:00:00.006077",
"end": "2020-11-17 12:53:24.142058",
"failed": false,
"rc": 0,
"start": "2020-11-17 12:53:24.135981",
"stderr": "",
"stderr_lines": [],
"stdout": "Bar",
"stdout_lines": [
"Bar"
]
}
}
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
My ansible version is:
ansible 2.7.10
python version = 2.7.5

Ansible: how to print var name while looping?

I have this inside setup/tasks/apps.yml:
- name: Mac App Store | Install apps.
shell: mas list | grep {{ item.id }} || mas install {{ item.id }}
with_items: "{{ mac_store_apps }}"
register: result
changed_when: result.stdout.find('Installed') != -1
I know I can use:
loop_control:
label: '{{ item.name }}'
But I want to print something like:
Attempting to Install {{ item.name }}
before each execution. How can that be done?
There is no way to generate output before each iteration of your loop; Ansible only produces output when a task (or an iteration of a looping task) is complete. If you are concerned that package installation can take a while and you want to provide some feedback, you could just print out a notice in advance, as in:
- debug:
msg: "Note: package installation may take several minutes to complete"
You can sort of get what you want by placing your install task in a separate file, and then calling include_tasks in a loop. For example, if we have the following playbook:
---
- hosts: localhost
gather_facts: false
vars:
mac_store_apps:
- name: foo
- name: bar
- name: baz
- name: qux
tasks:
- name: "Mac App Store | Install apps."
include_tasks: ./install.yml
with_items: "{{ mac_store_apps }}"
- debug:
var: all_results
And the following tasks in install.yml:
---
- name: "Mac App Store | Install {{ item.name }}"
shell: "true"
register: result
- name: Store result
set_fact:
all_results: "{{ all_results|default([]) + [{'item': item, 'result': result}] }}"
We will see as output:
PLAY [localhost] ******************************************************************************
TASK [Mac App Store | Install apps.] **********************************************************
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'foo'})
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'bar'})
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'baz'})
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'qux'})
TASK [Mac App Store | Install foo] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [Mac App Store | Install bar] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [Mac App Store | Install baz] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [Mac App Store | Install qux] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [debug] **********************************************************************************
ok: [localhost] => {
"all_results": [
{
"item": {
"name": "foo"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002398",
"end": "2019-04-23 11:19:25.146497",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.144099",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
},
{
"item": {
"name": "bar"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002245",
"end": "2019-04-23 11:19:25.285859",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.283614",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
},
{
"item": {
"name": "baz"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002406",
"end": "2019-04-23 11:19:25.426909",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.424503",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
},
{
"item": {
"name": "qux"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002232",
"end": "2019-04-23 11:19:25.574214",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.571982",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
]
}
PLAY RECAP ************************************************************************************
localhost : ok=13 changed=4 unreachable=0 failed=0

Ansible display the contents of a variable

If I have these 2 task
- name: Replace ServerIP in config_file on OTHER NODES
set_fact:
variable: "{{hostvars.localhost.new_ips.results}}"
- name: Display variable
debug: var=variable
The result of which are:
TASK: [Display variable] *********************************************************
ok: [vm2] => {
"variable": [
{
"changed": true,
"cmd": "echo \"11.11.4.74\"",
"delta": "0:00:00.002244",
"end": "2014-08-26 02:34:22.880447",
"invocation": {
"module_args": "echo \"11.11.4.74\"",
"module_name": "shell"
},
"item": "74",
"rc": 0,
"start": "2014-08-26 02:34:22.878203",
"stderr": "",
"stdout": "11.11.4.74"
},
{
"changed": true,
"cmd": "echo \"11.11.4.138\"",
"delta": "0:00:00.002156",
"end": "2014-08-26 02:34:22.958337",
"invocation": {
"module_args": "echo \"11.11.4.138\"",
"module_name": "shell"
},
"item": "138",
"rc": 0,
"start": "2014-08-26 02:34:22.956181",
"stderr": "",
"stdout": "11.11.4.138"
}
]
}
ok: [vm1] => {
"variable": [
{
"changed": true,
"cmd": "echo \"11.11.4.74\"",
"delta": "0:00:00.002244",
"end": "2014-08-26 02:34:22.880447",
"invocation": {
"module_args": "echo \"11.11.4.74\"",
"module_name": "shell"
},
"item": "74",
"rc": 0,
"start": "2014-08-26 02:34:22.878203",
"stderr": "",
"stdout": "11.11.4.74"
},
{
"changed": true,
"cmd": "echo \"11.11.4.138\"",
"delta": "0:00:00.002156",
"end": "2014-08-26 02:34:22.958337",
"invocation": {
"module_args": "echo \"11.11.4.138\"",
"module_name": "shell"
},
"item": "138",
"rc": 0,
"start": "2014-08-26 02:34:22.956181",
"stderr": "",
"stdout": "11.11.4.138"
}
]
}
Then how can I access the stdout part of variable only. Please note, I just require the stdout part of this variable i.e 11.11.4.74 and 11.11.4.138 (in a loop preferably)
You can either access it individually
{{ variable[0].stdout }}
and
{{ variable[1].stdout }}
OR use loop
- debug: var=item.stdout
with_items: variable

Resources