diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..baa2d87 --- /dev/null +++ b/.travis.yml @@ -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" diff --git a/hosts.yml b/hosts.yml new file mode 100644 index 0000000..e221fab --- /dev/null +++ b/hosts.yml @@ -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" +... diff --git a/ntp_playbook.yml b/ntp_playbook.yml new file mode 100644 index 0000000..021dc4e --- /dev/null +++ b/ntp_playbook.yml @@ -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" +... diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..37ce370 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +ansible==2.6.2 +yamllint diff --git a/test_playbook.yml b/test_playbook.yml new file mode 100644 index 0000000..7ad6f81 --- /dev/null +++ b/test_playbook.yml @@ -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] }}" +...