In this exercise, you will configure the host name for network devices.
Outcomes
You should be able to:
View the host name using an ad hoc command.
Configure the host name using an ad hoc command.
Configure the host name with a simple playbook.
Configure the host name using a multivendor playbook.
Open a terminal window on the workstation VM and change to the ~/proj/ directory.
Execute an ad hoc command that displays the host name of VyOS device leaf02.
[student@workstation proj]$ansible -m vyos_command \>-a "commands='sh host name'" leaf02leaf02 | SUCCESS => { "changed": false, "stdout": [ "vyos" ], "stdout_lines": [ [ "vyos" ] ] }
Execute an ad hoc command that uses the vyos_system module to set the host name for leaf02.
[student@workstation proj]$ansible -m vyos_system \>-a "host_name={{ inventory_hostname }}" leaf02leaf02 | SUCCESS => { "changed": true, "commands": [ "set system host-name 'leaf02'" ] }
Execute an ad hoc command to verify that the host name was changed as expected.
[student@workstation proj]$ansible -m vyos_command \>-a "commands='sh host name'" leaf02leaf02 | SUCCESS => { "changed": false, "stdout": [ "leaf02" ], "stdout_lines": [ [ "leaf02" ] ] }
Repeat the ad hoc command that sets the host name, but this time explicitly set it back to its original value.
[student@workstation proj]$ansible -m vyos_system \>-a "host_name=vyos" leaf02leaf02 | SUCCESS => { "changed": true, "commands": [ "set system host-name 'vyos'" ] }
Create a playbook named multi-vendor-set-hostname1.yml that sets the host name to inventory_hostname.
The value of hosts should default to network, and the when conditional should be used in order to invoke either vyos_system or ios_system.
[student@workstation proj]$cat multi-vendor-set-hostname1.yml--- - name: sets hostname in a multi-vendor way hosts: network tasks: - name: set hostname on vyos device vyos_system: host_name: "{{ inventory_hostname }}" when: ansible_network_os == 'vyos' - name: set hostname on ios device ios_system: hostname: "{{ inventory_hostname }}" when: ansible_network_os == 'ios'
Perform the play in the playbook you created.
[student@workstation proj]$ansible-playbook -l leaf02 \>multi-vendor-set-hostname1.yml
Execute an ad hoc command to confirm that the play did in fact succeed in changing the host name.
[student@workstation proj]$ansible -m vyos_command \>-a "commands='sh host name'" leaf02
Execute an ad hoc command to return leaf02's host name to its original value.
[student@workstation proj]$ansible -m vyos_system -a "host_name=vyos" leaf02leaf02 | SUCCESS => { "changed": true, "commands": [ "set system host-name 'vyos'" ] }
This concludes the guided exercise.