In this exercise, you will view system settings for network devices.
Outcomes
You should be able to:
View individual system settings using ad hoc commands.
View multiple system settings with a simple playbook.
View multiple system settings using a multivendor playbook.
Open a terminal window on the workstation VM and change to the ~/proj/ directory.
Use individual ad hoc commands to view the host name, domain, and name servers for spine01, which is a VyOS device.
[student@workstation proj]$ansible -m vyos_command \>-a "commands='sh host name'" spine01[student@workstation proj]$ansible -m vyos_command \>-a "commands='sh host domain'" spine01[student@workstation proj]$ansible -m vyos_command \>-a "commands='sh dns forwarding nameservers'" spine01
Use individual ad hoc commands to view the host name, domain, and name servers for cs01, which is an IOS device.
[student@workstation proj]$ansible -m ios_command \>-a "commands='sh run | include hostname'" cs01[student@workstation proj]$ansible -m ios_command \>-a "commands='sh run | include ip domain name'" cs01[student@workstation proj]$ansible -m ios_command \>-a "commands='sh run | include ip name-server'" cs01
Compose a playbook named host-dnsinfo1.yml that displays the host name, domain name, and name servers for cs01.
[student@workstation proj]$cat host-dnsinfo1.yml--- - name: display host name, domain name, nameservers for ios devices hosts: ios tasks: - name: run the commands ios_command: commands: - sh run | include hostname - sh run | include ip domain name - sh run | include ip name-server register: result - debug: var: result.stdout
Alternatively, you could define a variable of type sequence that contains the distinctive portion of the show run | include commands, and loop over that.
[student@workstation proj]$cat host-dnsinfo1.yml--- - name: display host name, domain name, nameservers for ios devices hosts: ios vars: includes: - hostname - domain name - name-server tasks: - name: run the commands ios_command: commands: - sh run | include {{ item }} loop: "{{ includes }}" register: result - debug: var: item.stdout loop: "{{ result.results }}"
Perform the play in the playbook you just created.
[student@workstation proj]$ansible-playbook host-dnsinfo1.ymlPLAY [display host name, domain name, nameservers for ios devices] ************* TASK [run the commands] ******************************************************** ok: [cs01] TASK [debug] ******************************************************************* ok: [cs01] => { "result.stdout": [ "hostname cs01", "ip domain name lab.example.com", "" ] } PLAY RECAP ********************************************************************* cs01 : ok=2 changed=0 unreachable=0 failed=0
Compose a playbook named multi-vendor-host-dnsinfo1.yml that displays the host name, domain name, and name servers for both VyOS and IOS devices, and which defaults to hosts group network, and can target an individual host when the ansible-playbook command is used to set the variable target with the -e option.
You can copy the playbook from step 3 to save typing.
[student@workstation proj]$cat multi-vendor-host-dnsinfo1.yml--- - name: multi-vendor play to display host name, domain name, nameservers hosts: network tasks: - name: ios host, domain, nameserver ios_command: commands: - sh run | include hostname - sh run | include ip domain name - sh run | include ip name-server register: result when: ansible_network_os == 'ios' - name: display ios host settings debug: var: result.stdout when: ansible_network_os == 'ios' - name: vyos host, domain, nameserver vyos_command: commands: - sh host name - sh host domain - sh config | grep name-server register: result when: ansible_network_os == 'vyos' - name: display vyos host settings debug: var: result.stdout when: ansible_network_os == 'vyos'
Note that the same alternative approach with loops can also be employed here.
Perform the play contained in your new playbook.
First use the limit option (-l SUBSET) to specify cs01, then run it in default mode.
[student@workstation proj]$ansible-playbook -l cs01 \>multi-vendor-host-dnsinfo1.ymlPLAY [multi-vendor play to display host name, domain name, nameservers] ******** TASK [ios host, domain, nameserver] ******************************************** ok: [cs01] TASK [display ios host settings] *********************************************** ok: [cs01] => { "result.stdout": [ "hostname cs01", "ip domain name lab.example.com", "" ] } TASK [vyos host, domain, nameserver] ******************************************* skipping: [cs01] TASK [display vyos host settings] ********************************************** skipping: [cs01] PLAY RECAP ********************************************************************* cs01 : ok=2 changed=0 unreachable=0 failed=0[student@workstation proj]$ansible-playbook multi-vendor-host-dnsinfo1.yml
This concludes the guided exercise.