Reputation: 83
Below is a snippet from a playbook. How can I print a message after the command is run for each loop item? Something like
image.pyc is now running for item1
image.pyc is now running for item2
and so on
- name: Image Server(s)
command: python3 image.pyc {{ item }} "{{ systemVersion }}"
register: async_out
async: 7200
poll: 0
with_items: "{{ hostinfo_input.hosts }}"
What I want is simply this
TASK [Image Server(s)] *************
changed: [localhost] => (item=8m007)
changed: [localhost] => (item=8m013)
Running image.pyc for 8m007
Running image.pyc for 8m013
Upvotes: 0
Views: 1217
Reputation: 12063
Your requirement as it is described currently
How can I print a message after the command is run for each loop item?
could just be fulfilled by loop
output with label
This is for making console output more readable ...
in example
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: Image Server(s)
command: "python3 image.pyc {{ item }} {{ systemVersion }}"
register: async_out
async: 7200
poll: 0
loop: "{{ ansible_play_hosts }}"
loop_control:
label: "Running image.pyc for {{ item }}"
resulting into an output of
TASK [Image Server(s)] *******************************************************
changed: [test1.example.com] => (item=Running image.pyc for test1.example.com)
changed: [test2.example.com] => (item=Running image.pyc for test2.example.com)
...
The given example just provides the message after the command is sent over to the Remote Node and was started there. That is simply working because it is not defined who should provide (print) the message, the loop
via label
, async
or the script
. See also the comment of @Zeitounator ...
Upvotes: 0
Reputation: 730
Have a look at Ansible
documentation for the debug
module: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html
E.g.use it like this:
# playbook.yaml
---
- hosts: localhost
connection: local
vars:
ip_addresses:
- 10.0.0.10
- 10.0.0.11
tasks:
- name: Task
include_tasks: task.yaml
loop: "{{ ip_addresses }}"
# task.yaml
---
- name: Command
ansible.builtin.shell: "echo {{ item }}"
register: output
- name: Message
ansible.builtin.debug:
msg: |
- ip_address={{ item }}
- ouput={{ output }}
Unfortunately this does not work with async
. See https://github.com/ansible/ansible/issues/22716.
Upvotes: 0
Reputation: 44615
In a nutshell, extremely simple, untested, to be adapted to your use case specifically in terms of error/output control:
- name: Image Server(s)
ansible.builtin.command: python3 image.pyc {{ item }} "{{ systemVersion }}"
register: async_out
async: 7200
poll: 0
with_items: "{{ hostinfo_input.hosts }}"
- name: Wait for commands to finish
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 720
delay: 10
loop: "{{ async_out.results }}"
- name: Show async command output
ansible.builtin.debug:
msg: "{{ item.stdout }}"
loop: "{{ async_out.results }}"
- name: Good guests always clean up after themselves
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
mode: cleanup
loop: "{{ async_out.results }}"
References:
Upvotes: 2