Lab 7.2 - Use Ansible To Back Up and Configure A Device
Lab 7.2 - Use Ansible To Back Up and Configure A Device
Lab 7.2 - Use Ansible To Back Up and Configure A Device
Background / Scenario
In this lab, you will explore the fundamentals of how to use Ansible to automate some basic device
management task. First, you will configure Ansible in your DEVASC VM. Next, you will use Ansible to connect
to the CSR1000v and back up its configuration. Finally, you will configure the CSR1000v with IPv6
addressing.
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
CSR1000v Virtual Machine
Instructions
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
lab you will run Ansible from the ansible-csr1000v directory. Therefore, you need separate hosts and
ansible.cfg files for each lab.
Note: The terms hosts file and inventory file are synonymous and will be used interchangeably throughout
the Ansible labs.
The Ansible inventory file defines the devices and groups of devices that are used by the Ansible playbook.
The file can be in one of many formats, including YAML and INI, depending on your Ansible environment. The
inventory file can list devices by IP address or fully qualified domain name (FQDN), and may include host
specific parameters as well.
a. Open the hosts file in the ansible-csr1000v directory.
b. Add the following lines to the hosts file and save.
# Enter the hosts or devices for Ansible playbooks
CSR1kv ansible_user=cisco ansible_password=cisco123! ansible_host=192.168.56.101
After the comment (#), the hosts file begins with an alias, CSR1kv. An alias is used from within the
Ansible playbook to reference a device. After the alias, the hosts file specifies three variables that will be
used by the Ansible playbook to access the device. These are the SSH credentials Ansible needs to
securely access the CSR1000v VM.
o ansible_user is a variable containing the username used to connect to the remote device. Without
this, the user that is running the ansible-playbook would be used.
o ansible_password is a variable containing the corresponding password for ansible_user. If not
specified, the SSH key would be used.
o ansible_host is a variable containing the IP address or FQDN of the device.
c. Use the ansible --version command to display version information. Notice that this lab is using version
2.9.6. Ansible includes certain default files, including a default configuration file, ansible.cfg.
devasc@labvm:~/labs/devnet-src/ansible$ ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devasc/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
[defaults]
#inventory = /etc/ansible/hosts
<output omitted>
# retry files
# When a playbook fails a .retry file can be created that will be placed in ~/
# You can enable this feature by setting retry_files_enabled to True
# and you can change the location of the files by setting retry_files_save_path
#retry_files_enabled = False
<output omitted>
Notice that Ansible shows that the inventory hosts file it will use by default is /etc/ansible/hosts. In a
previous step, you edited the inventory hosts file in the ansible-csr1000v directory. In the next step you will
edit a new ansible.cfg file which uses the hosts inventory file that you created.
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
In the next part, you will create a playbook to tell Ansible what to do.
tasks:
- name: DISPLAYING THE RUNNING-CONFIG
ios_command:
commands:
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
- show running-config
register: config
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
output (stdout) is the default file descriptor where a process can write output used in Unix-like
operating systems, such as Linux and Mac OS X.
o dest: "backups/show_run_{{ inventory_hostname }}.txt" - This is the path and file
name to where the file should be copied. The inventory_hostname variable is an Ansible "magic
variable" that automatically receives the hostname as configured in the hosts file. In your case,
recall that this is CSR1kv. This parameter results in a file show_run_CSR1kv.txt stored in the
backups directory. The file will contain the output of the show running-config command. You
will create the backups directory in the next step.
devasc@labvm:~/labs/devnet-src/ansible$/ansible-csr1000v$
Note: In many examples you will see the playbook run using the -i inventory-filename option. For
example:
devasc@labvm:~/labs/devnet-src/ansible/ansible-csr1000v $ ansible-playbook
backup_cisco_router_playbook.yaml -i hosts
This option tells Ansible the location and name of the inventory file, the list of devices the playbook will
use. This option is not necessary because you configured the inventory file name and location in your
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
local ansible.cfg file: inventory=./hosts. You can use the -i inventory-filename option to override the
information in the ansible.cfg file.
The PLAY RECAP should display ok=2 changed=1 indicating a successful playbook execution.
If your Ansible playbook fails, some of the things to check in your playbook are:
o Make sure your hosts and ansible.cfg files are correct.
o Make sure the YAML indentation is correct.
o Make sure your IOS command is correct.
o Check all the Ansible playbook syntax.
o Verify you can ping the CSR1000v.
If you continue to have problems, try typing one line at a time and running the playbook each time.
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
tasks:
- name: SET IPv6 ADDRESS
ios_config:
parents: "interface GigabitEthernet1"
lines:
- description IPv6 ADDRESS
- ipv6 address 2001:DB8:ACAD:1::1/64
- ipv6 address FE80::1:1 link-local
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
The last task saves the information in the register output to a file IPv6_output_CSR1kv.txt in the
ios_configurations subdirectory.
Step 4: Run the Ansible playbook to configure IPv6 addressing on the CSR1000v VM.
a. In Part 1, you started the CSR1000v VM. Ping it to verify you can access it. Enter Ctrl+ C to abort the
ping.
devasc@labvm:~/labs/devnet-src/ansible/ansible-csr1000v$ ping 192.168.56.101
PING 192.168.56.101 (192.168.56.101) 56(84) bytes of data.
64 bytes from 192.168.56.101: icmp_seq=1 ttl=63 time=0.913 ms
64 bytes from 192.168.56.101: icmp_seq=2 ttl=63 time=0.875 ms
^C
--- 192.168.56.101 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.875/0.894/0.913/0.019 ms
devasc@labvm:~/labs/devnet-src/ansible/ansible-csr1000v$
b. Create the directory ios_configurations. As indicated in the last line of your playbook, this is the
directory where the output for the show ipv6 interface brief command will be stored.
devasc@labvm:~/labs/devnet-src/ansible/ansible-csr1000v$ mkdir
ios_configurations
c. Now you can run the Ansible playbook using the ansible-playbook command. The -v verbose option can
be used to display the tasks being performed in the playbook.
devasc@labvm:~/labs/devnet-src/ansible/ansible-csr1000v$ ansible-playbook -v
cisco_router_ipv6_config_playbook.yaml
Using /home/devasc/labs/ansible-csr1000v/ansible.cfg as config file
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 11 www.netacad.com
Lab 7.2 - Use Ansible to Back Up and Configure a Device
The first time you run the playbook, the PLAY RECAP should display ok=3 changed=2 and failed=0
indicating a successful execution. These values may be different if you run the playbook again.
2020 - 2022 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 11 www.netacad.com