Bootstrap FreeKB - Ansible - Run tasks against certain managed nodes using the --limit option
Ansible - Run tasks against certain managed nodes using the --limit option

Updated:   |  Ansible articles

Tasks are run against target servers. Some Ansible documentation refers to the target servers as "hosts".


There are a few ways to run an ansible ad hoc command or the ansible-playbook command against certain target servers. 


After a clean install of Ansible, the "inventory" directive in ansible.cfg is commented out, like this.

#inventory = /path/to/hosts


In this scenario, the default hosts file is /etc/ansible/hosts and the default hosts file is completely commented out. If you were to issue command ansible all -m ping, the following would be displayed. Likewise, if you were to uncomment the "inventory" directive in ansible.cfg without defining your inventory, the following would be displayed.

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


Typically, target servers are defined in the default hosts file or your own inventory file. Sometimes, the "inventory" directive in ansible.cfg is uncommented and updated to point to the directory where the default hosts file or your own inventory file will be located.

Additionally, Ansible uses inventory plugins to parse inventory. For example, ansible.cfg may have the following.

enable_plugins = host_list, yaml, ini


For example, the yaml inventory_plugin allows you to define target servers in a YAML default hosts file or your own inventory file. For example, let's say you have a default hosts file or your own inventory file named inventory.yml that contains target systems, perhaps something like this.



Here is how you can run the example.yml playbook using the target systems specified in inventory.yml.

ansible-playbook example.yml --inventory /path/to/inventory.yml


In this example, --limit is used so that the ansible ad hoc command would only be run against server1.


Unlike the -i or --inventory command line option, when using --limit, the target server must exist in your default hosts file or your own inventory file.

The --limit command line option is CaSe SenSiTiVe, meaning the target server hostname must be an exact match of the hostname in your default hosts file or your own inventory file.

Optionally, you can define host aliases in your default hosts file or your own inventory file, such as server1 and SERVER1 for, if you would like.

ansible all --module-name ping --limit


And here is a similar example using the ansible-playbook command.

ansible-playbook foo.yml --limit


Or, you can have a comma separated list of managed nodes.

ansible-playbook foo.yml --limit,


Or, the limit option can be used to run a play against a group of managed hosts in your inventory. In this example, the playbook will be run against the "linux" hosts in default hosts file or your own inventory file.

ansible-playbook foo.yml --limit linux


Two (or more) groups can be used. In this example, the playbook will be run against the "linux" and "windows" hosts in default hosts file or your own inventory file.

ansible-playbook foo.yml --limit "linux windows"


Or, you could create a file that contains some of the managed nodes. Let's host limit.txt contains the following.


Here is how you would run the playbook against the hosts defined in limit.txt. This is commonly used with the .retry file.

ansible-playbook foo.yml --limit @limit.txt


play_hosts can be used to store the server in a variable.

If you are running version 2.5 or higher of Ansible, fail can be used to fail a play when the -l or --limit command line option was not used, meaning the ansible_limit magic variable is not defined, like this.

- debug:
    var: ansible_limit

- name: fail when the -l or --limit option is not used on the command line
   msg: the -l or --limit option was not used on the command line
  when: ansible_limit is undefined


The following magic variables can be used to output the managed hosts the play is being run against:

Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 02e77e in the box below so that we can be sure you are a human.