-1

I have this sample inventory file:

[web1] // Web Server 1 contains the following sites:
example1.com
example2.com

[web2] // Web Server 2 contains the following sites:
example3.com
example4.com

But whenever I run a playbook for example with apt-get update command it tries to iterate through all the sites in the given web servers above.

How do I run the playbook in the host itself and not the example*.com sites because it's redundant.

EDIT

As requested here's the yaml file: (nothing fancy here.)

# Using all but --limit the execution to webservers
# I know I can use `webservers` but this was deliberate
- hosts: all 
  tasks:
    - // sudo apt-get update
JohnnyQ
  • 117
  • 2
  • 8

3 Answers3

0

hosts: all

This means that it will run on all listed hosts. If you want to restrict it to a particular group then you have to do that e.g.

[web1]
host1.example.com
host2.example.com

[web2]
host6.example.com
host10.example.com

with the above inventory

hosts: web1

would apply to host1, host2.

hosts: web2

would apply to host6, host10.

hosts: all

would apply to, well all of them

user9517
  • 115,471
  • 20
  • 215
  • 297
  • but notice i used `--limit webservers` so it will not apply to all. Anyway the real question is how can I restrict the plays to the web hosts and not the sites found on each host? – JohnnyQ Jun 09 '16 at 06:59
0

My temporary solution is to create a different group for the web server itself and run the server specific configuration from there.

webserver1 ansible_host=10.10.1.2
webserver2 ansible_host=10.10.1.3

[webservers]
webserver1
webserver2

[web1]
example1.com
example2.com

[web2]
example3.com
example4.com

So I can run ansible-playbook playbook.yml --limit webservers.

JohnnyQ
  • 117
  • 2
  • 8
0

inventory:

[webservers]
webserver1
webserver2

playbook:

- hosts: webservers
  roles:
    - { role: common, tag: common }
- hosts: webserver1
    - { role: host1.example.com, tag: host1.example.com }
    - { role: host2.example.com, tag: host2.example.com }
- hosts: webserver2
    - { role: host3.example.com, tag: host3.example.com }
    - { role: host3.example.com, tag: host4.example.com }

Roles:

host1.example.com/
  tasks/
    main.yml
  vars/
    main.yml
host2.example.com/
    ...

All sites on webserver1:

ansible-playbook playbook.yml -l webserver1

Site host3.example.com on it's webserver:

ansible-playbook playbook.yml -t host3.example.com

Shell command on webservers group:

ansible webservers -m shell -a "sudo apt-get update"

You should check out ansible best practices

Selivanov Pavel
  • 2,206
  • 3
  • 26
  • 48