0% found this document useful (0 votes)
10 views9 pages

ANSIBLE SCRIPTS From FEB Batch

Uploaded by

Aj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

ANSIBLE SCRIPTS From FEB Batch

Uploaded by

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

ANSIBLE SCRIPTS

root@ubuntu:/etc/ansible# nano hosts


[AUTOMATION-SWITCHES]
192.168.32.200 ansible_ssh_user=admin ansible_ssh_pass=cisco

ANSIBLE PLAYBOOK#1

---
- hosts: AUTOMATION-SWITCHES
gather_facts: false
connection: local
gather_facts: network_cli

tasks:
- name: show run
ios_command:
commands:
- show ip int br

ANSIBLE PLAYBOOK#2

---
- name: Running show commands on Cisco IOS
hosts: AUTOMATION-SWITCHES
gather_facts: false
connection: network_cli

tasks:
- name: Run multiple commands on Cisco IOS nodes
ios_command: #ansible module
commands: #commands to run
- show version
- show ip interface brief

register: output #register the output in a variable named


output

- debug: var=output.stdout_lines #print the variable at the


console line by line

ANSIBLE PLAYBOOK#3
---
- hosts: AUTOMATION-SWITCHES
gather_facts: false #networking devices
connection: local #provider_cli

tasks:
- name: Backup Cisco-IOS
ios_config:
backup: yes #returned value function

ANSIBLE PLAYBOOK#4

---
- hosts: AUTOMATION-SWITCHES
gather_facts: true
connection: network_cli

tasks:
- name: show run
ios_command:
commands:
- show clock
- show ip int br
register: config #register is variable. to capture the output of
this first task

- name: save output to /etc/ansible/backups


copy:
content: "{{ config.stdout[0] }} \n {{ config.stdout[1] }}"
#{{ config.stdout_lines }} output is not good
dest:
"/etc/ansible/backups/show_run_{{ inventory_hostname }}_{{ansib
le_date_time.date}}.txt"

ANSIBLE PLAYBOOK#5

---
- name: Running show commands on Cisco IOS
hosts: 192.168.32.200
gather_facts: no
connection: local

vars: #playbook keyword (dictionary of variables)


login:
username: admin
password: cisco
auth_pass: cisco
authorize: true

tasks:
- name: Run multiple commands on Cisco IOS nodes
ios_command:
provider: "{{login}}"
commands:
- show run

register: print_output

- debug: var=print_output.stdout_lines
PART B

INSTALL ANSIBLE GALAXY:


ansible-galaxy collection install cisco.asa
pip3 install paramiko

Cat /etc/ansible/hosts
[ciscoasa]
ciscoasa01 ansible_host=192.168.32.189

[ciscoasa:vars]
ansible_connection=ansible.netcommon.network_cli
ansible_network_os=cisco.asa.asa
ansible_user=admin
ansible_password=cisco
ansible_become=true
ansible_become_method=ansible.netcommon.enable
ansible_become_password=cisco
ansible_python_interpreter=python

ANSIBLE PLAYBOOK#1

---
- name: all hosts
hosts: ciscoasa

tasks:
- name: Show the ASA version
cisco.asa.asa_command:
commands:
- show version
register: result123

- debug:
msg={{result123.stdout_lines}}

ANSIBLE PLAYBOOK#2

--
- name: all hosts
hosts: ciscoasa

tasks:
- name: Show the ASA version
cisco.asa.asa_command:
commands:
- show asp drop
register: result123

- debug:
msg={{result123.stdout_lines}}

ANSIBLE PLAYBOOK#3

---
- name: convert interface to structured data
hosts: ciscoasa
gather_facts: false
tasks:

- name: Gather facts


cisco.asa.asa_acls:
config:
state: gathered
register: gather

- name: Create inventory directory


become: true
delegate_to: localhost
file:
path: "{{ inventory_dir
}}/host_vars/{{ inventory_hostname }}"
state: directory

- name: Write each resource to a file


become: true
delegate_to: localhost
copy:
content: "{{ gather['gathered'] | to_nice_yaml }}"
dest: "{{ inventory_dir
}}/host_vars/{{ inventory_hostname }}/acls.yaml"

PART C

ANSIBLE JINJA SCRIPT#1: Vlan.j2

---
- hosts: [AUTOMATION-SWITCHES]
gather_facts: no
connection: network_cli
become: yes
become_method: enable
vars_files: #definaing variable file having datas
- vlans.json

tasks:
- name: Configure New Vlans using vlans.j2 and
vlans.json/vlans.yml
ios_config:
backup: yes
src: vlans.j2
match: none

vlans.json
{

"vlans":[

{ "vlanid": "501", "name": "CUST1-VRF" },

{ "vlanid": "502", "name": "CUST2-VRF"},

{ "vlanid": "548", "name": "CUST48-VRF"},

{ "vlanid": "549", "name": "CUST49-VRF"},

{ "vlanid": "550", "name": "CUST50-VRF"}

vlans.yml
---

vlans:

- vlanid: '501'

name: CUST1-VRF

- vlanid: '502'

name: CUST2-VRF

- vlanid: '548'
name: CUST48-VRF

- vlanid: '549'

name: CUST49-VRF

- vlanid: '550'

name: CUST50-VRF

vlans.j2
{% for vlan in vlans %}
vlan {{ vlan.vlanid }}
name {{ vlan.name }}
{% endfor %}}

Reference:
Online Converter: https://www.json2yaml.com/

ANSIBLE JINJA SCRIPT#2: INTERFACE.j2

---
- hosts: [AUTOMATION-SWITCHES]
gather_facts: no
connection: network_cli
vars_files:
- interfaces.json

tasks:
- name: configure new vlans
ios_config:
backup: yes
src: interfaces.j2
match: none

interfaces.json
{

"ip_interfaces":[

"interface":"GigabitEthernet3/0",

"description":"Created on Ansible Gi3/0",

"ip":"133.33.33.33",
"subnet":"255.255.255.0"

},

"interface":"GigabitEthernet3/2",

"description":"Created on Ansible Gi3/0",

"ip":"144.44.44.44",

"subnet":"255.255.255.0"

Interfaces.yml

---

ip_interfaces:

- interface: GigabitEthernet3/0

description: Created on Ansible Gi3/0

ip: 133.33.33.33

subnet: 255.255.255.0

- interface: GigabitEthernet3/2

description: Created on Ansible Gi3/0

ip: 144.44.44.44

subnet: 255.255.255.0

interfaces.j2

{% for item in ip_interfaces %}

interface {{ item.interface }}
description {{ item.description }}

no switchport

ip address {{ item.ip }} {{ item.subnet }}

no shutdown

exit

{% endfor %}

You might also like