How to Play with particular group using Ansible Playbook
Asked Answered
P

3

9

Ansible version: 2.1.0

My ansible hosts file is:

[PM]
xyz.example.com ansible_connection=ssh

[ND]
pqr.example.com ansible_connection=ssh

[CM]
xyz.example.com ansible_connection=ssh
pqr.example.com ansible_connection=ssh

And playbook is:

- hosts: PM:ND:CM
   remote_user: root
   tasks:
    {some thing}

- hosts: PM
   remote_user: root
   tasks:
    {some thing}

 - hosts: ND
   remote_user: root
   tasks:
    {some thing}

- hosts: CM
   remote_user: root
   tasks:
    {some thing}

And I am running playbook with the following command:

ansible-playbook --limit 'PM' akana-installation.yml

But still the playbook is playing with all hosts, it means

Play 'PM:ND:CM'
Play 'PM'
Play 'ND'
Play 'CM'

those all plays are playing. Please help me to resolve this.

What I need is: While executing playbook I will give group name, that only group should play, so please let me know is there any other way.

Phyletic answered 12/7, 2016 at 9:18 Comment(0)
N
5

Original question was: --limit option is not working

By calling ansible-playbook --limit 'PM' akana-installation.yml you tell ansible to limit servers to the hosts that are in PM group.
In your case it will be xyz.example.com.
Keep in mind that if you have this server in several groups, as you do, it will still be a member of that groups.
Your limited inventory will become:

[PM]
xyz.example.com ansible_connection=ssh

[ND]

[CM]
xyz.example.com ansible_connection=ssh

And ansible-playbook will execute every play in your playbook that is applicable for xyz.example.com.
In your case:

Play 'PM:ND:CM'
[xyz.example.com]
Play 'PM'
[xyz.example.com]
Play 'ND'
skipping: no hosts matched
Play 'CM'
[xyz.example.com]
Nasalize answered 12/7, 2016 at 9:47 Comment(2)
Yes, you are right that's what happening to me. But I want to play with particular group hosts, how can I achieve this?Phyletic
@KishoreReddy, this is not how thing works in ansible. You can limit hosts (as you do with --limit switch), but it will not limit plays. If you want to limit plays, consider separating them to different playbooks and include them into the main playbook (see example: site.yml and webservers.yml). Or use tags.Nasalize
T
3

Create dir for playbook files:

mkdir playbooks

Split your playbooks into separate files, eg. playbooks/pm.yml:

- hosts: PM
  remote_user: root
  tasks:
  {some thing}

Create file all.yml

- hosts: PM:ND:CM
  remote_user: root
  tasks:
  {some thing}

- include: playbooks/pm.yml
- include: playbooks/nd.yml
- include: playbooks/cm.yml    

Now You have separate logic and You can play all with command:

ansible-playbook all.yml

or run separate command:

ansible-playbook playbooks/pm.yml

or

ansible-playbook playbooks/nd.yml
Tremml answered 11/8, 2016 at 7:21 Comment(0)
T
0

This is to Add on to the previous answer. And I apologize if I misunderstood your question, but your example playbook, had no descriptions.

Cleaning up skipped hosts:
Group 'ND' will (as stated above) not match any hosts. And if you want to remove the output from the play, you would have to use a Callback plugin. I use the following Gist:

Another way to limit:
To limit in the way you are asking to, you will need to use TAGS. This will allow you to limit by play (which is what you seem to want to do) instead of by host lists.

To do this, add a tag to the play:

...
- hosts: PM
  remote_user: root
  tags: pmtag
  tasks:
    {some thing}

Then run the playbook with the new tag excluded or included depending on your needs.

ansible-playbook --limit 'PM' akana-installation.yml --tags pmtag

This will ensure that only hosts in PM run only plays tagged with pmtag.

Thinskinned answered 18/7, 2016 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.