Skip to content

Commit 12f0c54

Browse files
authored
Create NetworkPlaybookExample
1 parent 41f44e8 commit 12f0c54

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

Ansible/NetworkPlaybookExample

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
https://docs.ansible.com/ansible/latest/network/index.html
2+
3+
Hostfile:
4+
5+
cat /etc/ansible/inventory/host-file
6+
7+
[routers]
8+
router-1 ansible_host=192.168.1.57
9+
router-2 ansible_host=192.168.1.58
10+
11+
and Variables
12+
13+
cat /etc/ansible/inventory/host-file
14+
15+
[routers]
16+
router-1 ansible_host=192.168.1.57
17+
router-2 ansible_host=192.168.1.58
18+
19+
[routers:vars]
20+
ansible_network_os=ios
21+
ansible_user=ansible
22+
ansible_password=cisco123
23+
24+
ansible_network_os- Informs Ansible which Network platform this hosts corresponds to.
25+
ansible_user - The user to connect to the remote device
26+
ansible_password - The password for the user.
27+
28+
29+
30+
31+
Playbook which runs 'show version | incl Version' command on both routers and show us the output.
32+
33+
34+
35+
---
36+
37+
- name: Cisco show version example
38+
hosts: routers
39+
gather_facts: false
40+
connection: network_cli
41+
42+
tasks:
43+
- name: run show version on the routers
44+
ios_command:
45+
commands: show version | incl Version
46+
register: output
47+
48+
- name: print output
49+
debug:
50+
var: output.stdout_lines
51+
52+
53+
YAML file starts with ---
54+
name - Any arbitrary name
55+
hosts - Referring to the inventory group called 'routers'
56+
gather_facts - We don't need to gather any information from the routers. This may be useful when working with servers.
57+
connection - Playbook is run against a network device.
58+
register - You can create variables from the output of an Ansible task with the task keyword register. You can use registered variables in any later tasks in your play
59+
debug - This module prints statements during execution.
60+
stdout_lines - Ansible will print the output in an easy to readable format.
61+
62+
We are running two tasks, first one runs show version | incl Version on both routers and saves the output in a variable called output.
63+
The Second task prints the variable ouput in a nice format.
64+
65+
66+
67+
Run multiple 'show commands' at once
68+
You can run multiple show commands within the same task.
69+
70+
71+
- name: Cisco ip interface brief + ip route
72+
hosts: routers
73+
gather_facts: false
74+
connection: network_cli
75+
76+
tasks:
77+
- name: run show ip interface brief + ip route
78+
ios_command:
79+
commands:
80+
- show ip interface brief
81+
- show ip route
82+
register: output
83+
84+
- name: print output
85+
debug:
86+
var: output.stdout_lines
87+
88+
89+
Ansible Network Examples
90+
91+
https://docs.ansible.com/ansible/latest/network/user_guide/network_best_practices_2.5.html

0 commit comments

Comments
 (0)