Bookmark this page

Lab: Deploying Ansible

In this lab, you will create a ~/proj/ directory containing an ansible.cfg file, a static inventory file, and a group_vars/ directory. In the group_vars/ directory, create group files that set values for variables that make it possible to connect and authenticate automatically to devices.

Outcomes

You should be able to create a simple Ansible project directory that contains a static hosts inventory, an Ansible configuration file, and a group_vars/ directory with files that sets variables needed to automate the connection and authentication process.

Open a terminal window on the workstation VM.

Table 1.4. Lab Network Managed Resources

HostOS/NOSCredentials
spine01VyOSuser: vyos, password: vyos
spine02VyOSuser: vyos, password: vyos
leaf01VyOSuser: vyos, password: vyos
leaf02VyOSuser: vyos, password: vyos
cs01IOSuser: admin, password: student

Procedure 1.1. Instructions

  1. Create and change into the ~/proj/ project directory.

    [student@workstation ~]$ mkdir ~/proj
    [student@workstation ~]$ cd ~/proj
  2. Create an Ansible hosts inventory file suitable for managing devices shown in the Lab Network Managed Resources table.

    Use your favorite text editor to create the inventory file. Ensure it has contents similar to the following:

    [leafs]
    leaf[01:02]
    
    [spines]
    spine[01:02]
    
    [cloud-services]
    cs01
    
    [ios:children]
    cloud-services
    
    [vyos:children]
    spines
    leafs
    
    [network:children]
    vyos
    ios
    
    [servers]
    server[01:03]
  3. Verify the inventory.

    [student@workstation proj]$ ansible-inventory -i inventory --graph
    @all:
       |--@network:
       |  |--@ios:
       |  |  |--@cloud-services:
       |  |  |  |--cs01
       |  |--@vyos:
       |  |  |--@leafs:
       |  |  |  |--leaf01
       |  |  |  |--leaf02
       |  |  |--@spines:
       |  |  |  |--spine01
       |  |  |  |--spine02
       |--@servers:
       |  |--server01
       |  |--server02
       |  |--server03
       |--@ungrouped:
  4. Create a local ansible.cfg file that contains your customizations.

    Use your favorite text editor to create an ansible.cfg file with contents similar to the following:

    [defaults]
    host_key_checking = False
    inventory = inventory
    ask_pass = True
    gathering = explicit
    
    [persistent_connection]
    # avoid timing out when configuring slow 102 VM
    command_timeout = 180
    connect_timeout = 100
    connect_rety_timeout = 100
  5. Create a ~/proj/group_vars directory to contain further customizations.

    [student@workstation proj]$ mkdir ~/proj/group_vars
  6. Populate the group_vars/ directory with group files that set the connection method and authentication details (ansible_user).

    Use your favorite text editor to create group_vars files for the network, vyos, and ios host groups. Create them with the following contents:

    [student@workstation proj]$ cat group_vars/network
    ansible_connection: network_cli
    [student@workstation proj]$ cat group_vars/vyos
    ansible_network_os: vyos
    ansible_user: vyos
    [student@workstation proj]$ cat group_vars/ios
    ansible_network_os: ios
    ansible_user: admin
  7. Check your work.

    [student@workstation proj]$ ansible -m debug -a "var=ansible_user" vyos
    SSH password: vyos
    leaf01 | SUCCESS -> {
        "ansible_user": "vyos"
    }
    leaf02 | SUCCESS -> {
        "ansible_user": "vyos"
    }
    spine01 | SUCCESS -> {
        "ansible_user": "vyos"
    }
    spine02 | SUCCESS -> {
        "ansible_user": "vyos"
    }
    
    [student@workstation proj]$ ansible -m ping vyos
    SSH password: vyos
    leaf01 | SUCCESS -> {
        "changed": "false"
        "ping": "pong"
    }
    leaf02 | SUCCESS -> {
        "changed": "false"
        "ping": "pong"
    }
    spine01 | SUCCESS -> {
        "changed": "false"
        "ping": "pong"
    }
    spine02 | SUCCESS -> {
        "changed": "false"
        "ping": "pong"
    }

    A hosts inventory, configuration file, and group variables supporting this particular inventory form a collection that provides an operational foundation for future automation projects. This would be a good time to commit and push it to a Git repository.

This concludes the lab.

Revision: do457-2.5-4693601