How to execute task on subset of hosts - Ansible

Host file

[root@tiber ~]# cat hosts.yml
[nodes]
tiber
po
docker

I want to run task on 2ND & 3RD HOSTS only.

[root@tiber ~]# cat play.yml
- hosts: all
tasks:
- shell: "hostname"
when: inventory_hostname != play_hosts[0]
[root@tiber ~]# ansible-playbook -i hosts.yml play.yml
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [docker]
ok: [tiber]
ok: [po]

TASK: [shell hostname] ********************************************************
skipping: [tiber]
changed: [docker]
changed: [po]

PLAY RECAP ********************************************************************
docker : ok=2 changed=1 unreachable=0 failed=0
po : ok=2 changed=1 unreachable=0 failed=0
tiber : ok=1 changed=0 unreachable=0 failed=0

comments powered by Disqus