How to run ansible playbook on a single or multiple selected host(s) ?

Lets see how to run a playbook on a single selected host or multiple selected hosts.

We can either make groups in hosts file or mention the host name using “-l” option with ansible-playbook command.

1. Running an ansible playbook on a single host

$ ansible-playbook playbooks/thirdplay.yml  -l r005-host1.ngelinux.com

PLAY [Copying test.html file] ******************************************************************************************************************************************************************

TASK [copy] ************************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com]

PLAY [Verify Copied File] **********************************************************************************************************************************************************************

TASK [shell] ***********************************************************************************************************************************************************************************
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
    "msg": "total 8\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html\n-rw-r--r-- 1 root     root     25 Oct  5 06:16 test.txt"
}

PLAY RECAP *************************************************************************************************************************************************************************************
r005-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   
$ 

2. Run playbook on more than one host.

$ ansible-playbook playbooks/thirdplay.yml  -l 'r005-host1.ngelinux.com,r168-host1.ngelinux.com'

PLAY [Copying test.html file] ******************************************************************************************************************************************************************

TASK [copy] ************************************************************************************************************************************************************************************
ok: [r168-host1.ngelinux.com]
ok: [r005-host1.ngelinux.com]

PLAY [Verify Copied File] **********************************************************************************************************************************************************************

TASK [shell] ***********************************************************************************************************************************************************************************
changed: [r168-host1.ngelinux.com]
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
    "msg": "total 8\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html\n-rw-r--r-- 1 root     root     25 Oct  5 06:16 test.txt"
}
ok: [r168-host1.ngelinux.com] => {
    "msg": "total 4\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html"
}

PLAY RECAP *************************************************************************************************************************************************************************************
r005-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   
r168-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   
$

Leave a Reply

Your email address will not be published.