ANSIBLE SCRIPTS From FEB Batch
ANSIBLE SCRIPTS From FEB Batch
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
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
ANSIBLE PLAYBOOK#5
---
- name: Running show commands on Cisco IOS
hosts: 192.168.32.200
gather_facts: no
connection: local
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
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:
PART C
---
- 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":[
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/
---
- 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",
"ip":"133.33.33.33",
"subnet":"255.255.255.0"
},
"interface":"GigabitEthernet3/2",
"ip":"144.44.44.44",
"subnet":"255.255.255.0"
Interfaces.yml
---
ip_interfaces:
- interface: GigabitEthernet3/0
ip: 133.33.33.33
subnet: 255.255.255.0
- interface: GigabitEthernet3/2
ip: 144.44.44.44
subnet: 255.255.255.0
interfaces.j2
interface {{ item.interface }}
description {{ item.description }}
no switchport
no shutdown
exit
{% endfor %}