Search code examples
ansibleansible-2.xansible-inventoryansible-facts

run a task on limited number of hosts in a group using ansible


Team,

basically, goal is to run a task only on N nodes from 1-10 in a hosts group. trying to see what is the best feasible way to achieve this.. any insights?

here is my try with with hosts parameter but task is getting executed on all hosts.

I have 10 hosts in a group hosts_gpu but I want to run task only on 1-3.

ansible-playbook -i hosts_gpu[1:3] test.yml

output:

all hosts affected

expected

host1
host2
host3

Solution

  • As @Mamun has already mention you could use --limit (or -l) option to limit playbook execution to specific hosts.

    ansible-playbook -i inventory_file playbook.yml --limit "host1:host2:host3"
    

    If hosts_gpu is a group then you could limit playbook by executing

    ansible-playbook -i inventory_file playbook.yml --limit "hosts_gpu"
    

    Or you could limit execution to the first three hosts in the group using Python list slicing notation.

    ansible-playbook -i inventory_file playbook.yml --limit "hosts_gpu[0:3]"