0
votes

normally in ansible you always define a specific host for a specific playbook. But what is if I want to run a playbook for multiple hosts?

For a bit more clarity: I have following groups:

  • Webservers
  • Dev Clients

and following playbooks:

  • install main software
  • install certbot
  • install nginx
  • install vscode

For obvious reasons for sure the nginx playbook should have the entry: hosts: Webservers but what is with the main software playbook. This playbook should include both hosts.

In the end I want to have many different playbooks and groups and the only thing I wanna execute is: ansible-playbook ~/playbooks/webservers.yml and this triggers: "main software, certbot, nginx..." via includes. But this doesn't work without editing the hosts section of every playbook that is included in the webservers.yml.

Is it possible to tell a playbook that it should run on the hosts from the main-playbook which has included it? like: webservers.yml -> hosts -> nginx.yml -> run on all clients which are entered in webservers.yml?

Does somebody of you has an idea? Or is this just not realizable with ansible?

Regards

1

1 Answers

0
votes

You could use include module with when statement using group_names var as below:

webservers.yml

- hosts: all
  tasks:
    - name: Include Main Software playbook
      include: main_software.yml

    - name: Include Nginx playbook
      include: nginx.yml
      when: "'webservers' in {{ group_names }}"

    - name: Include Vscode playbook
      include: vscode.yml
      when: "'DevClients' in {{ group_names }}"

Remember main_software.yml, nginx.yml and vscode.yml playbooks must have a list of tasks not a play.