Floren
Floren

Reputation: 541

Use a different task name for Ansible roles execution?

I have the current node role:

$ tree roles/node
roles/node
├── defaults
│   └── main.yaml
└── tasks
    ├── main.yaml
    ├── reset.yaml
    └── unmount.yaml

Current provisioning.yaml playbook is using the main tasks:

- name: Node Provisioning
  hosts: node
  become: true
  gather_facts: true
  roles:
    - role: node

I would like to create a separate reset.yaml playbook which uses the reset tasks:

- name: Node Reset
  hosts: node
  become: true
  gather_facts: true
  roles:
    - role: node

I understand I could create a separate role or use tags, but my goal is to use the same role name and define into playbook the reset tasks name, instead of main.

Is there a proper solution allowing me to use a specific tasks_from in my playbook scenario? The example above is a simplified playbook, for proof of concept.

Upvotes: 0

Views: 509

Answers (1)

larsks
larsks

Reputation: 311721

There are three ways to include a role in your playbook:

  • Use the classic roles: directive in the play;
  • Using the dynamic include_role task
  • Using the static import_role task

While the roles: directive doesn't support a tasks_from argument, the other two options do. You could write:

- name: Node Reset
  hosts: node
  become: true
  gather_facts: true
  tasks:
    - import_role:
        name: node
        tasks_from: reset.yaml

Here's a complete test walk-through. If used the following layout:

.
├── playbook.yaml
└── roles
    └── node
        └── tasks
            ├── main.yaml
            ├── reset.yaml
            └── umount.yaml

Where roles/node/tasks/reset.yaml contains:

- debug:
    msg: "This is reset.yaml"

- name: Umount filesystem
  ansible.builtin.include_tasks:
    file: umount.yaml
  with_items:
    - /run/netns
    - /var/lib/kubelet
  loop_control:
    loop_var: mounted_fs

And roles/node/tasks/unmount.yaml contains:

- debug:
    msg: "This is umount.yaml; fs: {{ mounted_fs }}"

If I run this playbook.yml:

- hosts: localhost
  gather_facts: false
  tasks:
    - import_role:
        name: node
        tasks_from: reset

I get as output:

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

TASK [node : debug] ************************************************************
ok: [localhost] => {
    "msg": "This is reset.yaml"
}

TASK [node : Umount filesystem] ************************************************
included: /home/lars/tmp/ansible/roles/node/tasks/umount.yaml for localhost => (item=/run/netns)
included: /home/lars/tmp/ansible/roles/node/tasks/umount.yaml for localhost => (item=/var/lib/kubelet)

TASK [node : debug] ************************************************************
ok: [localhost] => {
    "msg": "This is umount.yaml; fs: /run/netns"
}

TASK [node : debug] ************************************************************
ok: [localhost] => {
    "msg": "This is umount.yaml; fs: /var/lib/kubelet"
}

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

You can find my complete test setup here.

Upvotes: 3

Related Questions