In this lab, you will review the process of composing and executing ad hoc commands and plays.
Outcomes
You should be able to:
Compose and execute an ad hoc command that displays the results you would see if you typed commands interactively on the console of a network device.
For VyOS devices, display the results of the show interfaces command.
For IOS devices, display the results of the show ip interfaces brief command.
Create a playbook that does the same thing as the ad hoc commands you just executed.
Perform the play or plays in the playbook you created.
Compose a playbook named ios-vyos-assert-domain.yml that asserts that the domain name is currently set to "lab.example.com."
Perform the play you created that asserts the value of the domain name.
Open a terminal window on the workstation VM, and change into a directory that provides files that support Ansible connectivity to network devices in the Lab Network (that is, the ability to perform tasks on remote devices there).
Instructions
Perform the following steps:
Choose a module that provides the ability to run commands remotely on network devices.
Figure out the correct arguments and syntax to use in order to execute the CLI commands as Ansible ad hoc commands using the appropriate module.
Execute the ad hoc commands.
Compose a playbook that accomplishes the same thing.
Perform the play or plays in the playbook you created to display interface information.
Compose a playbook named ios-vyos-assert-domain.yml that asserts that the domain name is currently set to "lab.example.com."
At the command line of a VyOS device, you could type "sh conf comm | grep domain-name" to show how the device is currently configured with respect to domain name.
At the command line of an IOS device, you could type "sh run | include domain."
Perform the play you created that asserts the value of the domain name.
Choose a module that provides the ability to run commands remotely on network devices.
On VyOS devices, the module to use is vyos_command.
On IOS devices, the module to use is ios_command.
Figure out the correct arguments and syntax to use in order to execute the CLI commands as Ansible ad hoc commands using the appropriate module.
For VyOS devices this is the command:
ansible -m vyos_command -a"commands='sh int'" vyos
For IOS devices this is the command:
ansible -m ios_command -a"commands='sh ip int br'" ios
Execute the ad hoc commands
For VyOS devices:
[student@workstation ansible-generic-project]$ansible -m vyos_command \>-a "commands='sh int'" vyos
For IOS devices:
[student@workstation ansible-generic-project]$ansible -m ios_command \>-a "commands='sh ip int br'" ios
Compose a playbook that accomplishes the same thing.
[student@workstation ansible-generic-project]$cat ios-vyos-sh-br-int.yml--- - name: multi-vendor play that shows interfaces hosts: network vars: ios_command: sh ip int br vyos_command: sh int tasks: - name: "{{ ios_command }} on IOS device {{ inventory_hostname }}" ios_command: commands: - "{{ ios_command }}" register: ios_result when: ansible_network_os == 'ios' - name: show IOS result debug: var: ios_result when: ansible_network_os == 'ios' - name: "{{ vyos_command }} on IOS device {{ inventory_hostname }}" vyos_command: commands: - "{{ vyos_command }}" register: vyos_result when: ansible_network_os == 'vyos' - name: show VyOS result debug: var: vyos_result when: ansible_network_os == 'vyos'
Perform the play or plays in the playbook you created.
[student@workstation ansible-generic-project]$ansible-playbook \>ios-vyos-sh-br-int.yml