1

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?

1 Answer 1

1

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   
Sign up to request clarification or add additional context in comments.

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.