Answers: <p>If you want to &quot;add base_packages.yml to the title of the task&quot; you'll have to add it to the names of the tasks. For example</p>
  <pre class="lang-yaml prettyprint-override"><code>shell&gt; cat base_packages.yml
  ---
  - name: &quot;base_packages: .... &quot;
  </code></pre>
  <p><code>sed</code> might help to automate it.</p>
  <p>You can use Ansible to configure its code. For example, the playbook below adds missing names to the <em>import_tasks</em> in files listed in the variable <em>format_files</em></p>
  <pre class="lang-yaml prettyprint-override"><code> shell&gt; cat configure.yml
  - hosts: localhost
  
    vars:
  
      format_files:
        - roles/common/tasks/main.yml
  
    tasks:
  
      - copy:
          dest: &quot;{{ item }}&quot;
          content: &quot;{{ content }}&quot;
        loop: &quot;{{ format_files }}&quot;
        vars:
          lines: &quot;{{ lookup('file', item).splitlines() }}&quot;
          regex: '(.*)- import_tasks: (.*)$'
          replace: '\1- name: Import \2'
          content: |
            {% for line in lines %}
            {% if line is match(regex) %}
            {{ line | regex_replace(regex, replace) }}
            {{ line | regex_replace('- ', '  ')  }}
            {% else %}
            {{ line }}
            {% endif %}
            {% endfor %}
  </code></pre>
  <p>gives (abridged) running with <em>-CD</em> options</p>
  <pre class="lang-yaml prettyprint-override"><code>shell&gt; ansible-playbook configure.yml -CD
    ...
  TASK [copy] *********************************************************************************************************
  --- before: roles/common/tasks/main.yml
  +++ after: roles/common/tasks/main.yml
  @@ -2,19 +2,32 @@
   # tasks file for common
   - name: python installation on centos 6 machines
     block:
  -  - import_tasks: python2_centos6.yml
  -  - import_tasks: python3_centos6.yml
  +  - name: Import python2_centos6.yml
  +    import_tasks: python2_centos6.yml
  +  - name: Import python3_centos6.yml
  +    import_tasks: python3_centos6.yml
     when: ansible_distribution_major_version == &quot;6&quot;
  -- import_tasks: python.yml
  +- name: Import python.yml
  +  import_tasks: python.yml
     when: ansible_distribution_major_version == &quot;7&quot;
  -- import_tasks: pip.yml
  -- import_tasks: pip_packages.yml
  -- import_tasks: repo.yml
  -- import_tasks: yum.yml
  -- import_tasks: selinux.yml
  -- import_tasks: base_packages.yml
  -- import_tasks: iptables.yml
  +- name: Import pip.yml
  +  import_tasks: pip.yml
  +- name: Import pip_packages.yml
  +  import_tasks: pip_packages.yml
  +- name: Import repo.yml
  +  import_tasks: repo.yml
  +- name: Import yum.yml
  +  import_tasks: yum.yml
  +- name: Import selinux.yml
  +  import_tasks: selinux.yml
  +- name: Import base_packages.yml
  +  import_tasks: base_packages.yml
  +- name: Import iptables.yml
  +  import_tasks: iptables.yml
     when: ansible_distribution_major_version == &quot;7&quot;
  -#- import_tasks: time.yml
  -- import_tasks: locale.yml
  -- import_tasks: vim.yml
  +#- name: Import time.yml
  +#  import_tasks: time.yml
  +- name: Import locale.yml
  +  import_tasks: locale.yml
  +- name: Import vim.yml
  +  import_tasks: vim.yml
  
  changed: [localhost] =&gt; (item=roles/common/tasks/main.yml)
  </code></pre>
  <p>The result will be the formatted file below</p>
  <pre class="lang-yaml prettyprint-override"><code>shell&gt; cat roles/common/tasks/main.yml
  ---
  # tasks file for common
  - name: python installation on centos 6 machines
    block:
    - name: Import python2_centos6.yml
      import_tasks: python2_centos6.yml
    - name: Import python3_centos6.yml
      import_tasks: python3_centos6.yml
    when: ansible_distribution_major_version == &quot;6&quot;
  - name: Import python.yml
    import_tasks: python.yml
    when: ansible_distribution_major_version == &quot;7&quot;
  - name: Import pip.yml
    import_tasks: pip.yml
  - name: Import pip_packages.yml
    import_tasks: pip_packages.yml
  - name: Import repo.yml
    import_tasks: repo.yml
  - name: Import yum.yml
    import_tasks: yum.yml
  - name: Import selinux.yml
    import_tasks: selinux.yml
  - name: Import base_packages.yml
    import_tasks: base_packages.yml
  - name: Import iptables.yml
    import_tasks: iptables.yml
    when: ansible_distribution_major_version == &quot;7&quot;
  #- name: Import time.yml
  #  import_tasks: time.yml
  - name: Import locale.yml
    import_tasks: locale.yml
  - name: Import vim.yml
    import_tasks: vim.yml
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-09T13:00:38.867Z
Answers: For example like this: `find . -type f -name "*.yml" -exec sed -i -e "s~^- ~- name: \"{}\"\\n  ~" {} \;`
Answers: informatik-handwerk.de
Answers: CC BY-SA 4.0
Answers: 2020-05-19T17:40:06.573Z
Answers: 2024-11-09T13:53:01.990Z
Answers: 2024-11-09T13:53:01.990Z
Answers: Vladimir Botka
Body: <p>I wrote an Ansible role which does lots of things, it's called "common" and all Ansible hosts are using it.</p>
  
  <p>The directory structure is as follows:</p>
  
  <pre><code>./common
  │   ├── ./common/README.md
  │   ├── ./common/defaults
  │   │   └── ./common/defaults/main.yml
  │   ├── ./common/handlers
  │   │   └── ./common/handlers/main.yml
  │   ├── ./common/meta
  │   │   └── ./common/meta/main.yml
  │   ├── ./common/tasks
  │   │   ├── ./common/tasks/base_packages.yml
  │   │   ├── ./common/tasks/iptables.yml
  │   │   ├── ./common/tasks/locale.yml
  │   │   ├── ./common/tasks/main.yml
  │   │   ├── ./common/tasks/pip.yml
  │   │   ├── ./common/tasks/pip_packages.yml
  │   │   ├── ./common/tasks/python.yml
  │   │   ├── ./common/tasks/python2_centos6.yml
  │   │   ├── ./common/tasks/python3_centos6.yml
  │   │   ├── ./common/tasks/repo.yml
  │   │   ├── ./common/tasks/selinux.yml
  │   │   ├── ./common/tasks/time.yml
  │   │   ├── ./common/tasks/vim.yml
  │   │   └── ./common/tasks/yum.yml
  │   ├── ./common/templates
  │   │   ├── ./common/templates/chrony.conf.j2
  │   │   └── ./common/templates/ntp.conf.j2
  │   ├── ./common/tests
  │   │   ├── ./common/tests/inventory
  │   │   └── ./common/tests/test.yml
  │   └── ./common/vars
  │       └── ./common/vars/main.yml
  </code></pre>
  
  <p>main.yml looks like so:</p>
  
  <pre><code>---
  # tasks file for common
  - name: python installation on centos 6 machines
    block:
    - import_tasks: python2_centos6.yml
    - import_tasks: python3_centos6.yml
    when: ansible_distribution_major_version == "6"
  - import_tasks: python.yml
    when: ansible_distribution_major_version == "7"
  - import_tasks: pip.yml
  - import_tasks: pip_packages.yml
  - import_tasks: repo.yml
  - import_tasks: yum.yml
  - import_tasks: selinux.yml
  - import_tasks: base_packages.yml
  - import_tasks: iptables.yml
    when: ansible_distribution_major_version == "7"
  #- import_tasks: time.yml
  - import_tasks: locale.yml
  - import_tasks: vim.yml
  </code></pre>
  
  <p>The playbook that I'm currently running:</p>
  
  <pre><code>---
  
  # local_slave_playbook_c6.yml
  - hosts: localhost
    vars:
      ansible_user_shell: /bin/bash
    gather_facts: true
    roles:
      - common
      - consul-client
      - hosts
      #- node-exporter
      - cron
      - jenkins-slave
  </code></pre>
  
  <p>While I run the "common" role on a server, I want to know which imported_tasks file is currently running for debug matters. So for example, while Ansible "takes care" for the installation of base packages, I want to add "base_packages.yml" to the title of the task.</p>
  
  <p>I looked through the <a href="https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html" rel="nofollow noreferrer">list of Ansible special variables</a> but couldn't find the variable that I need.</p>
  
  <p>There is one special variable that looks right "{{ ansible_play_name }}" but that gives me "localhost".</p>
  
