Skip to content

Commit

Permalink
initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrusso42518 committed Nov 29, 2018
0 parents commit 4f67ae4
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
language: python
python:
- "2.7"

# Install python packages for ansible and linters.
install:
- "pip install -r requirements.txt"

# Execute linting and unit tests before running the main playbook.
before_script:
- "find . -name '*.yml' | xargs yamllint -s"

# Perform playbook testing with mock CI inputs.
script:
- "ansible-playbook test_playbook.yml -i hosts.yml"
11 changes: 11 additions & 0 deletions hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
all:
hosts:
CSR1:
vars:
ansible_network_os: "ios"
ansible_user: "ansible"
ansible_ssh_pass: "ansible"
ntp_server1: "192.0.2.1"
ntp_server2: "192.0.2.2"
...
29 changes: 29 additions & 0 deletions ntp_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: "Manage NTP configuration"
hosts: all
connection: network_cli
gather_facts: false
tasks:
- name: "TASK 1: Validate NTP server IP addresses"
assert:
that:
- "ntp_server1 is defined"
- "ntp_server2 is defined"
- "ntp_server1 | ipv4 == ntp_server1"
- "ntp_server2 | ipv4 == ntp_server2"
msg: "Malformed input; please check ntp_server values"

- name: "TASK 2: Apply NTP updates"
ios_config:
commands:
- "ntp authenticate"
- "ntp logging"
- "ntp server {{ ntp_server1 }}"
- "ntp server {{ ntp_server2 }}"
register: "ntp_updates"

- name: "TASK 3: Print changes if NTP config changed"
debug:
var: ntp_updates.updates
when: "ntp_updates.updates is defined"
...
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ansible==2.6.2
yamllint
44 changes: 44 additions & 0 deletions test_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name: "Import original playbook with modified inputs"
import_playbook: "ntp_playbook.yml"
vars:
ntp_server1: "203.0.113.1"
ntp_server2: "203.0.113.2"

- name: "Log into routers to test playbook"
hosts: all
connection: network_cli
gather_facts: false
tasks:
- name: "TASK 1: Gather mock NTP data after enabling NTP"
ios_command:
commands: "show ntp associations"
register: ntp_associations

- name: "TASK 2: Check that mock NTP data is present"
assert:
that:
- "'203.0.113.1' in ntp_associations.stdout[0]"
- "'203.0.113.2' in ntp_associations.stdout[0]"
msg: "Missing some NTP data:\n{{ ntp_associations.stdout[0] }}"

- name: "TASK 2: Disable NTP"
ios_config:
commands: "no ntp"

- name: "TASK 3: Wait 2 seconds"
pause:
seconds: 2

- name: "TASK 4: Gather mock NTP data after disabling NTP"
ios_command:
commands: "show ntp associations"
register: ntp_associations

- name: "TASK 5: Check mock NTP data is absent"
assert:
that:
- "'203.0.113.1' not in ntp_associations.stdout[0]"
- "'203.0.113.2' not in ntp_associations.stdout[0]"
msg: "Saw some NTP data:\n{{ ntp_associations.stdout[0] }}"
...

0 comments on commit 4f67ae4

Please sign in to comment.