4

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?

dan_linder
  • 881
  • 1
  • 9
  • 30
  • For me, your current `main.yml` looks clean, but why not have one role for setting up new servers and one for decommissioning instead? – ilias-sp Mar 04 '19 at 23:01

1 Answers1

7

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.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63