Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Play with particular group using Ansible Playbook

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.

like image 788
Kishore Reddy Avatar asked Jul 12 '16 09:07

Kishore Reddy


People also ask

What is an Ansible playbook?

A playbook is composed of one or more ‘plays’ in an ordered list. The terms ‘playbook’ and ‘play’ are sports analogies. Each play executes part of the overall goal of the playbook, running one or more tasks. Each task calls an Ansible module. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.

How to run Ansible on a single host?

Ansible completes the play on the specified number or percentage of hosts before starting the next batch of hosts. You can restrict the number of workers allotted to a block or task with throttle. You can control how Ansible selects the next host in a group to execute against with order. You can run a task on a single host with run_once.

What is the user and group module in Ansible?

In Ansible the user and group module helps us accomplish user management tasks. This playbook highlights various tasks. password – user’s encrypted password. Please note that the password should already been encrypted or use Ansible playbook encryption but hide the password in Ansible vault

What are Ansible keywords?

These keywords are not strategies. They are directives or options applied to a play, block, or task. By default, Ansible runs in parallel against all the hosts in the pattern you set in the hosts: field of each play.


Video Answer


2 Answers

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]
like image 170
Konstantin Suvorov Avatar answered Oct 27 '22 06:10

Konstantin Suvorov


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
like image 44
Piotr Dobrysiak Avatar answered Oct 27 '22 07:10

Piotr Dobrysiak