Bookmark this page

Guided Exercise: Configuring the Host Name

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.

  1. 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'" leaf02
    leaf02 | SUCCESS => {
        "changed": false,
        "stdout": [
            "vyos"
        ],
        "stdout_lines": [
            [
                "vyos"
            ]
        ]
    }
  2. 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 }}" leaf02
    leaf02 | SUCCESS => {
        "changed": true,
        "commands": [
            "set system host-name 'leaf02'"
        ]
    }
  3. 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'" leaf02
    leaf02 | SUCCESS => {
        "changed": false,
        "stdout": [
            "leaf02"
        ],
        "stdout_lines": [
            [
                "leaf02"
            ]
        ]
    }
  4. 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" leaf02
    leaf02 | SUCCESS => {
        "changed": true,
        "commands": [
            "set system host-name 'vyos'"
        ]
    }
  5. 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'
  6. Perform the play in the playbook you created.

    [student@workstation proj]$ ansible-playbook -l leaf02 \
    > multi-vendor-set-hostname1.yml
  7. 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
  8. 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" leaf02
    leaf02 | SUCCESS => {
        "changed": true,
        "commands": [
            "set system host-name 'vyos'"
        ]
    }

This concludes the guided exercise.

Revision: do457-2.5-4693601