When I run my Ansible-Playbook, I want my task name to execute specifically

Viewed 512

My Playbook File:

---
- hosts : localhost
  become: yes
  tasks:
  - name: To install "{{ item }}"
    yum:
      name: "{{ item }}"
    with_items: 
    - git
    - tree
    - wget

When I execute the above playbook file, my output is :

PLAY [localhost] ****************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [localhost]

TASK [To install "{{ item }}"] **************************************************************************************************************************************************
changed: [localhost] => (item=[u'git', u'tree', u'wget'])

PLAY RECAP **********************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Here TASK [To install "{{ item }}"] => I don't want the word item, I want it to be specific. Like when the loop is coming at git, it should be "To install git".. Then at tree, it should be "To install tree" etc., I should get 3 TASK because of 3 installation. I tried using debug module, its getting failed.

Note : I want these variables (git, tree, wget) to be present in my playbook.yaml file.

Please let me know if you have any queries. Thanks in Advance !!

1 Answers

The name of the task won't be evaluated in each iteration of the loop. To achieve what you want, it's possible to put the task into a separate file and include it in the loop. For example, the file and the playbook below

shell> cat install_pkg.yml 
- name: "To install {{ item }}"
  debug:
    msg: "name: {{ item }}"

shell> cat playbook.yml
- hosts: localhost
  tasks:
    - include_tasks: install_pkg.yml
      loop:
        - git
        - tree
        - wget

give (abridged)

shell> ansible-playbook playbook.yml 

TASK [include_tasks] ****
included: /scratch/install_pkg.yml for localhost
included: /scratch/install_pkg.yml for localhost
included: /scratch/install_pkg.yml for localhost

TASK [To install git] ****
ok: [localhost] => 
  msg: 'name: git'

TASK [To install tree] ****
ok: [localhost] => 
  msg: 'name: tree'

TASK [To install wget] ****
ok: [localhost] => 
  msg: 'name: wget'
Related