0

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

3 Answers 3

2

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:

Sign up to request clarification or add additional context in comments.

4 Comments

This won't come to "- name: Show async command output" task until 720x10 seconds are over. This is not what I want. What I want is this TASK [Image Server(s)] ******************************************************************************************************************************************************************************* changed: [localhost] => (item=8m007) changed: [localhost] => (item=8m013) Running image.pyc for 8m007 Running image.pyc for 8m013
Please do not add needed code/info/output needed to answer your question in comments: edit your question. Thanks
This won't come to "- name: Show async command output" task until 720x10 seconds are over. <= wrong. It will stop if those commands are over before that deadline. So 7200s is a maximum time, as your requested with async: 7200.
Just to put away a possible misunderstanding: are you trying to display the output of the command synchronously (i.e. as it is played in real time on the target)? If yes, this is not possible. async or not, you will have to wait for the task to be finished before you get its result and have access to its stdout.
0

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.

3 Comments

How does the example relates to and answers the very minimal yet specif case posted in OP?
Unfortunately this does not work with async <= then for which exact reason did you add an include_tasks in your edit? Why is this mandatory since it breaks async?
I was not sure whether async is a must since the question only states "How can I print a message after the command is run for each loop item." but the code containes aszync. The include_task was used because block was not possible with loop. If you prefer I can delete the full post @Zeitounator.
0

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 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.