How to Play with particular group using Ansible Playbook

KishoreReddy picture KishoreReddy · Jul 12, 2016 · Viewed 10.6k times · Source

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.

Answer

Konstantin Suvorov picture Konstantin Suvorov · Jul 12, 2016

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 asma.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]