|
| 1 | +--- |
| 2 | +# This is the first play, but rather than define a new play |
| 3 | +# and corresponding set of tasks, we import the main NTP |
| 4 | +# playbook and pass in some custom NTP servers instead. Using |
| 5 | +# static inputs is a good way to "stub out" our variables file |
| 6 | +# for the purpose of testing the playbook's logic. |
| 7 | +- name: "PLAY 1: Import original playbook with mock NTP servers" |
| 8 | + import_playbook: "ntp_config.yml" |
| 9 | + vars: |
| 10 | + ntp_server1: "203.0.113.1" |
| 11 | + ntp_server2: "203.0.113.2" |
| 12 | + |
| 13 | +# This is the second play. Its purpose is to ensure the |
| 14 | +# first play (playbook import) worked correctly. The general |
| 15 | +# flow is to ensure the NTP configuration was correctly applied, |
| 16 | +# remove the NTP configuration, then ensure the NTP configuration |
| 17 | +# was correctly removed. Removal isn't critical, but without it |
| 18 | +- name: "PLAY 2: Log into routers to test playbook" |
| 19 | + hosts: "all" |
| 20 | + tasks: |
| 21 | + |
| 22 | + # This is the first task. It collects information from the |
| 23 | + # router using the "ios_command" module, which is used for |
| 24 | + # non-configuration (exec shell) commands. The command issued |
| 25 | + # returns a table showing each NTP server configured. |
| 26 | + # The output from this command is registered in the |
| 27 | + # variable called "ntp_associations". |
| 28 | + - name: "TASK 1: Gather mock NTP state data after enabling NTP" |
| 29 | + ios_command: |
| 30 | + commands: "show ntp associations" |
| 31 | + register: "ntp_associations" |
| 32 | + |
| 33 | + # This is the second task. Now that we've registered the router's |
| 34 | + # feedback, we can test it using basic string membership/containment |
| 35 | + # operations. We must ensure that our two mock NTP servers are |
| 36 | + # present in this output. The text output is contained in a subfield |
| 37 | + # called "stdout", and the 0 index is used to represent that this is |
| 38 | + # the output associated with the first (and only) command issued. |
| 39 | + - name: "TASK 2: Check that mock NTP servers are present" |
| 40 | + assert: |
| 41 | + that: |
| 42 | + - "'203.0.113.1' in ntp_associations.stdout[0]" |
| 43 | + - "'203.0.113.2' in ntp_associations.stdout[0]" |
| 44 | + msg: "Missing some NTP data:\n{{ ntp_associations.stdout[0] }}" |
| 45 | + |
| 46 | + # This is the third task. At this point, we've verified that the |
| 47 | + # original NTP playbook works, but we should clean up after ourselves. |
| 48 | + # Note that if we wanted to create a more robust and complex CI system, |
| 49 | + # these steps would be unnecessary, since the disposal nature of |
| 50 | + # virtual routers would indicate that simply deleting the router |
| 51 | + # and spinning up a new one for a future CI test would be fine. In our |
| 52 | + # case, the virtual router is longer living, and we want to prepare it |
| 53 | + # for future tests by removing the mock servers. |
| 54 | + - name: "TASK 3: Remove mock NTP servers" |
| 55 | + ios_config: |
| 56 | + commands: |
| 57 | + - "no ntp server 203.0.113.1" |
| 58 | + - "no ntp server 203.0.113.2" |
| 59 | + |
| 60 | + # This is the fourth task and inserts a short pause using the |
| 61 | + # "pause" module. This allows the router to fully disable its |
| 62 | + # NTP logic for the two recently removed servers. Waiting helps |
| 63 | + # avoid a false negative in future tasks by ensuring the router |
| 64 | + # does not incorrectly report that these NTP servers still exist. |
| 65 | + - name: "TASK 4: Wait 1 seconds" |
| 66 | + pause: |
| 67 | + seconds: 1 |
| 68 | + |
| 69 | + # This is the fifth task and is a copy of the second task. We |
| 70 | + # need to update our ntp_associations variable and test to |
| 71 | + # ensure NTP was removed in a future task. |
| 72 | + - name: "TASK 5: Gather mock NTP data after disabling NTP" |
| 73 | + ios_command: |
| 74 | + commands: "show ntp associations" |
| 75 | + register: "ntp_associations" |
| 76 | + |
| 77 | + # This is the sixth task and checks the registered output from the |
| 78 | + # previous task to ensure the mock NTP servers were not seen |
| 79 | + # in the NTP association state data. If they were, then something |
| 80 | + # went wrong with the NTP configuration removal in task 4. |
| 81 | + - name: "TASK 6: Check mock NTP data is absent" |
| 82 | + assert: |
| 83 | + that: |
| 84 | + - "'203.0.113.1' not in ntp_associations.stdout[0]" |
| 85 | + - "'203.0.113.2' not in ntp_associations.stdout[0]" |
| 86 | + msg: "Saw some NTP data:\n{{ ntp_associations.stdout[0] }}" |
| 87 | +... |
0 commit comments