-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneshot.yml
146 lines (111 loc) · 4.17 KB
/
oneshot.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
- hosts: oneshot
gather_facts: false
connection: local
vars:
run_tests: "yes"
show_debug: "no"
filename: "commands.csv"
csvfile: "{{ lookup('file', '{{filename}}') }}"
scr: "./commands.j2"
dst: "./hosts"
cli:
username: "vagrant"
password: "vagrant"
host: "{{ ansible_ssh_host }}"
tests:
inventory: "show inventory | i SN"
version: "show version | i Version"
hostname: "show run | i hostname"
tasks:
# TODO:
# Make sure not to overwrite other config
# Currently just pushes the config no matter what
# Should run the play in check mode first to see what happens
# Extra vars
# All those inputs need to be able to be passed in
# defaults set if no entered
# Add option to backup before a change (backup role)
# Add option to backup after a change
# Make it NXOS or IOS/XE agnostic
# Turn into a role at this point
# Need the version pre supplied
# OS needed to make decision on nxos or ios config modules
################################################################
# Parse of csv file to YAML host inventory
################################################################
- name: Parse CSV To YAML
template:
src: "{{scr}}"
dest: "{{dst}}"
run_once: true
tags:
- gen_hosts
################################################################
# Run tests first - any issues we exit the play
################################################################
- name: Gather some custom facts {{ inventory_hostname }} {{ ansible_ssh_host}}
ios_command:
commands:
- "{{tests['inventory']}}"
- "{{tests['version']}}"
- "{{tests['hostname']}}"
provider: "{{ cli }}"
register: run_tests
when: ("yes" in run_tests)
tags:
- gather_facts
- name: Print the tests
debug:
var: run_tests
when: show_debug == "yes"
# ################################################################
# # Set the results of the above actions, to test with meta handlers
# ################################################################
- name: Check results of the tests in one task
set_fact:
test_combo_result: "1"
when: ("serno in item" and "type in item" and "inventory_hostname in item")
with_items: "{{run_tests.stdout}}"
# ################################################################
# # More debugging
# ################################################################
- name: debug
debug:
var: test_combo_result
when: show_debug == "yes"
# ################################################################
# # At this point we can stack up the exit conditions with the logic
# # in the When statement
# # Turns out the module is idempotent, when you make it all case
# # sensitive!!
# # So can make more host tests and still end with meta, just not
# # the config
# ################################################################
- meta: end_play
when: (test_combo_result is not defined)
# ################################################################
# # Attempt to push the config here
# ################################################################
- name: Push a set of commands
ios_config:
provider: "{{ cli }}"
lines:
- "{{ hostvars[item]['line1'] }}"
- "{{ hostvars[item]['line2'] }}"
parents: "{{ hostvars[item]['parent'] }}"
save_when: modified
register: "set_config"
with_items: "{{ groups['oneshot'] }}"
tags:
- push_config
# ################################################################
# # Debug the config here
# ################################################################
- name: debug
debug:
var: set_config
when: show_debug == "yes"
#################################################################
# backup the config here - tftp or scp??
#################################################################