nimgeneva
nimgeneva

Reputation: 89

Ansible - How to manage Large list of small tasks

I have written lots of small yaml task files for an ansible project. Only few of these tasks files are being reused (say 30%). I wonder how to manage this big list of tasks, should I convert all of them to roles, and call the playbook with roles: Playbook as below (pls ignore the syntaxe), will be clear, But I do not like to have a role for each simple task.

- name: playbook with roles for each task
  hosts: all
  roles:
    - small_task_1
    - small_task_2
    - small_task_3
    - small_task_4
    ....
    ....
    - small_task_20

I liked the idea of putting them 2-3 roles, and call the task with include_task (or import_task), but the problem is, to each call of a task, I have to add "import_role: , name: , tasks_from" EVEN THOUGH It's from the same role!

$ ansible-galaxy role list $ /home/user1/.ansible/roles

in roles_a and roles_b, I may have around 10 yaml tasks for each

,,,

,,,

Above thing is not very handy...

I would like to group the tasks in a "role" (or any other ansible thing), and call the tasks from that group... something like :

,,,

,,,

I've tried with block , but it does not shorten the playbook.

Can anyone guide me, please?

Upvotes: 1

Views: 733

Answers (1)

larsks
larsks

Reputation: 311556

For your final example, you can just use a loop directive on your include_role task, like this:

- hosts: localhost
  gather_facts: false
  tasks:
    - include_role:
        name: role_a
        tasks_from: "{{ item }}"
      loop:
        - small_task_1
        - small_task_2
        - small_task_3
        - small_task_4

Upvotes: 1

Related Questions