Example 1
hosts.yml
centos:
hosts:
provisioner:
ansible_connection: local
centos1:
ansible_host: '192.168.77.22'
ansible_port: 2345
ubuntu:
hosts:
ubuntu1:
ansible_host: '192.168.77.23'
Tip
The setup
module is at the core of Ansible. It gathers basic information about the remote hosts. By default it is always run at the beginning of each playbook. Check out the module's official documentation or run
ansible-doc setup
Execute the following command and look for the Linux distribution name on the remote host
ansible centos1 -m setup
What is the expected output of the command?
We expect the distribution to be set to CentOS
.
Bonus round
Filter out the name of network interface on the remote host where the default IPv4 address is located. In order to complete the task
- install
jq
on your systemsudo yum install -y jq
- parse the setup module's output to make it a valid
JSON
(you can pipe the result of the Ansible command tosed '1c {'
) - use
jq
to find the corresponding key-pair value in the output (take a look at this tutorial to get a basic idea on howjq
works). The key you are loooking for is underansible_facts
->ansible_default_ipv4
->interface
.