Write and run an Ansible Playbook.
Outcomes
Create and run a playbook to configure the login banners on Cisco devices running the IOS XE operating system.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that:
Directories are created for this exercise.
The podman, ansible-core, and ansible-navigator packages are installed.
An ansible.cfg file and an ansible-navigator.yml file are created for this exercise.
An inventory file is created for this exercise.
[student@workstation ~]$ lab start implement-playbooks
Instructions
Open Visual Studio Code (VS Code) on the workstation machine and open the ~/implement-playbooks directory.
Click on the workstation top bar, enter code in the field, and then press Enter.
In VS Code, click → . Click → and then click .
If prompted, then select and then click .
Use VS Code on the workstation machine to create a playbook named banner.yml.
Add a task to the playbook that configures the login banners for managed nodes in the ios inventory group.
In VS Code, click → .
Click and then click .
Create a playbook with the following contents, and save the file as banner.yml.
---
- name: Configure login banners
hosts: ios
gather_facts: false
tasks:
- name: Configure banner on IOS devices
cisco.ios.ios_banner:
banner: login
text: Managed by Ansible
state: present
notify: ios_save_changes
handlers:
- name: ios_save_changes
cisco.ios.ios_config:
save_when: alwaysOpen a terminal, change to the ~/implement-playbooks directory, and use the ansible-navigator command to run the banner.yml playbook.
[student@workstation ~]cd ~/implement-playbooks[student@workstation implement-playbooks]$ansible-navigator run banner.yml \ -m stdoutPLAY [Configure login banners] ************************************************* TASK [Configure banner on IOS devices] ***************************************** changed: [iosxe1.lab.example.com] changed: [iosxe2.lab.example.com] RUNNING HANDLER [ios_save_changes] ********************************************* changed: [iosxe1.lab.example.com] changed: [iosxe2.lab.example.com] PLAY RECAP ********************************************************************* iosxe1.lab.example.com : ok=2 changed=2 unreachable=0 failed=0 ... iosxe2.lab.example.com : ok=2 changed=2 unreachable=0 failed=0 ...
Verify that the login banner was correctly configured on the iosxe1.lab.example.com and iosxe2.lab.example.com managed nodes, and then change back to the /home/student directory.
Connect to the iosxe1.lab.example.com managed node by using SSH and verify the login banner.
Then log out from the iosxe1.lab.example.com managed node.
[student@workstation implement-playbooks]$ssh iosxe1.lab.example.comManaged by Ansible iosxe1.lab.example.com#exitConnection to iosxe1.lab.example.com closed by remote host. Connection to iosxe1.lab.example.com closed.
Connect to the iosxe2.lab.example.com managed node by using SSH and verify the login banner.
Then log out from the iosxe2.lab.example.com managed node.
[student@workstation implement-playbooks]$ssh iosxe2.lab.example.comManaged by Ansible iosxe2.lab.example.com#exitConnection to iosxe2.lab.example.com closed by remote host. Connection to iosxe2.lab.example.com closed.
Close the /home/student/implement-playbooks directory in VS Code.
If you are using the GNOME terminal, return to the /home/student directory.
Click → in VS Code to close the /home/student/implement-playbooks directory.
If you are using the GNOME terminal, run the cd command to return to the student home directory:
[student@workstation implement-playbooks]$ cd
[student@workstation ~]$