1

I have a fairly complex set of Playbooks and imported Roles building an application platform. Everything is defined in a single hosts.ini file, and extensive use is being made of groups to identify specialist server roles: web, database, search, etc.

However the overall platform supports two distinct instances of the application, and in some cases I need to set-up specific links between two of the four web nodes. For example, for the hosts list below I want to set-up gluster volumes between app1_node1 and app1_node2, and app2_node1 and app2_node2.

[web]
app1_node1
app1_node2
app2_node1
app2_node2

And have a playbook which does that, but only for two nodes at once. So if the hosts file also contains the following:

[webapp1]
app1_node1
app1_node2

[webapp2]
app2_node1
app2_node2

and the playbook is invoked with --limit=webapp1 (or webapp2) everything works fine.

But I'm not sure of the best way to build everything in one go. I could, for example, duplicate the playbook and have different hosts statements in each. But that doesn't seem like a good idea.

The best solution I've come up with is to have an intermediate playbook launcher that looks like this:

---
- name: PLAYBOOK playbook_launcher.yml
  hosts: webapp1

  tasks:

- name: Include playbook
  import_playbook: theplaybook.yml

- hosts: webapp2

  tasks:

- name: Include playbook
  import_playbook: theplaybook.yml

With the actual playbook having a broader hosts statement.

---
- name: PLAYBOOK theplaybook.yml
  hosts: web

Is this the best approach, or am I missing something obvious in the way Ansible works that makes this easier?

1 Answer 1

0

Try using:

--limit=webapp1:webapp2
3
  • wildcards would be the option as well . use varaible in host field. - host: "{{ myhosts }}"
    – meguest
    Sep 23, 2019 at 13:35
  • wildcards would be the option as well and pass it as a extra vars. use variable in host field. - host: "{{ myhosts }}" --- Invoke playbook by ansible-playbook -i <inventory> -e "myhosts=webapp*" ------ OR If hostname contains same name patterns... [webapp_gluster:children] webapp_glusterfs1 webapp_glusterfs2 [webapp_glusterfs1] webapp[01:02].example.com [webapp_glusterfs1] webapp[03:04].example.com Now, we can invoke playbook with -e "myhost=webapp_gluster"
    – meguest
    Sep 23, 2019 at 13:49
  • Putting a list after --limit is quite a neat solution. But I prefer my solution, as I like the default position to be that running the site.yml playbook without --limit builds everything, and I like limiting --limit to when I'm building a subset of the overall environment. Also the ansible-playbook command is already quite busy (--inventory --private-key, --ask-vault-pass), and adding --limit or --extra_vars all the time makes it even more complicated. You last solution would probably work OK for this situation, but wouldn't really scale up to 10 or 20 instances of the application.
    – P Burke
    Sep 24, 2019 at 8:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .