Reduce duplicate/repeated steps in playbook

Hi ,Team

Require your help to reduce repeated steps , in the following playbook ,3 roles are common ,but 2 roles( permission.yml & reload_base_service.yml) variable value is different

  • hosts: masters

gather_facts: yes
vars:

  • owner: “roks”

  • mode: “0644”

  • base_service: “mot-idm”
    tasks:

  • include_role:
    name: motors
    tasks_from: “{{ item }}”
    with_items:

  • permission.yml

  • reload_base_service.yml

  • restart_component.yaml

  • hosts: organization
    gather_facts: yes
    vars:

  • owner: “taxi”

  • mode: “0755”

  • base_service: “mzee-org”
    tasks:

  • include_role:
    name: motors
    tasks_from: “{{ item }}”
    with_items:

  • permission.yml

  • reload_base_service.yml

  • restart_component.yaml

  • hosts: doxy
    gather_facts: yes
    vars:

  • owner: “matter”

  • mode: “0644”

  • base_service: “rater-x”
    tasks:

  • include_role:
    name: motors
    tasks_from: “{{ item }}”
    with_items:

  • permission.yml

  • reload_base_service.yml

  • restart_component.yaml

Thanks

Hi Team

Any thoughts on reducing this repetition !!

Thanks

Yes check if this logic works

  1. Create a one group in inventory with all child group masters, organization and doxy
    So that you can run the restart role

  2. Set the variables on group level masters, organization and doxy
    So that your role will automatically read this variables while executing

So it will be only two plays
Remember running role sequence is in your hand

Regards
Gg

Thanks Gajendra ,will test on the mentioned suggestions & revert back.

Hi Pawan, Gajendra,

You can use Anchors in YAML, like

https://github.com/ansible-collections/vmware/blob/main/tests/integration/targets/vmware_about_facts/tasks/main.yml#L37
https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/
https://github.com/Akasurde/ansible-reproducers/blob/master/useful_pbs/debug_anchor.yml