1

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

1 Answer 1

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

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

 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

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

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
1
  • For example like this: find . -type f -name "*.yml" -exec sed -i -e "s~^- ~- name: \"{}\"\\n ~" {} \; Commented Nov 9 at 13:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.