Search code examples
ansibleansible-inventory

Ansible: How to run particular host in Vagrant?


I have a complex Ansible setup with several hosts in my group file. Something like this.

# hosts/groups

[local]
127.0.0.1

[server1]
server1.domain.com

[server2]
server2.domain.com

[group1]
local
server1

[group2]
local
server2

This way, I can run both groups against localhost:2222 which is my Vagrant box, however, they will be both executed. For testing I would very much prefer to choose, which setting, I would like to test. I have experimented with --extra-vars arguments and conditionals, which is pretty ugly. Is there a way to use the extra_vars argument with the host configuration. Using a command like ...

ansible-playbook playbook.yml -i hosts -l 127.0.0.1:2222 --extra-vars "vhost=server1.domain.com"

Or am I entirely wrong.


Solution

  • I don't think there's an easy way to do this via alterations to how you executes Ansible.

    The best option I can think of involves potentially re-organizing your playbooks. If you create group1.yaml and group2.yaml, each containing the statements necessary for setting up group1 and group2, respectively, then you can run something like

    [$]> ansible-playbook -l 127.0.0.1:2222 group1.yaml
    

    to only run the group1 configuration against your development instance.

    If you still want a convenient way to run all tasks, alter your playbook.yaml to include the other playbooks:

    - include: group1.yaml
    - include: group2.yaml