In this exercise, you will resolve names by way of a dynamic inventory script when executing ad hoc commands and running plays.
Outcomes
You should be able to:
Use the ansible-inventory command to view the contents provided by a dynamic inventory script.
Execute an ad hoc command using a dynamic inventory script.
Use the ansible-playbook command with a dynamic inventory script to run a play or plays in a playbook.
Rename the dynamic inventory script and use an ad hoc command to illustrate that the name is not significant.
Remove the execute bit from the dynamic inventory script and attempt to run an ad hoc command to illustrate that the script must be executable.
Open a terminal window on the workstation VM and change to the proj/ directory.
Use the ansible-inventory command to view the contents provided by a dynamic inventory script.
Download the dynamic inventory simulation script dyninv.py and the required data.yml data file.
[student@workstation proj]$wget \>http://materials.example.com/playbooks/data.yml[student@workstation proj]$wget \>http://materials.example.com/playbooks/dyninv.py
Make the script executable.
[student@workstation proj]$chmod +x dyninv.py
Use the ansible-inventory command to view the contents provided by a dynamic inventory script.
[student@workstation proj]$ansible-inventory -i dyninv.py --list
This demonstrates that the dynamic inventory script is working; host inventory information is being provided by the script instead of a static inventory file.
Execute an ad hoc command using a dynamic inventory script.
It should list interfaces on the VyOS device named spine01.
[student@workstation proj]$ansible -i dyninv.py -m vyos_command \>-a "commands='sh int'" spine01
Use the ansible-playbook command with a dynamic inventory script to run a play or plays in a playbook.
Write a playbook named vyos-dyn-facts1.yml that gathers facts from devices running VyOS.
Ensure it contains the following:
---
- name: gather facts for vyos devices
hosts: vyos
tasks:
- name: invoke vyos_facts with gather_subset=all
vyos_facts:
gather_subset: all
when: ansible_network_os == 'vyos'
- debug:
var: ansible_facts
Use ansible-playbook to run the playbook.
Specify the dynamic inventory script dyninv.py as the host inventory with the -i option.
Limit it to just INVENTORYspine01.
[student@workstation proj]$ansible-playbook -i dyninv.py -l spine01 \>vyos-dyn-facts1.yml
Rename the dynamic inventory script and execute an ad hoc command to illustrate that the name is not significant.
Rename the script.
[student@workstation proj]$mv dyninv.py myinv
Execute an ad hoc command referencing the dynamic inventory script that lists the interfaces on the VyOS device named spine01.
[student@workstation proj]$ansible -i myinv -m vyos_command \>-a "commands='sh int'" spine01
This illustrates that the name of the script is not significant, as long as it is a valid file name.
We see the _meta block, groups and hosts, and so forth.
Remove the execute bit from the dynamic inventory script and attempt to execute an ad hoc command to illustrate that the script must be executable.
Use the chmod command to remove the execute bit from the dynamic inventory script.
[student@workstation proj]$chmod -x myinv
Execute the same ad hoc command that was successful earlier with the same dynamic inventory script when it was executable.
[student@workstation proj]$ansible -i myinv -m vyos_command \>-a "commands='sh int'" spine01[WARNING]: * Failed to parse /home/student/proj/myinv with script plugin:problem running /home/student/proj/myinv --list ([Errno 13] Permission denied) ...output omitted... [WARNING]: Could not match supplied host pattern, ignoring: spine01
This illustrates that the dynamic inventory script must be executable.
This concludes the guided exercise.