Tags: <ansible>
Title: Ansible: how to display the name of the currently running included_tasks playbook file?
UserDisplayName: Itai Ganot
ThreadText: # Ansible: how to display the name of the currently running included_tasks playbook file?
  
  (Asked by: username_0 on 2020-05-19)
  
  I wrote an Ansible role which does lots of things, it's called "common" and all Ansible hosts are using it.
  
  The directory structure is as follows:
  
  ```
  ./common
  │   ├── ./common/README.md
  │   ├── ./common/defaults
  │   │   └── ./common/defaults/main.yml
  │   ├── ./common/handlers
  │   │   └── ./common/handlers/main.yml
  │   ├── ./common/meta
  │   │   └── ./common/meta/main.yml
  │   ├── ./common/tasks
  │   │   ├── ./common/tasks/base_packages.yml
  │   │   ├── ./common/tasks/iptables.yml
  │   │   ├── ./common/tasks/locale.yml
  │   │   ├── ./common/tasks/main.yml
  │   │   ├── ./common/tasks/pip.yml
  │   │   ├── ./common/tasks/pip_packages.yml
  │   │   ├── ./common/tasks/python.yml
  │   │   ├── ./common/tasks/python2_centos6.yml
  │   │   ├── ./common/tasks/python3_centos6.yml
  │   │   ├── ./common/tasks/repo.yml
  │   │   ├── ./common/tasks/selinux.yml
  │   │   ├── ./common/tasks/time.yml
  │   │   ├── ./common/tasks/vim.yml
  │   │   └── ./common/tasks/yum.yml
  │   ├── ./common/templates
  │   │   ├── ./common/templates/chrony.conf.j2
  │   │   └── ./common/templates/ntp.conf.j2
  │   ├── ./common/tests
  │   │   ├── ./common/tests/inventory
  │   │   └── ./common/tests/test.yml
  │   └── ./common/vars
  │       └── ./common/vars/main.yml
  
  ```
  
  main.yml looks like so:
  
  ```
  ---
  # tasks file for common
  - name: python installation on centos 6 machines
    block:
    - import_tasks: python2_centos6.yml
    - import_tasks: python3_centos6.yml
    when: ansible_distribution_major_version == "6"
  - import_tasks: python.yml
    when: ansible_distribution_major_version == "7"
  - import_tasks: pip.yml
  - import_tasks: pip_packages.yml
  - import_tasks: repo.yml
  - import_tasks: yum.yml
  - import_tasks: selinux.yml
  - import_tasks: base_packages.yml
  - import_tasks: iptables.yml
    when: ansible_distribution_major_version == "7"
  #- import_tasks: time.yml
  - import_tasks: locale.yml
  - import_tasks: vim.yml
  
  ```
  
  The playbook that I'm currently running:
  
  ```
  ---
  
  # local_slave_playbook_c6.yml
  - hosts: localhost
    vars:
      ansible_user_shell: /bin/bash
    gather_facts: true
    roles:
      - common
      - consul-client
      - hosts
      #- node-exporter
      - cron
      - jenkins-slave
  
  ```
  
  While I run the "common" role on a server, I want to know which imported\_tasks file is currently running for debug matters. So for example, while Ansible "takes care" for the installation of base packages, I want to add "base\_packages.yml" to the title of the task.
  
  I looked through the [list of Ansible special variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html) but couldn't find the variable that I need.
  
  There is one special variable that looks right "{{ ansible\_play\_name }}" but that gives me "localhost".
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-05-19. Score: 1
  
  If you want to "add base\_packages.yml to the title of the task" you'll have to add it to the names of the tasks. For example
  
  ```yaml
  shell> cat base_packages.yml
  ---
  - name: "base_packages: .... "
  
  ```
  
  `sed` might help to automate it.
  
  You can use Ansible to configure its code. For example, the playbook below adds missing names to the *import\_tasks* in files listed in the variable *format\_files*
  
  ```yaml
   shell> cat configure.yml
  - hosts: localhost
  
    vars:
  
      format_files:
        - roles/common/tasks/main.yml
  
    tasks:
  
      - copy:
          dest: "{{ item }}"
          content: "{{ content }}"
        loop: "{{ format_files }}"
        vars:
          lines: "{{ lookup('file', item).splitlines() }}"
          regex: '(.*)- import_tasks: (.*)$'
          replace: '\1- name: Import \2'
          content: |
            {% for line in lines %}
            {% if line is match(regex) %}
            {{ line | regex_replace(regex, replace) }}
            {{ line | regex_replace('- ', '  ')  }}
            {% else %}
            {{ line }}
            {% endif %}
            {% endfor %}
  
  ```
  
  gives (abridged) running with *-CD* options
  
  ```yaml
  shell> ansible-playbook configure.yml -CD
    ...
  TASK [copy] *********************************************************************************************************
  --- before: roles/common/tasks/main.yml
  +++ after: roles/common/tasks/main.yml
  @@ -2,19 +2,32 @@
   # tasks file for common
   - name: python installation on centos 6 machines
     block:
  -  - import_tasks: python2_centos6.yml
  -  - import_tasks: python3_centos6.yml
  +  - name: Import python2_centos6.yml
  +    import_tasks: python2_centos6.yml
  +  - name: Import python3_centos6.yml
  +    import_tasks: python3_centos6.yml
     when: ansible_distribution_major_version == "6"
  -- import_tasks: python.yml
  +- name: Import python.yml
  +  import_tasks: python.yml
     when: ansible_distribution_major_version == "7"
  -- import_tasks: pip.yml
  -- import_tasks: pip_packages.yml
  -- import_tasks: repo.yml
  -- import_tasks: yum.yml
  -- import_tasks: selinux.yml
  -- import_tasks: base_packages.yml
  -- import_tasks: iptables.yml
  +- name: Import pip.yml
  +  import_tasks: pip.yml
  +- name: Import pip_packages.yml
  +  import_tasks: pip_packages.yml
  +- name: Import repo.yml
  +  import_tasks: repo.yml
  +- name: Import yum.yml
  +  import_tasks: yum.yml
  +- name: Import selinux.yml
  +  import_tasks: selinux.yml
  +- name: Import base_packages.yml
  +  import_tasks: base_packages.yml
  +- name: Import iptables.yml
  +  import_tasks: iptables.yml
     when: ansible_distribution_major_version == "7"
  -#- import_tasks: time.yml
  -- import_tasks: locale.yml
  -- import_tasks: vim.yml
  +#- name: Import time.yml
  +#  import_tasks: time.yml
  +- name: Import locale.yml
  +  import_tasks: locale.yml
  +- name: Import vim.yml
  +  import_tasks: vim.yml
  
  changed: [localhost] => (item=roles/common/tasks/main.yml)
  
  ```
  
  The result will be the formatted file below
  
  ```yaml
  shell> cat roles/common/tasks/main.yml
  ---
  # tasks file for common
  - name: python installation on centos 6 machines
    block:
    - name: Import python2_centos6.yml
      import_tasks: python2_centos6.yml
    - name: Import python3_centos6.yml
      import_tasks: python3_centos6.yml
    when: ansible_distribution_major_version == "6"
  - name: Import python.yml
    import_tasks: python.yml
    when: ansible_distribution_major_version == "7"
  - name: Import pip.yml
    import_tasks: pip.yml
  - name: Import pip_packages.yml
    import_tasks: pip_packages.yml
  - name: Import repo.yml
    import_tasks: repo.yml
  - name: Import yum.yml
    import_tasks: yum.yml
  - name: Import selinux.yml
    import_tasks: selinux.yml
  - name: Import base_packages.yml
    import_tasks: base_packages.yml
  - name: Import iptables.yml
    import_tasks: iptables.yml
    when: ansible_distribution_major_version == "7"
  #- name: Import time.yml
  #  import_tasks: time.yml
  - name: Import locale.yml
    import_tasks: locale.yml
  - name: Import vim.yml
    import_tasks: vim.yml
  
  ```
  
  > **Comments:**
  > **username_2**: For example like this: `find . -type f -name "*.yml" -exec sed -i -e "s~^- ~- name: \"{}\"\\n  ~" {} \;`  
  
  ---
  
  