100% found this document useful (1 vote)
176 views8 pages

First Ansible Playbook For Beginners

This document provides instructions for completing a lab that introduces using Ansible to configure networking devices. The lab uses Ansible to configure IP addresses on router interfaces. It involves 5 tasks: 1) configure IP addresses and SSH keys on routers, 2) generate SSH sessions, 3) create Ansible inventory and variable files, 4) write a playbook to turn on interfaces and configure IP addresses, and 5) verify the configurations. The playbook uses Ansible modules to loop through interface variables and configure IP addresses on routers matching the variables.

Uploaded by

xmywayx5316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
176 views8 pages

First Ansible Playbook For Beginners

This document provides instructions for completing a lab that introduces using Ansible to configure networking devices. The lab uses Ansible to configure IP addresses on router interfaces. It involves 5 tasks: 1) configure IP addresses and SSH keys on routers, 2) generate SSH sessions, 3) create Ansible inventory and variable files, 4) write a playbook to turn on interfaces and configure IP addresses, and 5) verify the configurations. The playbook uses Ansible modules to loop through interface variables and configure IP addresses on routers matching the variables.

Uploaded by

xmywayx5316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Download PNETLab Platform

PNETLAB Store
PNETLab.com

First Ansible playbook for beginners

1. Lab Topology

2. Requirements

CCNA.

Linux-Ubuntu, Vim editor commands.

Understand Ansible overview.

SSH connect sessions.

3. Lab Objective

1
Download PNETLab Platform
PNETLAB Store
PNETLab.com

This lab started from basic step of using Ansible platform to connect routers in topology
using SSH sessions. Step by step to create playbook that push to routers.

4. Lab tasks

Task 1: Configure IP address for interfaces in routers that connect to Ansible


Workstation, enable domain name and SSH key.

 NOTE: don’t configure IP address on e0/0 interface in Routers (Blue color


link). You need to use Ansible to configure them.

Task 2: Generate SSH between Workstation and Routers.

Task 3: Create inventory that include input methods.

Task 4: Create playbook and push to Routers that configure IP address on interfaces.

Task 5: Check configuration.

5. Lab setup

This lab use Ansible platform version 2.9.6, you can get it on devices mode in
PNETLab GUI:

You should reading guide before download and use this platforms:
2
Download PNETLab Platform
PNETLAB Store
PNETLab.com

https://user.pnetlab.com/store/devices/guide?id=4

IP address on eth1 of Ansible workstation is configured 192.168.10.100/24

Password login Ansible Worstation: admin/admin

6. Solution

Task 1

Configure IP address for interfaces in routers that connect to Ansible Workstation,


enable domain name and SSH key.

 Configure IP address that you can see at topology on Routers:


 R1

!
ip domain-name local
crypto key generate rsa
1024
!
username ansi privilege 15 password 123
line vty 0 4
login local
transport input ssh
!
interface Ethernet0/1
ip address 192.168.10.10 255.255.255.0
no shutdown
!

 R2

!
ip domain-name local
crypto key generate rsa
1024
!
username ansi privilege 15 password 123
line vty 0 4
login local
transport input ssh
!
3
Download PNETLab Platform
PNETLAB Store
PNETLab.com

interface Ethernet0/2
ip address 192.168.10.20 255.255.255.0
no shutdown
!

 Make sure after these, Worstation can ping successful to routers.

Task 2

Generate SSH between Workstation and Routers.

ssh [email protected]
yes
---<123>---
exit

ssh [email protected]
yes
---<123>---
exit

4
Download PNETLab Platform
PNETLAB Store
PNETLab.com

Task 3

Create inventory that include input methods.

 Move to Ansible directory:


o cd /etc/ansible/
 By default in Ansible directory, you can see hosts file and ansible.cfg file
o ls –l
 hosts file that include input method such as: name-devices, ip-address…in this
lab are “devices => group”,“R1 R2 => host”:
o vim /etc/ansible/hosts
 edit like this (just skip all comments in this file, that’s guide for you):

[devices]
R1 ansible_host=192.168.10.10
R2 ansible_host=192.168.10.20

 create group_vars and host_vars where you will add input methods for groups or
hosts files yaml:
o mkdir /etc/ansible/group_vars
o touch /etc/ansible/group_vars/devices.yml
o mkdir /etc/ansible/host_vars
o touch /etc/ansible/host_vars/R1.yml
o touch /etc/ansible/host_vars/R2.yml
 Add input methods, first with devices group:
o vim /etc/ansible/group_vars/devices.yml

5
Download PNETLab Platform
PNETLAB Store
PNETLab.com

---
ansible_user: ansi
ansible_ssh_pass: 123
...

 Then with per router host:


o vim /etc/ansible/host_vars/R1.yml

interfaces:
- name: Ethernet0/0
description: Link to R2 configured by Ansible
ipv4: 200.100.1.1/24

o vim /etc/ansible/host_vars/R2.yml

interfaces:
- name: Ethernet0/0
description: Link to R1 configured by Ansible
ipv4: 200.100.1.2/24

 You can edit variables as you wish but choose types of them carefully, if not it will
be error. In this lab use ios_interfaces and ios_l3_interfaces module, so
variables need match with modules rules. More informations you can see in this
link : https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html

Task 4

Create playbook and push to Routers that configure IP address on interfaces.

 Playbook include these configuration steps:


o Connect to Routers
o Turn on interfaces
o Configure IP address on that interfaces
 Create playbook with above steps:
o vim /etc/ansible/running.yml

---
- name: This is the first playbook -> Devices Connecting...
hosts: devices
gather_facts: false
connection: network_cli

6
Download PNETLab Platform
PNETLAB Store
PNETLab.com

tasks:
- name: Turn on interface...
ios_interfaces:
config:
- name: "{{ item.name }}"
enabled: True
state: merged
loop: "{{ interfaces }}"

- name: Configure interface...


ios_l3_interfaces:
config:
- name: "{{ item.name }}"
ipv4:
- address: "{{ item.ipv4 }}"
state: merged
loop: "{{ interfaces }}"
...

 Call this playbook by command:


o ansible-playbook running.yml
 This’s result:

Task 5

Check configuration.

 On R1:
7
Download PNETLab Platform
PNETLAB Store
PNETLab.com

 On R2:

 That’s all for starting with Ansible, finish this lab or you can try other configs!

PASS UNLOCK LAB: lab123

You might also like