dan_linder
dan_linder

Reputation: 1026

Starting role from "tasks/alternate.yml" instead of "tasks/main.yml"

I have a large playbook that uses multiple roles to setup new servers. I'd like to re-use the playbook but for the decommission stage instead of calling into role_name/tasks/main.yml and having a lot of when: statements, I'd like to tell Ansible to call the role but start in role_name/tasks/decommission.yml.

As a first test I setup my main.yml file like this:

 - name: "Provisioning new server"
   block:
     - name: "Include the provisioning steps."
       include_tasks: provision.yml
   when:
     - not decom

 - name: "DECOM - Unregister from Satellite server"
   block:
     - name: "DECOM - Include the deprovision steps."
       include_tasks: decommission.yml
   when:
     - decom

But that's getting really ugly to maintain. Is this possible or am I overlooking an alternative way to setup the playbook?

Upvotes: 5

Views: 2814

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68329

Q: "Tell Ansible to call the role but start in role_name/tasks/decommission.yml"

A: Use include_role

    - include_role:
        name: role_name
        tasks_from: decommission.yml

,or import_role

    - import_role:
        name: role_name
        tasks_from: decommission.yml

See Re-using files and roles on what is the difference between including and importing a role.

Upvotes: 8

Related Questions