There are a few ways to run an ansible ad hoc command or the ansible-playbook command against certain managed nodes.
Hosts parameter
In this example, the playbook has hosts: all, meaning the playbook would be run against every managed host (e.g. target systems) in your default hosts file or your own inventory file. Refer to Ansible - Inventory groups (all | ungrouped | other).
---
- hosts: all
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
Let's say your default hosts file or inventory file contains the following. In this scenario, when using hosts: all, the playbook would be run against server1, server2, server3 and server4.
If you were to instead use hosts: windows, the playbook would only be run against server1 and server2. Or, if hosts: linux were used, the playbook would only be run against server3 and server4.
all:
children:
windows:
hosts:
server1.example.com:
server2.example.com:
linux:
hosts:
server3.example.com:
server4.example.com:
Let's say you want to run the playbook against only the first host. In this scenario you can use - hosts: all[0] to target only the first host.
---
- hosts: all[0]
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
when parameter
Let's say you have a playbook with two tasks, where the first task should be run against your "linux" managed nodes and the second task should be run against your "windows" managed nodes. The when parameter can be used.
---
- hosts: all
tasks:
- name: create /tmp/foo.txt on Linux
file:
path: /tmp/foo.txt
state: touch
when: inventory_hostname in groups['linux']
- name: create C:\Temp\bar.txt on Windows
file:
path: C:\Temp\bar.txt
state: touch
when: inventory_hostname in groups['windows']
...
-i command line option
In this example, the foo.yml playbook would only be run against server5. You must include the trailing comma.
The -i option can be used to run a playbook against a managed host that does not exist in default hosts file or inventory file.
ansible-playbook foo.yml -i server5.example.com,
Here is how to do the same using the ansible ad hoc command.
ansible -i server5.example.com, --module-name ping
--limit command line option
In this example, the ansible ad hoc command would only be run against server1.
AVOID TROUBLE
The --limit command line option is CaSe SenSiTiVe, meaning the 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 server1.example.com, if you would like.
ansible all --module-name ping --limit server1.example.com
In this example, the following command could be used so that the foo.yml playbook is only run on server1.example.com. Unlike the -i option, when using --limit, the managed host must exist in the default hosts or inventory file.
ansible-playbook foo.yml --limit server1.example.com
Or, you can have a comma separated list of managed nodes.
ansible-playbook foo.yml --limit server1.example.com,server2.example.com
Or, the limit option can be used to run a play against a group of managed hosts in your inventory, like this:
ansible-playbook foo.yml --limit linux
Or, you could create a file that contains some of the managed nodes. Let's host limit.txt contains the following.
server1.example.com
server2.example.com
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, the fail module can be used to fail a play when the -l or --limit command line option was not used, meaing the ansible_limit magic variable is not defined, like this.
- name: "fail when the -l or --limit option is not used on the command line'
fail:
msg: "the -l or --limit option was not used on the command line"
when: ansible_limit is not defined
The following magic variables can be used to output the managed hosts the play is being run against: