├── .ansible-lint ├── .github └── workflows │ └── ansible-lint.yml ├── .gitignore ├── F5 ├── address_list.md ├── address_list.yml └── bigip-info.yml ├── LICENSE ├── NetBox ├── get_ip.md ├── get_ip.yml ├── lookup.md └── lookup.yml ├── README.md ├── ansible-navigator.yml ├── ansible.cfg ├── collect-command-lines.yml ├── collect-command.md ├── collect-command.yml ├── collections └── requirements.yml ├── credenials-test.yml ├── data.json ├── dns-lookup.yml ├── files ├── css │ └── main.css ├── ip_addresses.json ├── neighbors.json ├── ospf.json ├── pictures │ ├── f5.png │ └── ntp-report.png ├── schema-array.json ├── schema.json ├── test_jschema.py └── webpage_logo.png ├── incorrect.yml ├── ios-genie-show-acl.md ├── ios-genie-show-acl.yml ├── ios-genie-show-ver.md ├── ios-genie-show-ver.yml ├── ip_range.md ├── ip_range.yml ├── list.json ├── meraki.md ├── meraki.yml ├── multi-line-config.md ├── multi-line-config.yml ├── network-restore.yml ├── network_backup.yml ├── ntp-compliance-email.yml ├── ntp-compliance.md ├── ntp-compliance.yml ├── roles ├── backup │ └── tasks │ │ ├── eos.yml │ │ ├── ios.yml │ │ ├── iosxr.yml │ │ ├── junos.yml │ │ ├── main.yml │ │ └── nxos.yml ├── ntpcheck │ └── tasks │ │ ├── ios.yml │ │ ├── iosxr.yml │ │ ├── main.yml │ │ ├── nxos.yml │ │ └── report │ │ └── data.yml ├── requirements.yml └── restore │ └── tasks │ ├── eos.yml │ ├── ios.yml │ ├── junos.yml │ └── main.yml ├── show-diff.md ├── show-diff.yml ├── templates └── report.j2 ├── test-json-tasks-1.yml ├── test-json-tasks-2.yml ├── test-json-tasks-3.yml ├── test-json-tasks-4.yml ├── test-json.md ├── test-json.yml ├── test-list.yml ├── use-encrypted-file.yml ├── validate.yml ├── validate_commands.yml └── validate_state.yml /.ansible-lint: -------------------------------------------------------------------------------- 1 | # exclude_paths included in this file are parsed relative to this file's location 2 | exclude_paths: 3 | - roles/backup 4 | - roles/restore 5 | - .github/workflows 6 | 7 | # This makes linter to fully ignore rules/tags listed below 8 | skip_list: 9 | - unpredictability 10 | 11 | # This makes the linter display but not fail for rules/tags listed below: 12 | warn_list: 13 | - git-latest 14 | -------------------------------------------------------------------------------- /.github/workflows/ansible-lint.yml: -------------------------------------------------------------------------------- 1 | name: Ansible Lint 2 | 3 | # Controls when the workflow will run 4 | on: [push, pull_request, workflow_dispatch] 5 | 6 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 7 | jobs: 8 | # This workflow contains a single job called "lint" 9 | lint: 10 | runs-on: ubuntu-latest 11 | 12 | # Steps represent a sequence of tasks that will be executed as part of the job 13 | steps: 14 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it 15 | - uses: actions/checkout@v3 16 | 17 | - name: Lint Ansible Playbook 18 | uses: ansible/ansible-lint-action@v6.0.2 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | private.pem 2 | test 3 | temp.* 4 | hosts 5 | backup/* 6 | NetBox/credentials.sh 7 | .vscode -------------------------------------------------------------------------------- /F5/address_list.md: -------------------------------------------------------------------------------- 1 | # F5 Address list 2 | 3 | ## Variables required 4 | 5 | ### Dependencies 6 | 7 | Install `f5networks.f5_modules`. 8 | 9 | ```bash 10 | ansible-galaxy collection install f5networks.f5_modules 11 | ``` 12 | 13 | ### AFM Enabled on the FW 14 | 15 | In the GUI (https://:8443/tmui/login.jsp) Go to **System**>**License**>**Modules**. 16 | 17 | ![f5](../files/pictures/f5.png) 18 | 19 | 20 | ## Playbook 21 | 22 | Latest version -> [address_list](address_list.yml). The following output might be outdated. 23 | 24 | ```yaml 25 | - name: Manage address lists on BIG-IP AFM 26 | hosts: f5 27 | connection: local 28 | gather_facts: false 29 | vars: 30 | create: false 31 | bigip_provider: 32 | server: "{{ ansible_host }}" 33 | user: "{{ ansible_user }}" 34 | password: "{{ ansible_password }}" 35 | server_port: 8443 36 | validate_certs: false 37 | 38 | tasks: 39 | - name: Create an address list 40 | f5networks.f5_modules.bigip_firewall_address_list: 41 | name: foo 42 | addresses: 43 | - 3.3.3.3 44 | - 4.4.4.4 45 | - 5.5.5.5 46 | provider: "{{ bigip_provider }}" 47 | register: output 48 | when: create 49 | 50 | - name: Remove an address list 51 | f5networks.f5_modules.bigip_firewall_address_list: 52 | name: foo 53 | state: absent 54 | provider: "{{ bigip_provider }}" 55 | register: output 56 | when: not create 57 | 58 | - name: Display output 59 | ansible.builtin.debug: 60 | var: output 61 | tags: debug 62 | ``` 63 | 64 | ## Output 65 | 66 | The following output might be outdated. 67 | 68 | ```bash 69 | ⇨ ansible-playbook -i ../../f5-host address_list.yml --skip-tags=debug 70 | 71 | PLAY [Manage address lists on BIG-IP AFM] ******************************************************************************************** 72 | 73 | TASK [Create an address list] ******************************************************************************************************** 74 | changed: [f5] 75 | 76 | TASK [Remove an address list] ******************************************************************************************************** 77 | skipping: [f5] 78 | 79 | PLAY RECAP *************************************************************************************************************************** 80 | f5 : ok=1 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 81 | ``` 82 | 83 | In the FW: 84 | 85 | ```ruby 86 | # show running-config security shared-objects address-list 87 | security shared-objects address-list foo { 88 | addresses { 89 | 3.3.3.3 { } 90 | 4.4.4.4 { } 91 | 5.5.5.5 { } 92 | } 93 | } 94 | ``` 95 | 96 | -------------------------------------------------------------------------------- /F5/address_list.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # ansible-galaxy collection install f5networks.f5_modules 3 | # ansible-playbook -i ../../f5-host address_list.yml --skip-tags=debug 4 | # One of the following features must be licensed/provisioned for the URI security/firewall/address-list : afm asm dos cgnat 5 | # UI: https://:8443/tmui/login.jsp System/License/Modules 6 | # AbstractDigestAuthHandler does not support the following scheme: 'X-Auth-Token' 7 | # Reboot: https://github.com/F5Networks/f5-ansible/issues/1798 8 | 9 | - name: Manage address lists on BIG-IP AFM 10 | hosts: f5 11 | connection: local 12 | gather_facts: false 13 | vars: 14 | create: false 15 | bigip_provider: 16 | server: "{{ ansible_host }}" 17 | user: "{{ ansible_user }}" 18 | password: "{{ ansible_password }}" 19 | server_port: 8443 20 | validate_certs: false 21 | 22 | tasks: 23 | - name: Create an address list 24 | f5networks.f5_modules.bigip_firewall_address_list: 25 | name: foo 26 | addresses: 27 | - 3.3.3.3 28 | - 4.4.4.4 29 | - 5.5.5.5 30 | provider: "{{ bigip_provider }}" 31 | register: output 32 | when: create 33 | 34 | - name: Remove an address list 35 | f5networks.f5_modules.bigip_firewall_address_list: 36 | name: foo 37 | state: absent 38 | provider: "{{ bigip_provider }}" 39 | register: output 40 | when: not create 41 | 42 | - name: Display output 43 | ansible.builtin.debug: 44 | var: output 45 | tags: debug 46 | -------------------------------------------------------------------------------- /F5/bigip-info.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # From: https://ansible.github.io/workshops/exercises/ansible_f5/1.1-get-facts/ 3 | # ansible-galaxy collection install f5networks.f5_modules 4 | # ansible-playbook -i ../../f5-host bigip-info.yml --skip-tags=debug 5 | 6 | - name: GRAB F5 FACTS 7 | hosts: f5 8 | connection: local 9 | gather_facts: false 10 | 11 | tasks: 12 | - name: COLLECT BIG-IP FACTS 13 | f5networks.f5_modules.bigip_device_info: 14 | gather_subset: 15 | - system-info 16 | provider: 17 | server: "{{ ansible_host }}" 18 | user: "{{ ansible_user }}" 19 | password: "{{ ansible_password }}" 20 | server_port: 8443 21 | validate_certs: false 22 | register: device_facts 23 | 24 | - name: DISPLAY COMPLETE BIG-IP SYSTEM INFORMATION 25 | debug: 26 | var: device_facts 27 | 28 | - name: DISPLAY ONLY THE MAC ADDRESS 29 | debug: 30 | var: device_facts['system_info']['base_mac_address'] 31 | 32 | - name: DISPLAY ONLY THE VERSION 33 | debug: 34 | var: device_facts['system_info']['product_version'] 35 | 36 | - name: DISPLAY COMPLETE BIG-IP SYSTEM INFORMATION 37 | debug: 38 | var: device_facts 39 | tags: debug 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /NetBox/get_ip.md: -------------------------------------------------------------------------------- 1 | # NetBox 2 | 3 | ## Dependencies 4 | 5 | ### Collections 6 | 7 | Install `netbox.netbox` and `ansible.utils`. 8 | ```bash 9 | ansible-galaxy collection install netbox.netbox ansible.utils 10 | ``` 11 | 12 | #### Python libraries 13 | 14 | Install `pynetbox` 15 | 16 | ``` 17 | pip3 install pynetbox 18 | ``` 19 | 20 | #### NetBox credentials 21 | 22 | Make your NetBox creadentials available, for example: 23 | 24 | ``` 25 | export NETBOX_URL=https://demo.netbox.dev/ 26 | export NETBOX_TOKEN=e150845b04b2f7336180dbb3137e29d963e4b23f 27 | ``` 28 | 29 | ## Tasks 30 | 31 | Latest version -> [get_ip](get_ip.yml). The following output might be outdated. 32 | 33 | ```yaml 34 | - name: Get a new /24 inside {{ primary_prefix }} within NetBox 35 | netbox.netbox.netbox_prefix: 36 | netbox_url: "{{ lookup('env', 'NETBOX_URL') }}" 37 | netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" 38 | data: 39 | parent: "{{ primary_prefix }}" 40 | prefix_length: 24 41 | state: present 42 | first_available: true 43 | register: prefix_info 44 | 45 | - name: Print return information from the previous task 46 | ansible.builtin.debug: 47 | var: prefix_info 48 | tags: debug 49 | 50 | - name: Allocate IP address from new range 51 | netbox.netbox.netbox_ip_address: 52 | netbox_url: "{{ lookup('env', 'NETBOX_URL') }}" 53 | netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" 54 | data: 55 | prefix: "{{ prefix_info['prefix']['prefix'] }}" 56 | state: new 57 | register: ip_address_info 58 | 59 | - name: Print return information from the previous task 60 | ansible.builtin.debug: 61 | var: ip_address_info 62 | tags: debug 63 | 64 | - name: Create variables with IP information 65 | ansible.builtin.set_fact: 66 | ip_address: "{{ ip_address_info['ip_address']['address'] | ansible.utils.ipaddr('ip') }}" 67 | netmask: "{{ ip_address_info['ip_address']['address'] | ansible.utils.ipaddr('netmask') }}" 68 | 69 | - name: Print return information from the previous task 70 | ansible.builtin.debug: 71 | msg: 72 | - "IP: {{ ip_address }}" 73 | - "Mask: {{ netmask }}" 74 | ``` 75 | 76 | ## Output 77 | 78 | The following output might be outdated. 79 | 80 | ```bash 81 | ⇨ ansible-playbook get_ip.yml --skip-tags=debug 82 | 83 | PLAY [Manage NetBox] ************************************************************************************************ 84 | 85 | TASK [Get a new /24 inside 172.16.0.0/16 within NetBox] ************************************************************* 86 | changed: [localhost] 87 | 88 | TASK [Allocate IP address from new range] *************************************************************************** 89 | changed: [localhost] 90 | 91 | TASK [Create variables with IP information] ************************************************************************* 92 | ok: [localhost] 93 | 94 | TASK [Print return information from the previous task] ************************************************************** 95 | ok: [localhost] => { 96 | "msg": [ 97 | "IP: 172.16.6.1", 98 | "Mask: 255.255.255.0" 99 | ] 100 | } 101 | 102 | PLAY RECAP ********************************************************************************************************** 103 | localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 104 | ``` 105 | 106 | -------------------------------------------------------------------------------- /NetBox/get_ip.yml: -------------------------------------------------------------------------------- 1 | # ansible-playbook get_ip.yml --skip-tags=debug 2 | # recycled from: https://josh-v.com/netbox_ansible_allocate_prefix_ipaddress/ 3 | --- 4 | - name: Manage NetBox 5 | hosts: localhost 6 | connection: local 7 | gather_facts: false 8 | vars: 9 | primary_prefix: "172.16.0.0/16" 10 | 11 | tasks: 12 | - name: Get a new /24 inside {{ primary_prefix }} within NetBox 13 | netbox.netbox.netbox_prefix: 14 | netbox_url: "{{ lookup('env', 'NETBOX_URL') }}" 15 | netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" 16 | data: 17 | parent: "{{ primary_prefix }}" 18 | prefix_length: 24 19 | state: present 20 | first_available: true 21 | register: prefix_info 22 | 23 | - name: Print return information from the previous task 24 | ansible.builtin.debug: 25 | var: prefix_info 26 | tags: debug 27 | 28 | - name: Allocate IP address from new range 29 | netbox.netbox.netbox_ip_address: 30 | netbox_url: "{{ lookup('env', 'NETBOX_URL') }}" 31 | netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" 32 | data: 33 | prefix: "{{ prefix_info['prefix']['prefix'] }}" 34 | state: new 35 | register: ip_address_info 36 | 37 | - name: Print return information from the previous task 38 | ansible.builtin.debug: 39 | var: ip_address_info 40 | tags: debug 41 | 42 | - name: Create variables with IP information 43 | ansible.builtin.set_fact: 44 | ip_address: "{{ ip_address_info['ip_address']['address'] | ansible.utils.ipaddr('ip') }}" 45 | netmask: "{{ ip_address_info['ip_address']['address'] | ansible.utils.ipaddr('netmask') }}" 46 | 47 | - name: Print return information from the previous task 48 | ansible.builtin.debug: 49 | msg: 50 | - "IP: {{ ip_address }}" 51 | - "Mask: {{ netmask }}" 52 | -------------------------------------------------------------------------------- /NetBox/lookup.md: -------------------------------------------------------------------------------- 1 | # NetBox 2 | 3 | ## Dependencies 4 | 5 | ### Collections 6 | 7 | Install `netbox.netbox` and `community.general`. 8 | ```bash 9 | ansible-galaxy collection install netbox.netbox community.general 10 | ``` 11 | 12 | #### Python libraries 13 | 14 | Install `pynetbox` and `jmespath`. 15 | 16 | ``` 17 | pip3 install pynetbox jmespath --user 18 | ``` 19 | 20 | #### NetBox credentials 21 | 22 | Make your NetBox creadentials available, for example: 23 | 24 | ``` 25 | export NETBOX_URL=https://demo.netbox.dev/ 26 | export NETBOX_TOKEN=e150845b04b2f7336180dbb3137e29d963e4b23f 27 | ``` 28 | 29 | ## Tasks 30 | 31 | Latest version -> [lookup](lookup.yml). The following output might be outdated. 32 | 33 | ```yaml 34 | - name: Get list of sites 35 | ansible.builtin.set_fact: 36 | sites: "{{ query('netbox.netbox.nb_lookup', 'sites', api_endpoint=netbox_url, token=netbox_token) }}" 37 | 38 | - name: Clean up the output 39 | ansible.builtin.debug: 40 | msg: "{{ sites | community.general.json_query('[*].value.name') }}" 41 | 42 | - name: Get list of devices with role Core Switch at MDF (ncsu-065) or DM-Buffalo (dm-buffalo) sites 43 | ansible.builtin.set_fact: 44 | devices: | 45 | {{ query('netbox.netbox.nb_lookup', 'devices', api_filter='site=ncsu-065 site=dm-buffalo 46 | role=core-switch', api_endpoint=netbox_url, token=netbox_token) }} 47 | 48 | - name: Print the result 49 | ansible.builtin.debug: 50 | msg: "{{ devices | json_query('[*].value.name') }}" 51 | ``` 52 | 53 | ## Output 54 | 55 | The following output might be outdated. 56 | 57 | ```bash 58 | ⇨ ansible-playbook lookup.yml --skip-tags=debug 59 | 60 | PLAY [Manage NetBox] ************************************************************************************************ 61 | 62 | TASK [Get list of sites] ******************************************************************************************** 63 | ok: [localhost] 64 | 65 | TASK [Clean up the output] ****************************************************************************************** 66 | ok: [localhost] => { 67 | "msg": [ 68 | "ATKNB", 69 | "Butler Communications", 70 | "D. S. Weaver Labs", 71 | "DM-Akron", 72 | "DM-Albany", 73 | "DM-Binghamton", 74 | "DM-Buffalo", 75 | "DM-Camden", 76 | "DM-NYC", 77 | "DM-Nashua", 78 | "DM-Pittsfield", 79 | "DM-Rochester", 80 | "DM-Scranton", 81 | "DM-Stamford", 82 | "DM-Syracuse", 83 | "DM-Utica", 84 | "DM-Yonkers", 85 | "Grinnells Lab", 86 | "Gênes", 87 | "JBB Branch 104", 88 | "JBB Branch 109", 89 | "JBB Branch 115", 90 | "JBB Branch 120", 91 | "JBB Branch 127", 92 | "JBB Branch 133", 93 | "MDF", 94 | "My First Site", 95 | "Test Site", 96 | "foo", 97 | "ssa-20-20", 98 | "staging_nellie" 99 | ] 100 | } 101 | 102 | TASK [Get list of devices with role Core Switch at MDF (ncsu-065) or DM-Buffalo (dm-buffalo) sites] ***************** 103 | ok: [localhost] 104 | 105 | TASK [Print the result] ********************************************************************************************* 106 | ok: [localhost] => { 107 | "msg": [ 108 | "CORE-VSS-1", 109 | "ncsu-coreswitch1", 110 | "ncsu-coreswitch2" 111 | ] 112 | } 113 | 114 | PLAY RECAP ********************************************************************************************************** 115 | localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 116 | ``` 117 | 118 | -------------------------------------------------------------------------------- /NetBox/lookup.yml: -------------------------------------------------------------------------------- 1 | # ansible-playbook lookup.yml --skip-tags=debug 2 | # recycled from: https://josh-v.com/netbox_ansible_collection/netbox-ansible-lookup-plugin/ 3 | --- 4 | - name: Manage NetBox 5 | hosts: localhost 6 | connection: local 7 | gather_facts: false 8 | vars: 9 | netbox_url: "{{ lookup('env', 'NETBOX_URL') }}" 10 | netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" 11 | 12 | tasks: 13 | - name: Get list of sites 14 | ansible.builtin.set_fact: 15 | sites: "{{ query('netbox.netbox.nb_lookup', 'sites', api_endpoint=netbox_url, token=netbox_token) }}" 16 | 17 | - name: Clean up the output 18 | ansible.builtin.debug: 19 | msg: "{{ sites | community.general.json_query('[*].value.name') }}" 20 | 21 | - name: Get list of devices with role Core Switch at MDF (ncsu-065) or DM-Buffalo (dm-buffalo) sites 22 | ansible.builtin.set_fact: 23 | devices: | 24 | {{ query('netbox.netbox.nb_lookup', 'devices', api_filter='site=ncsu-065 site=dm-buffalo 25 | role=core-switch', api_endpoint=netbox_url, token=netbox_token) }} 26 | 27 | - name: Print the result 28 | ansible.builtin.debug: 29 | msg: "{{ devices | community.general.json_query('[*].value.name') }}" 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Networking collection of Playbooks 2 | 3 | ![Ansible Lint](https://github.com/nleiva/ansible-networking/workflows/Ansible%20Lint/badge.svg) 4 | 5 | This is my collection of Ansible Networking examples. Of course, most of these are recycled from other repositories. 6 | 7 | ## Inventory 8 | 9 | I'm using this [ansible-inventory](https://github.com/nleiva/ansible-inventory/blob/master/hosts) to provide output examples ([DevNet always-on](https://developer.cisco.com/docs/sandbox/#!networking/networking-overview) devices). 10 | 11 | ## Examples 12 | 13 | - [Collect a command output](collect-command.md) 14 | - [F5 Address list](F5/address_list.md) 15 | - [Get IP address from NetBox](NetBox/get_ip.md) 16 | - [Meraki](meraki.md) 17 | - [Multi-line Config](multi-line-config.md) 18 | - [NetBox lookup](NetBox/lookup.md) 19 | - [NTP Compliance](ntp-compliance.md) 20 | - [Parse JSON outputs](test-json.md) 21 | - [Parse IOS XE ACLs](ios-genie-show-acl.md) 22 | - [Parse IOS XE SW Version](ios-genie-show-ver.md) 23 | - [Parsing a Cisco ASA config file](https://github.com/nleiva/ansible-parsing-cisco-asa): Three options to parse data from an unstructured config file. 24 | - [Reading IP address ranges](ip_range.md) 25 | - [Show config differences](show-diff.md) 26 | - [Network resource modules in action](https://github.com/nleiva/ansible-net-modules) 27 | -------------------------------------------------------------------------------- /ansible-navigator.yml: -------------------------------------------------------------------------------- 1 | --- 2 | ansible-navigator: 3 | ansible: 4 | config: ansible.cfg 5 | inventories: 6 | - ./hosts 7 | 8 | editor: 9 | command: code -g {filename}:{line_number} 10 | console: false 11 | 12 | logging: 13 | level: warning 14 | 15 | execution-environment: 16 | container-engine: podman 17 | enabled: true 18 | pull-policy: missing 19 | image: quay.io/nleiva/ee-general-image 20 | environment-variables: 21 | pass: 22 | - AWS_ACCESS_KEY_ID 23 | - AWS_SECRET_ACCESS_KEY 24 | 25 | playbook-artifact: 26 | enable: false 27 | 28 | mode: stdout 29 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | interpreter_python = auto_silent 3 | stdout_callback = yaml 4 | inventory = hosts 5 | forks = 50 6 | host_key_checking = False 7 | retry_files_enabled = False 8 | no_target_syslog = False 9 | callback_enabled = time 10 | 11 | [ssh_connection] 12 | scp_if_ssh = True 13 | 14 | [persistent_connection] 15 | connect_timeout = 60 16 | command_timeout = 60 -------------------------------------------------------------------------------- /collect-command-lines.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - 4 | hosts: "{{ my_devices }}" 5 | gather_facts: "{{ my_facts }}" 6 | 7 | tasks: 8 | - name: Capture SHOW COMMAND 9 | cli_command: 10 | command: "{{ my_cmd }}" 11 | register: cmd_output 12 | 13 | - name: Display OUTPUT, line by line 14 | debug: 15 | msg: Output is {{ cmd_output.stdout_lines }} 16 | 17 | ... 18 | -------------------------------------------------------------------------------- /collect-command.md: -------------------------------------------------------------------------------- 1 | # Collecting the output of a given command 2 | 3 | ## Variables required 4 | 5 | - `my_devices`: One or more groups or host patterns, separated by colons. 6 | - `my_facts`: Whether to collect facts per device: `yes` or `no`. 7 | - `my_cmd`: Command to issue. Ex: `show version | i ersion` 8 | 9 | ## Playbook 10 | 11 | Latest version -> [collect-command](collect-command.yml). The following output might be outdated. 12 | 13 | ```yaml 14 | hosts: "{{ my_devices }}" 15 | gather_facts: "{{ my_facts }}" 16 | 17 | tasks: 18 | - name: Capture SHOW COMMAND 19 | cli_command: 20 | command: "{{ my_cmd }}" 21 | register: cmd_output 22 | 23 | - name: Display OUTPUT 24 | debug: 25 | msg: Output is {{ cmd_output.stdout }} 26 | ``` 27 | 28 | ## Output 29 | 30 | The following output might be outdated. 31 | 32 | ```bash 33 | PLAY [ssh_devices] ************************************************************* 34 | 35 | TASK [Capture SHOW COMMAND] **************************************************** 36 | ok: [CSR1000V SSH] 37 | ok: [IOS XRv 9000 SSH] 38 | ok: [Nexus 9000v SSH] 39 | 40 | TASK [Display OUTPUT] ********************************************************** 41 | ok: [CSR1000V SSH] => { 42 | "msg": "Output is Cisco IOS XE Software, Version 16.11.01a\nCisco IOS Software [Gibraltar], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.11.1a, RELEASE SOFTWARE (fc1)\nlicensed under the GNU General Public License (\"GPL\") Version 2.0. The\nsoftware code licensed under GPL Version 2.0 is free software that comes\nGPL code under the terms of GPL Version 2.0. For more details, see the" 43 | } 44 | ok: [IOS XRv 9000 SSH] => { 45 | "msg": "Output is Cisco IOS XR Software, Version 6.5.3\n Version : 6.5.3" 46 | } 47 | ok: [Nexus 9000v SSH] => { 48 | "msg": "Output is Nexus 9000v is a demo version of the Nexus Operating System\n BIOS: version \n NXOS: version 9.2(1)\n System version:" 49 | } 50 | 51 | PLAY RECAP ********************************************************************* 52 | CSR1000V SSH : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 53 | IOS XRv 9000 SSH : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 54 | Nexus 9000v SSH : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 55 | ``` 56 | 57 | -------------------------------------------------------------------------------- /collect-command.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - 4 | hosts: "{{ my_devices }}" 5 | gather_facts: "{{ my_facts }}" 6 | 7 | tasks: 8 | - name: Capture SHOW COMMAND 9 | cli_command: 10 | command: "{{ my_cmd }}" 11 | register: cmd_output 12 | 13 | - name: Display OUTPUT 14 | debug: 15 | msg: Output is {{ cmd_output.stdout }} 16 | 17 | ... 18 | -------------------------------------------------------------------------------- /collections/requirements.yml: -------------------------------------------------------------------------------- 1 | --- 2 | collections: 3 | - name: amazon.aws 4 | version: 2.0.0 5 | - name: ansible.netcommon 6 | version: 2.0.0 7 | - name: ansible.utils 8 | - name: cisco.ios 9 | # - name: f5networks.f5_modules 10 | - name: community.general 11 | - name: cisco.meraki 12 | -------------------------------------------------------------------------------- /credenials-test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Test hostvars access from credentials 3 | hosts: all 4 | gather_facts: true 5 | vars: 6 | my_test1: "{{ ec2_tag_Contact | default('It did NOT work', true) }}" 7 | 8 | tasks: 9 | # - name: Debug hostvars 10 | # debug: 11 | # var: hostvars 12 | 13 | # - name: Debug groups 14 | # debug: 15 | # var: groups 16 | 17 | - name: Print from tags 18 | debug: 19 | msg: "{{ ec2_tag_Contact }}" 20 | 21 | - name: Print from variables 22 | debug: 23 | msg: "{{ my_test1 }}" 24 | 25 | - name: Print from inventory 26 | debug: 27 | msg: "{{ my_test2 }}" 28 | 29 | - name: End message 30 | debug: 31 | msg: "All good, looks like debug: var: has an issue" 32 | -------------------------------------------------------------------------------- /data.json: -------------------------------------------------------------------------------- 1 | $ANSIBLE_VAULT;1.2;AES256;nleiva 2 | 61343430313230613336393064636133383563646461633537356563666561373938363364336661 3 | 6535336539653539316563366333383163633866343539310a666665313139383537623531303262 4 | 62666137653338363131363538663464643234363461623434643833313134326438366336323137 5 | 6433326136363231640a313637646563336230633334396665633033663963616465383063386362 6 | 6132 7 | -------------------------------------------------------------------------------- /dns-lookup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Test DNS 3 | hosts: localhost 4 | connection: local 5 | become: false 6 | gather_facts: false 7 | 8 | tasks: 9 | - name: Test 1 10 | debug: msg="{{ lookup('dig', 'www.redhat.com') }}" 11 | 12 | - name: Debug hostvars 13 | debug: 14 | var: groups 15 | -------------------------------------------------------------------------------- /files/css/main.css: -------------------------------------------------------------------------------- 1 | p.hostname { 2 | color: #000000; 3 | font-weight: bolder; 4 | font-size: large; 5 | } 6 | 7 | #subtable { 8 | background: #ebebeb; 9 | margin: 0px; 10 | } 11 | 12 | #subtable tbody tr td { 13 | padding: 5px 5px 5px 5px; 14 | } 15 | 16 | #subtable thead th { 17 | padding: 5px; 18 | } 19 | 20 | * { 21 | -moz-box-sizing: border-box; 22 | -webkit-box-sizing: border-box; 23 | box-sizing: border-box; 24 | font-family: "Open Sans", "Helvetica"; 25 | 26 | } 27 | 28 | a { 29 | color: #ffffff; 30 | } 31 | 32 | p { 33 | color: #ffffff; 34 | } 35 | h1 { 36 | text-align: center; 37 | color: #ffffff; 38 | } 39 | 40 | body { 41 | background:#353a40; 42 | } 43 | 44 | table { 45 | border-collapse: separate; 46 | background:#fff; 47 | @include border-radius(5px); 48 | margin:50px auto; 49 | @include box-shadow(0px 0px 5px rgba(0,0,0,0.3)); 50 | } 51 | 52 | thead { 53 | @include border-radius(5px); 54 | } 55 | 56 | thead th { 57 | font-family: 'Patua One', monospace; 58 | font-size:16px; 59 | font-weight:400; 60 | color:#fff; 61 | @include text-shadow(1px 1px 0px rgba(0,0,0,0.5)); 62 | text-align:left; 63 | padding:20px; 64 | border-top:1px solid #858d99; 65 | background: #353a40; 66 | 67 | &:first-child { 68 | @include border-top-left-radius(5px); 69 | } 70 | 71 | &:last-child { 72 | @include border-top-right-radius(5px); 73 | } 74 | } 75 | 76 | tbody tr td { 77 | font-family: 'Open Sans', sans-serif; 78 | font-weight:400; 79 | color:#5f6062; 80 | font-size:13px; 81 | padding:20px 20px 20px 20px; 82 | border-bottom:1px solid #e0e0e0; 83 | 84 | } 85 | 86 | tbody tr:nth-child(2n) { 87 | background:#f0f3f5; 88 | } 89 | 90 | tbody tr:last-child td { 91 | border-bottom:none; 92 | &:first-child { 93 | @include border-bottom-left-radius(5px); 94 | } 95 | &:last-child { 96 | @include border-bottom-right-radius(5px); 97 | } 98 | } 99 | 100 | span.highlight { 101 | background-color: yellow; 102 | } 103 | 104 | .expandclass { 105 | color: #5f6062; 106 | } 107 | 108 | .content{ 109 | display:none; 110 | margin: 10px; 111 | } 112 | -------------------------------------------------------------------------------- /files/ip_addresses.json: -------------------------------------------------------------------------------- 1 | { 2 | "syncToken": "0123456789", 3 | "createDate": "yyyy-mm-dd-hh-mm-ss", 4 | "prefixes": [ 5 | { 6 | "ip_prefix": "192.0.2.0/24", 7 | "region": "region", 8 | "network_border_group": "network_border_group", 9 | "service": "subset" 10 | } 11 | ], 12 | "ipv6_prefixes": [ 13 | { 14 | "ipv6_prefix": "2001:db8:cafe::/64", 15 | "region": "region", 16 | "network_border_group": "network_border_group", 17 | "service": "subset" 18 | } 19 | ] 20 | } -------------------------------------------------------------------------------- /files/neighbors.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "203.0.113.2": { 4 | "address": "198.51.100.2", 5 | "dead_time": "00:00:39", 6 | "priority": 0, 7 | "state": "FULL/ -" 8 | } 9 | }, 10 | { 11 | "203.0.113.2": { 12 | "address": "192.0.2.2", 13 | "dead_time": "00:00:36", 14 | "priority": 0, 15 | "state": "INIT/ -" 16 | } 17 | } 18 | ] -------------------------------------------------------------------------------- /files/ospf.json: -------------------------------------------------------------------------------- 1 | { 2 | "parsed": { 3 | "interfaces": { 4 | "Tunnel0": { 5 | "neighbors": { 6 | "203.0.113.2": { 7 | "address": "198.51.100.2", 8 | "dead_time": "00:00:39", 9 | "priority": 0, 10 | "state": "FULL/ -" 11 | } 12 | } 13 | }, 14 | "Tunnel1": { 15 | "neighbors": { 16 | "203.0.113.2": { 17 | "address": "192.0.2.2", 18 | "dead_time": "00:00:36", 19 | "priority": 0, 20 | "state": "INIT/ -" 21 | } 22 | } 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /files/pictures/f5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nleiva/ansible-networking/5916e3dd80ec1797aed814c7af86606b17dd3438/files/pictures/f5.png -------------------------------------------------------------------------------- /files/pictures/ntp-report.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nleiva/ansible-networking/5916e3dd80ec1797aed814c7af86606b17dd3438/files/pictures/ntp-report.png -------------------------------------------------------------------------------- /files/schema-array.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "neighbor" : { 5 | "type" : "object", 6 | "properties" : { 7 | "address" : {"type" : "string"}, 8 | "dead_time" : {"type" : "string"}, 9 | "priority" : {"type" : "number"}, 10 | "state" : { 11 | "type" : "string", 12 | "pattern" : "^FULL" 13 | } 14 | }, 15 | "required" : [ "address","state" ] 16 | } 17 | }, 18 | "type": "array", 19 | "properties": { 20 | "type": "object", 21 | "patternProperties": { 22 | ".*" : { "$ref" : "#/definitions/neighbor" } 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /files/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "neighbor" : { 5 | "type" : "object", 6 | "properties" : { 7 | "address" : {"type" : "string"}, 8 | "dead_time" : {"type" : "string"}, 9 | "priority" : {"type" : "number"}, 10 | "state" : { 11 | "type" : "string", 12 | "pattern" : "^FULL" 13 | } 14 | }, 15 | "required" : [ "address","state" ] 16 | } 17 | }, 18 | "type": "object", 19 | "patternProperties": { 20 | ".*" : { "$ref" : "#/definitions/neighbor" } 21 | } 22 | } -------------------------------------------------------------------------------- /files/test_jschema.py: -------------------------------------------------------------------------------- 1 | from jsonschema import validate 2 | import json 3 | 4 | with open('files/schema.json') as f: 5 | schema = json.load(f) 6 | 7 | with open('files/neighbors.json') as f: 8 | data = json.load(f) 9 | 10 | for neighbor in data: 11 | print(json.dumps(neighbor, indent=2)) 12 | validate(instance=neighbor, schema=schema) 13 | 14 | # with open('files/schema-array.json') as f: 15 | # schema = json.load(f) 16 | 17 | # validate(instance=data, schema=schema) -------------------------------------------------------------------------------- /files/webpage_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nleiva/ansible-networking/5916e3dd80ec1797aed814c7af86606b17dd3438/files/webpage_logo.png -------------------------------------------------------------------------------- /incorrect.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Test 3 | hosts: localhost 4 | connection: local 5 | 6 | tasks: 7 | - name: Print Tower user 8 | debug: 9 | msg: '{{ tower_user_name }}' 10 | -------------------------------------------------------------------------------- /ios-genie-show-acl.md: -------------------------------------------------------------------------------- 1 | # Parse IOS XE ACL's 2 | 3 | ## Pre-requisites 4 | 5 | Have [parse_genie](https://galaxy.ansible.com/clay584/parse_genie) role installed. 6 | 7 | ```bash 8 | sudo yum install python3-devel 9 | sudo pip3 install psutil 10 | sudo pip3 install paramiko 11 | sudo pip3 install pyats 12 | sudo pip3 install genie 13 | 14 | ansible-galaxy install -r roles/requirements.yml 15 | ``` 16 | 17 | ## Variables required 18 | 19 | - `my_devices`: One or more groups or host patterns, separated by colons. 20 | - `my_facts`: Whether to collect facts per device: `yes` or `no`. 21 | 22 | ## Playbook 23 | 24 | Latest version -> [ios-genie-show-acl](ios-genie-show-acl.yml). The following output might be outdated. 25 | 26 | ```yaml 27 | hosts: "{{ my_devices }}" 28 | gather_facts: "{{ my_facts }}" 29 | 30 | tasks: 31 | - name: SHOW ACL's 32 | ios_command: 33 | commands: 34 | - show ip access-lists 35 | register: acls 36 | 37 | - name: PARSE with GENIE 38 | set_fact: 39 | pyats_acls: "{{ acls['stdout'][0] | parse_genie(command='show ip access-lists', os='iosxe') }}" 40 | 41 | - name: PRINT OUT 42 | debug: 43 | var: pyats_acls 44 | ``` 45 | 46 | ## Output 47 | 48 | The following output might be outdated. 49 | 50 | ```bash 51 | ⇨ ansible-playbook -i hosts -e "my_devices=ios, my_facts=no" ios-genie-show-acl.yml 52 | 53 | PLAY [IOS-XE Parse ACL's] ***************************************************************************************************************** 54 | 55 | TASK [SHOW ACL's] ************************************************************************************************************************* 56 | ok: [ios-xe-mgmt-latest.cisco.com] 57 | 58 | TASK [PARSE with GENIE] ******************************************************************************************************************* 59 | ok: [ios-xe-mgmt-latest.cisco.com] 60 | 61 | TASK [PRINT OUT] ************************************************************************************************************************** 62 | ok: [ios-xe-mgmt-latest.cisco.com] => { 63 | "pyats_acls": { 64 | "TEST": { 65 | "aces": { 66 | "10": { 67 | "actions": { 68 | "forwarding": "permit", 69 | "logging": "log-none" 70 | }, 71 | "matches": { 72 | "l3": { 73 | "ipv4": { 74 | "destination_network": { 75 | "any": { 76 | "destination_network": "any" 77 | } 78 | }, 79 | "protocol": "tcp", 80 | "source_network": { 81 | "any": { 82 | "source_network": "any" 83 | } 84 | } 85 | } 86 | }, 87 | "l4": { 88 | "tcp": { 89 | "destination_port": { 90 | "operator": { 91 | "operator": "eq", 92 | "port": 80 93 | } 94 | }, 95 | "established": false 96 | } 97 | } 98 | }, 99 | "name": "10" 100 | }, 101 | "20": { 102 | "actions": { 103 | "forwarding": "permit", 104 | "logging": "log-none" 105 | }, 106 | "matches": { 107 | "l3": { 108 | "ipv4": { 109 | "destination_network": { 110 | "any": { 111 | "destination_network": "any" 112 | } 113 | }, 114 | "protocol": "tcp", 115 | "source_network": { 116 | "any": { 117 | "source_network": "any" 118 | } 119 | } 120 | } 121 | }, 122 | "l4": { 123 | "tcp": { 124 | "destination_port": { 125 | "operator": { 126 | "operator": "eq", 127 | "port": 443 128 | } 129 | }, 130 | "established": false 131 | } 132 | } 133 | }, 134 | "name": "20" 135 | }, 136 | "30": { 137 | "actions": { 138 | "forwarding": "permit", 139 | "logging": "log-none" 140 | }, 141 | "matches": { 142 | "l3": { 143 | "ipv4": { 144 | "destination_network": { 145 | "host 8.8.8.8": { 146 | "destination_network": "host 8.8.8.8" 147 | } 148 | }, 149 | "protocol": "tcp", 150 | "source_network": { 151 | "any": { 152 | "source_network": "any" 153 | } 154 | } 155 | } 156 | }, 157 | "l4": { 158 | "tcp": { 159 | "destination_port": { 160 | "operator": { 161 | "operator": "eq", 162 | "port": 53 163 | } 164 | }, 165 | "established": false 166 | } 167 | } 168 | }, 169 | "name": "30" 170 | }, 171 | "40": { 172 | "actions": { 173 | "forwarding": "permit", 174 | "logging": "log-none" 175 | }, 176 | "matches": { 177 | "l3": { 178 | "ipv4": { 179 | "destination_network": { 180 | "host 8.8.8.8": { 181 | "destination_network": "host 8.8.8.8" 182 | } 183 | }, 184 | "protocol": "udp", 185 | "source_network": { 186 | "any": { 187 | "source_network": "any" 188 | } 189 | } 190 | } 191 | }, 192 | "l4": { 193 | "udp": { 194 | "destination_port": { 195 | "operator": { 196 | "operator": "eq", 197 | "port": 53 198 | } 199 | }, 200 | "established": false 201 | } 202 | } 203 | }, 204 | "name": "40" 205 | }, 206 | "50": { 207 | "actions": { 208 | "forwarding": "permit", 209 | "logging": "log-none" 210 | }, 211 | "matches": { 212 | "l3": { 213 | "ipv4": { 214 | "destination_network": { 215 | "any": { 216 | "destination_network": "any" 217 | } 218 | }, 219 | "protocol": "tcp", 220 | "source_network": { 221 | "192.0.2.0 0.0.0.255": { 222 | "source_network": "192.0.2.0 0.0.0.255" 223 | } 224 | } 225 | } 226 | }, 227 | "l4": { 228 | "tcp": { 229 | "established": false 230 | } 231 | } 232 | }, 233 | "name": "50" 234 | } 235 | }, 236 | "name": "TEST", 237 | "type": "ipv4-acl-type" 238 | }, 239 | "meraki-fqdn-dns": { 240 | "name": "meraki-fqdn-dns", 241 | "type": "ipv4-acl-type" 242 | } 243 | } 244 | } 245 | 246 | PLAY RECAP ******************************************************************************************************************************** 247 | ios-xe-mgmt-latest.cisco.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 248 | ``` 249 | 250 | -------------------------------------------------------------------------------- /ios-genie-show-acl.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: IOS-XE Parse ACL's 3 | hosts: "{{ my_devices }}" 4 | gather_facts: "{{ my_facts }}" 5 | connection: network_cli 6 | roles: 7 | - parse_genie 8 | 9 | tasks: 10 | - name: SHOW ACL's 11 | ios_command: 12 | commands: 13 | - show ip access-lists 14 | register: acls 15 | 16 | - name: PARSE with GENIE 17 | set_fact: 18 | pyats_acls: "{{ acls['stdout'][0] | parse_genie(command='show ip access-lists', os='iosxe') }}" 19 | 20 | - name: PRINT OUT 21 | debug: 22 | var: pyats_acls 23 | 24 | # ip access-list extended TEST 25 | # permit tcp any any eq www 26 | # permit tcp any any eq 443 27 | # permit tcp any host 8.8.8.8 eq domain 28 | # permit udp any host 8.8.8.8 eq domain 29 | # permit tcp 192.0.2.0 0.0.0.255 any 30 | # ! 31 | -------------------------------------------------------------------------------- /ios-genie-show-ver.md: -------------------------------------------------------------------------------- 1 | # Parse IOS XE SW Version 2 | 3 | ## Pre-requisites 4 | 5 | Have [parse_genie](https://galaxy.ansible.com/clay584/parse_genie) role installed. 6 | 7 | ```bash 8 | sudo yum install python3-devel 9 | sudo pip3 install psutil 10 | sudo pip3 install paramiko 11 | sudo pip3 install pyats 12 | sudo pip3 install genie 13 | 14 | ansible-galaxy install -r roles/requirements.yml 15 | ``` 16 | 17 | ## Variables required 18 | 19 | - `my_devices`: One or more groups or host patterns, separated by colons. 20 | - `my_facts`: Whether to collect facts per device: `yes` or `no`. 21 | 22 | ## Playbook 23 | 24 | Latest version -> [ios-genie-show-ver](ios-genie-show-ver.yml). The following output might be outdated. 25 | 26 | ```yaml 27 | hosts: "{{ my_devices }}" 28 | gather_facts: "{{ my_facts }}" 29 | 30 | tasks: 31 | - name: show version 32 | ios_command: 33 | commands: 34 | - show version 35 | register: version 36 | 37 | - name: Set Fact Genie Filter 38 | set_fact: 39 | pyats_version: "{{ version['stdout'][0] | parse_genie(command='show version', os='ios') }}" 40 | 41 | - name: Debug Pyats facts - all 42 | debug: 43 | var: pyats_version.version 44 | ``` 45 | 46 | ## Output 47 | 48 | The following output might be outdated. 49 | 50 | ```bash 51 | ⇨ ansible-playbook -i hosts -e "my_devices=ios, my_facts=no" ios-genie-show-ver.yml 52 | 53 | PLAY [IOS show version genie example] ***************************************************************************************************** 54 | 55 | TASK [show version] *********************************************************************************************************************** 56 | ok: [ios-xe-mgmt-latest.cisco.com] 57 | 58 | TASK [Set Fact Genie Filter] ************************************************************************************************************** 59 | ok: [ios-xe-mgmt-latest.cisco.com] 60 | 61 | TASK [Debug Pyats facts - all] ************************************************************************************************************ 62 | ok: [ios-xe-mgmt-latest.cisco.com] => { 63 | "pyats_version.version": { 64 | "chassis": "CSR1000V", 65 | "chassis_sn": "9ANAICM566S", 66 | "compiled_by": "mcpre", 67 | "compiled_date": "Thu 11-Apr-19 23:59", 68 | "curr_config_register": "0x2102", 69 | "disks": { 70 | "bootflash:.": { 71 | "disk_size": "7774207", 72 | "type_of_disk": "virtual hard disk" 73 | }, 74 | "webui:.": { 75 | "disk_size": "0", 76 | "type_of_disk": "WebUI ODM Files" 77 | } 78 | }, 79 | "hostname": "csr1000v-1", 80 | "image_id": "X86_64_LINUX_IOSD-UNIVERSALK9-M", 81 | "image_type": "production image", 82 | "last_reload_reason": "reload", 83 | "license_level": "ax", 84 | "license_type": "N/A(Smart License Enabled)", 85 | "main_mem": "2378575", 86 | "mem_size": { 87 | "non-volatile configuration": "32768", 88 | "physical": "8112832" 89 | }, 90 | "next_reload_license_level": "ax", 91 | "number_of_intfs": { 92 | "Gigabit Ethernet": "3" 93 | }, 94 | "os": "IOS-XE", 95 | "platform": "Virtual XE", 96 | "processor_type": "VXE", 97 | "returned_to_rom_by": "reload", 98 | "rom": "IOS-XE ROMMON", 99 | "rtr_type": "CSR1000V", 100 | "system_image": "bootflash:packages.conf", 101 | "uptime": "3 days, 1 hour, 40 minutes", 102 | "uptime_this_cp": "3 days, 1 hour, 41 minutes", 103 | "version": "16.11.1a", 104 | "version_short": "16.11" 105 | } 106 | } 107 | 108 | TASK [Debug Pyats facts - version] ******************************************************************************************************** 109 | ok: [ios-xe-mgmt-latest.cisco.com] => { 110 | "pyats_version.version.version": "16.11.1a" 111 | } 112 | 113 | TASK [Debug Pyats facts - uptime] ********************************************************************************************************* 114 | ok: [ios-xe-mgmt-latest.cisco.com] => { 115 | "pyats_version.version.uptime": "3 days, 1 hour, 40 minutes" 116 | } 117 | 118 | PLAY RECAP ******************************************************************************************************************************** 119 | ios-xe-mgmt-latest.cisco.com : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 120 | ``` 121 | 122 | -------------------------------------------------------------------------------- /ios-genie-show-ver.yml: -------------------------------------------------------------------------------- 1 | # From: https://github.com/michaelford85/ansible-ios/blob/master/ios-genie-sho-ver.yml 2 | --- 3 | - name: IOS show version genie example 4 | hosts: "{{ my_devices }}" 5 | gather_facts: "{{ my_facts }}" 6 | connection: network_cli 7 | roles: 8 | - parse_genie 9 | 10 | tasks: 11 | - name: show version 12 | ios_command: 13 | commands: 14 | - show version 15 | register: version 16 | 17 | - name: Set Fact Genie Filter 18 | set_fact: 19 | pyats_version: "{{ version['stdout'][0] | parse_genie(command='show version', os='ios') }}" 20 | 21 | - name: Debug Pyats facts - all 22 | debug: 23 | var: pyats_version.version 24 | 25 | - name: Debug Pyats facts - version 26 | debug: 27 | var: pyats_version.version.version 28 | 29 | - name: Debug Pyats facts - uptime 30 | debug: 31 | var: pyats_version.version.uptime 32 | -------------------------------------------------------------------------------- /ip_range.md: -------------------------------------------------------------------------------- 1 | # Reading IP address ranges 2 | 3 | ## Variables required 4 | 5 | ### Inputs 6 | 7 | JSON file [ip_addresses.json](files/ip_addresses.json). 8 | 9 | ```json 10 | { 11 | "syncToken": "0123456789", 12 | "createDate": "yyyy-mm-dd-hh-mm-ss", 13 | "prefixes": [ 14 | { 15 | "ip_prefix": "192.0.2.0/24", 16 | "region": "region", 17 | "network_border_group": "network_border_group", 18 | "service": "subset" 19 | } 20 | ], 21 | "ipv6_prefixes": [ 22 | { 23 | "ipv6_prefix": "2001:db8:cafe::/64", 24 | "region": "region", 25 | "network_border_group": "network_border_group", 26 | "service": "subset" 27 | } 28 | ] 29 | } 30 | ``` 31 | 32 | 33 | ## Playbook 34 | 35 | Latest version -> [ip_range](ip_range.yml). The following output might be outdated. 36 | 37 | ```yaml 38 | - name: Play around with IP address ranges 39 | hosts: localhost 40 | connection: local 41 | become: false 42 | gather_facts: false 43 | vars: 44 | input: "{{ lookup('file','files/ip_addresses.json') | from_json }}" 45 | test_list: ['192.0.2.18', 'host.fqdn', '::1', '192.168.32.0/24', 'fe80::100/10', 46 | '2001:db8:cafe::f00/64', True, '', '42540766412265424405338506004571095040/64'] 47 | 48 | tasks: 49 | - name: Create IPv4 List 50 | set_fact: 51 | ipv4_list: "{{ input.prefixes }}" 52 | 53 | - name: Create IPv6 List 54 | set_fact: 55 | ipv6_list: "{{ input.ipv6_prefixes }}" 56 | 57 | - name: TEST 1 58 | block: 59 | - name: Loop over IPv4 addresses 60 | debug: 61 | msg: "{{ item.ip_prefix }}" 62 | with_items: "{{ ipv4_list }}" 63 | 64 | - name: TEST 2 65 | block: 66 | - name: Print first and last ip of an IPv4 range (query by index number) 67 | debug: 68 | msg: "{{ item.ip_prefix | ipaddr('1') | ipv4('address') }}-{{ item.ip_prefix | ipaddr('-1') | ipv4('address') }}" 69 | with_items: "{{ ipv4_list }}" 70 | 71 | - name: TEST 3 72 | block: 73 | - name: Print first and last ip of an IPv6 range (query by index number) 74 | debug: 75 | msg: "{{ item.ipv6_prefix | ipaddr('1') | ipv6('address') }}-{{ item.ipv6_prefix | ipaddr('-1') | ipv6('address') }}" 76 | with_items: "{{ ipv6_list }}" 77 | 78 | - name: TEST 4 79 | block: 80 | - name: Check if values in 'test_list' are in the range of an IPv4 prefix 81 | debug: 82 | msg: "{{ test_list | ipaddr(item.ip_prefix) }}" 83 | with_items: "{{ ipv4_list }}" 84 | 85 | - name: TEST 5 86 | block: 87 | - name: Check if values in 'test_list' are in the range of an IPv6 prefix 88 | debug: 89 | msg: "{{ test_list | ipaddr(item.ipv6_prefix) }}" 90 | with_items: "{{ ipv6_list }}" 91 | ``` 92 | 93 | ## Output 94 | 95 | The following output might be outdated. 96 | 97 | ```bash 98 | ⇨ ansible-playbook ip_range.yml 99 | [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' 100 | 101 | PLAY [Play around with IP address ranges] **************************************************************************************** 102 | 103 | TASK [Create IPv4 List] ********************************************************************************************************** 104 | ok: [localhost] 105 | 106 | TASK [Create IPv6 List] ********************************************************************************************************** 107 | ok: [localhost] 108 | 109 | TASK [Loop over IPv4 addresses] ************************************************************************************************** 110 | ok: [localhost] => (item={'ip_prefix': '192.0.2.0/24', 'region': 'region', 'network_border_group': 'network_border_group', 'service': 'subset'}) => { 111 | "msg": "192.0.2.0/24" 112 | } 113 | 114 | TASK [Print first and last ip of an IPv4 range (query by index number)] ********************************************************** 115 | ok: [localhost] => (item={'ip_prefix': '192.0.2.0/24', 'region': 'region', 'network_border_group': 'network_border_group', 'service': 'subset'}) => { 116 | "msg": "192.0.2.1-192.0.2.255" 117 | } 118 | 119 | TASK [Print first and last ip of an IPv6 range (query by index number)] ********************************************************** 120 | ok: [localhost] => (item={'ipv6_prefix': '2001:db8:cafe::/64', 'region': 'region', 'network_border_group': 'network_border_group', 'service': 'subset'}) => { 121 | "msg": "2001:db8:cafe::1-2001:db8:cafe:0:ffff:ffff:ffff:ffff" 122 | } 123 | 124 | TASK [Check if values in 'test_list' are in the range of an IPv4 prefix] ********************************************************* 125 | ok: [localhost] => (item={'ip_prefix': '192.0.2.0/24', 'region': 'region', 'network_border_group': 'network_border_group', 'service': 'subset'}) => { 126 | "msg": [ 127 | "192.0.2.18" 128 | ] 129 | } 130 | 131 | TASK [Check if values in 'test_list' are in the range of an IPv6 prefix] ********************************************************* 132 | ok: [localhost] => (item={'ipv6_prefix': '2001:db8:cafe::/64', 'region': 'region', 'network_border_group': 'network_border_group', 'service': 'subset'}) => { 133 | "msg": [ 134 | "2001:db8:cafe::f00/64" 135 | ] 136 | } 137 | 138 | PLAY RECAP *********************************************************************************************************************** 139 | localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 140 | ``` 141 | 142 | -------------------------------------------------------------------------------- /ip_range.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # ansible-playbook ip_range.yml 3 | # IP range JSON example from: https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html#aws-ip-syntax 4 | 5 | - name: Play around with IP address ranges 6 | hosts: localhost 7 | connection: local 8 | become: false 9 | gather_facts: false 10 | vars: 11 | input: "{{ lookup('file','files/ip_addresses.json') | from_json }}" 12 | test_list: ['192.0.2.18', 'host.fqdn', '::1', '192.168.32.0/24', 'fe80::100/10', 13 | '2001:db8:cafe::f00/64', true, '', '42540766412265424405338506004571095040/64'] 14 | 15 | tasks: 16 | - name: Create IPv4 List 17 | set_fact: 18 | ipv4_list: "{{ input.prefixes }}" 19 | 20 | - name: Create IPv6 List 21 | set_fact: 22 | ipv6_list: "{{ input.ipv6_prefixes }}" 23 | 24 | - name: TEST 1 25 | block: 26 | - name: Loop over IPv4 addresses 27 | debug: 28 | msg: "{{ item.ip_prefix }}" 29 | with_items: "{{ ipv4_list }}" 30 | 31 | - name: TEST 2 32 | block: 33 | - name: Print first and last ip of an IPv4 range (query by index number) 34 | debug: 35 | msg: "{{ item.ip_prefix | ipaddr('1') | ipv4('address') }}-{{ item.ip_prefix | ipaddr('-1') | ipv4('address') }}" 36 | with_items: "{{ ipv4_list }}" 37 | 38 | - name: TEST 3 39 | block: 40 | - name: Print first and last ip of an IPv6 range (query by index number) 41 | debug: 42 | msg: "{{ item.ipv6_prefix | ipaddr('1') | ipv6('address') }}-{{ item.ipv6_prefix | ipaddr('-1') | ipv6('address') }}" 43 | with_items: "{{ ipv6_list }}" 44 | 45 | - name: TEST 4 46 | block: 47 | - name: Check if values in 'test_list' are in the range of an IPv4 prefix 48 | debug: 49 | msg: "{{ test_list | ipaddr(item.ip_prefix) }}" 50 | with_items: "{{ ipv4_list }}" 51 | 52 | - name: TEST 5 53 | block: 54 | - name: Check if values in 'test_list' are in the range of an IPv6 prefix 55 | debug: 56 | msg: "{{ test_list | ipaddr(item.ipv6_prefix) }}" 57 | with_items: "{{ ipv6_list }}" 58 | -------------------------------------------------------------------------------- /list.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Gig0/0/21", 4 | "enabled": false 5 | }, 6 | { 7 | "name": "Gig0/0/22", 8 | "enabled": true 9 | }, 10 | { 11 | "name": "Vlan", 12 | "enabled": false 13 | } 14 | ] -------------------------------------------------------------------------------- /meraki.md: -------------------------------------------------------------------------------- 1 | # Meraki 2 | 3 | ## Variables requiered 4 | 5 | - `api_key`: API Key. 6 | - `org_name`: Organization name, example `DevNet Sandbox`. 7 | 8 | 9 | ## Playbook 10 | 11 | Latest version -> [meraki](meraki.yml). The following output might be outdated. 12 | 13 | ```yaml 14 | - name: Meraki Test 15 | hosts: localhost 16 | gather_facts: no 17 | collections: 18 | - cisco.meraki 19 | 20 | tasks: 21 | - name: Query information about all organizations associated to the API user 22 | meraki_organization: 23 | auth_key: "{{ api_key }}" 24 | state: query 25 | delegate_to: localhost 26 | register: orgs 27 | 28 | - debug: 29 | var: orgs.data[2] 30 | 31 | - name: Query all devices in organization {{ org_name }} 32 | meraki_device: 33 | auth_key: "{{ api_key }}" 34 | org_name: "{{ org_name }}" 35 | state: query 36 | register: devices 37 | 38 | - debug: 39 | var: devices.data[16] 40 | 41 | - name: Query management information 42 | meraki_management_interface: 43 | auth_key: "{{ api_key }}" 44 | org_name: "{{ org_name }}" 45 | state: query 46 | net_id: "{{ devices.data[16].network_id }}" 47 | serial: "{{ devices.data[16].serial }}" 48 | register: mgmt 49 | 50 | - debug: 51 | var: mgmt 52 | 53 | - name: Query information about all administrators associated to the organization {{ org_name }} 54 | meraki_admin: 55 | auth_key: "{{ api_key }}" 56 | org_name: "{{ org_name }}" 57 | state: query 58 | register: admins 59 | 60 | - debug: 61 | msg: " {{ admins.data[3].name }} at {{ admins.data[3].email }} has API Key -> {{ admins.data[3].has_api_key }}" 62 | 63 | - name: Query SNMP settings in the {{ org_name }} organization 64 | meraki_snmp: 65 | auth_key: "{{ api_key }}" 66 | org_name: "{{ org_name }}" 67 | state: query 68 | register: snmp 69 | 70 | - debug: 71 | var: snmp 72 | 73 | - name: List all networks associated to the {{ org_name }} organization 74 | meraki_network: 75 | auth_key: "{{ api_key }}" 76 | org_name: "{{ org_name }}" 77 | state: query 78 | register: nets 79 | 80 | - name: Query network named {{ nets.data[0].name }} in the {{ org_name }} organization 81 | meraki_network: 82 | auth_key: "{{ api_key }}" 83 | org_name: "{{ org_name }}" 84 | net_name: "{{ nets.data[0].name }}" 85 | state: query 86 | register: net 87 | 88 | - name: Query syslog configurations on network named {{ nets.data[0].name }} in the {{ org_name }} organization 89 | meraki_syslog: 90 | auth_key: "{{ api_key }}" 91 | org_name: "{{ org_name }}" 92 | net_name: "{{ nets.data[0].name }}" 93 | state: query 94 | register: syslog 95 | 96 | - debug: 97 | var: syslog 98 | 99 | - name: List SSID(s) on network {{ nets.data[0].name }} 100 | meraki_ssid: 101 | auth_key: "{{ api_key }}" 102 | org_name: "{{ org_name }}" 103 | net_name: "{{ nets.data[0].name }}" 104 | state: query 105 | register: ssids 106 | 107 | - debug: 108 | var: ssids.data[0] 109 | ``` 110 | 111 | ## Output 112 | 113 | The following output might be outdated. 114 | 115 | ```bash 116 | ⇨ ansible-playbook meraki.yml 117 | [WARNING]: No inventory was parsed, only implicit localhost is available 118 | [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 119 | 'all' 120 | 121 | PLAY [Meraki Test] ******************************************************************************************************* 122 | 123 | TASK [Query information about all organizations associated to the API user] ********************************************** 124 | ok: [localhost -> localhost] 125 | 126 | TASK [debug] ************************************************************************************************************* 127 | ok: [localhost] => { 128 | "orgs.data[2]": { 129 | "id": "549236", 130 | "name": "DevNet Sandbox", 131 | "url": "https://n149.meraki.com/o/-t35Mb/manage/organization/overview" 132 | } 133 | } 134 | 135 | TASK [Query all devices in organization DevNet Sandbox] ****************************************************************** 136 | ok: [localhost] 137 | 138 | TASK [debug] ************************************************************************************************************* 139 | ok: [localhost] => { 140 | "devices.data[16]": { 141 | "claimed_at": "1519658520.55085", 142 | "mac": "e0:55:3d:17:d4:23", 143 | "model": "MX65", 144 | "name": "", 145 | "network_id": "L_646829496481105433", 146 | "public_ip": "64.103.26.57", 147 | "serial": "Q2QN-9J8L-SLPD" 148 | } 149 | } 150 | 151 | TASK [Query management information] ************************************************************************************** 152 | ok: [localhost] 153 | 154 | TASK [debug] ************************************************************************************************************* 155 | ok: [localhost] => { 156 | "mgmt": { 157 | "changed": false, 158 | "data": { 159 | "ddns_hostnames": { 160 | "active_ddns_hostname": "devnet-sandbox-always-on-wired-vzhddpprjp.dynamic-m.com", 161 | "ddns_hostname_wan1": "devnet-sandbox-always-on-wired-vzhddpprjp-1.dynamic-m.com", 162 | "ddns_hostname_wan2": "devnet-sandbox-always-on-wired-vzhddpprjp-2.dynamic-m.com" 163 | }, 164 | "wan1": { 165 | "using_static_ip": false, 166 | "vlan": null, 167 | "wan_enabled": "not configured" 168 | }, 169 | "wan2": { 170 | "using_static_ip": false, 171 | "vlan": null, 172 | "wan_enabled": "not configured" 173 | } 174 | }, 175 | "failed": false, 176 | "response": "OK (unknown bytes)", 177 | "status": 200 178 | } 179 | } 180 | 181 | TASK [Query information about all administrators associated to the organization DevNet Sandbox] ************************** 182 | ok: [localhost] 183 | 184 | TASK [debug] ************************************************************************************************************* 185 | ok: [localhost] => { 186 | "msg": " devnetmerakiadmin at devnetmerakiadmin@cisco.com has API Key -> True" 187 | } 188 | 189 | TASK [Query SNMP settings in the DevNet Sandbox organization] ************************************************************ 190 | ok: [localhost] 191 | 192 | TASK [debug] ************************************************************************************************************* 193 | ok: [localhost] => { 194 | "snmp": { 195 | "changed": false, 196 | "data": { 197 | "hostname": "snmp.meraki.com", 198 | "peer_ips": null, 199 | "port": 16100, 200 | "v2c_enabled": false, 201 | "v3_auth_mode": "SHA", 202 | "v3_enabled": false, 203 | "v3_priv_mode": "AES128" 204 | }, 205 | "failed": false, 206 | "response": "OK (unknown bytes)", 207 | "status": 200 208 | } 209 | } 210 | 211 | TASK [List all networks associated to the DevNet Sandbox organization] *************************************************** 212 | ok: [localhost] 213 | 214 | TASK [Query network named DevNet Sandbox ALWAYS ON in the DevNet Sandbox organization] *********************************** 215 | ok: [localhost] 216 | 217 | TASK [Query syslog configurations on network named DevNet Sandbox ALWAYS ON in the DevNet Sandbox organization] ********** 218 | ok: [localhost] 219 | 220 | TASK [debug] ************************************************************************************************************* 221 | ok: [localhost] => { 222 | "syslog": { 223 | "changed": false, 224 | "data": [], 225 | "failed": false, 226 | "response": "OK (unknown bytes)", 227 | "status": 200 228 | } 229 | } 230 | 231 | TASK [List SSID(s) on network DevNet Sandbox ALWAYS ON] ****************************************************************** 232 | ok: [localhost] 233 | 234 | TASK [debug] ************************************************************************************************************* 235 | ok: [localhost] => { 236 | "ssids.data[0]": { 237 | "auth_mode": "open", 238 | "availability_tags": [], 239 | "available_on_all_aps": true, 240 | "band_selection": "Dual band operation", 241 | "enabled": true, 242 | "ip_assignment_mode": "NAT mode", 243 | "min_bitrate": 11, 244 | "name": "DevNet Sandbox ALWAYS ON - wirel", 245 | "number": 0, 246 | "per_client_bandwidth_limit_down": 0, 247 | "per_client_bandwidth_limit_up": 0, 248 | "radius_accounting_enabled": null, 249 | "splash_page": "None", 250 | "ssid_admin_accessible": false, 251 | "visible": true 252 | } 253 | } 254 | 255 | PLAY RECAP *************************************************************************************************************** 256 | localhost : ok=16 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 257 | ``` 258 | 259 | -------------------------------------------------------------------------------- /meraki.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # https://github.com/CiscoDevNet/ansible-meraki 3 | # https://galaxy.ansible.com/cisco/meraki 4 | # ansible-galaxy collection install cisco.meraki 5 | # ansible-playbook meraki.yml 6 | 7 | - name: Meraki Test 8 | hosts: localhost 9 | gather_facts: false 10 | 11 | tasks: 12 | - name: Query information about all organizations associated to the API user 13 | cisco.meraki.meraki_organization: 14 | auth_key: "{{ api_key }}" 15 | state: query 16 | delegate_to: localhost 17 | register: orgs 18 | 19 | - name: Print name of all available organizations 20 | ansible.builtin.debug: 21 | var: item.name 22 | loop: "{{ orgs.data }}" 23 | 24 | - name: Select one organization 25 | ansible.builtin.set_fact: 26 | organization: "{{ orgs.data[0].name }}" 27 | cacheable: yes 28 | 29 | - name: Print orn name test {{ organization }} 30 | ansible.builtin.debug: 31 | var: organization 32 | 33 | - name: Query all devices in organization {{ org_name }} 34 | cisco.meraki.meraki_device: 35 | auth_key: "{{ api_key }}" 36 | org_name: "{{ org_name }}" 37 | state: query 38 | register: devices 39 | 40 | - name: Print all devices in organization {{ org_name }} 41 | ansible.builtin.debug: 42 | var: devices 43 | 44 | # - name: Query management information 45 | # cisco.meraki.meraki_management_interface: 46 | # auth_key: "{{ api_key }}" 47 | # org_name: "{{ org_name }}" 48 | # state: query 49 | # net_id: "{{ devices.data[16].network_id }}" 50 | # serial: "{{ devices.data[16].serial }}" 51 | # register: mgmt 52 | 53 | # - ansible.builtin.debug: 54 | # var: mgmt 55 | 56 | # - name: Create a new organization named {{ org_name }} 57 | # meraki_organization: 58 | # auth_key: "{{ api_key }}" 59 | # org_name: "{{ org_name }}" 60 | # state: present 61 | # delegate_to: localhost 62 | 63 | - name: Query information about all administrators associated to the organization {{ org_name }} 64 | cisco.meraki.meraki_admin: 65 | auth_key: "{{ api_key }}" 66 | org_name: "{{ org_name }}" 67 | state: query 68 | register: admins 69 | 70 | - ansible.builtin.debug: 71 | msg: "{{ admins }}" 72 | # msg: " {{ admins.data[3].name }} at {{ admins.data[3].email }} has API Key -> {{ admins.data[3].has_api_key }}" 73 | 74 | - name: Query SNMP settings in the {{ org_name }} organization 75 | cisco.meraki.meraki_snmp: 76 | auth_key: "{{ api_key }}" 77 | org_name: "{{ org_name }}" 78 | state: query 79 | register: snmp 80 | 81 | - ansible.builtin.debug: 82 | var: snmp 83 | 84 | - name: List all networks associated to the {{ org_name }} organization 85 | cisco.meraki.meraki_network: 86 | auth_key: "{{ api_key }}" 87 | org_name: "{{ org_name }}" 88 | state: query 89 | register: nets 90 | 91 | - name: Print out one network 92 | ansible.builtin.debug: 93 | var: nets.data[0] 94 | 95 | - name: Query network named {{ nets.data[0].name }} in the {{ org_name }} organization 96 | cisco.meraki.meraki_network: 97 | auth_key: "{{ api_key }}" 98 | org_name: "{{ org_name }}" 99 | net_name: "{{ nets.data[0].name }}" 100 | state: query 101 | register: net 102 | 103 | - name: Print out network data 104 | ansible.builtin.debug: 105 | var: net 106 | 107 | - name: Query syslog configurations on network named {{ nets.data[0].name }} in the {{ org_name }} organization 108 | cisco.meraki.meraki_syslog: 109 | auth_key: "{{ api_key }}" 110 | org_name: "{{ org_name }}" 111 | net_name: "{{ nets.data[0].name }}" 112 | state: query 113 | register: syslog 114 | 115 | - ansible.builtin.debug: 116 | var: syslog 117 | 118 | # - name: Enable VLANs on network {{ nets.data[0].name }} 119 | # cisco.meraki.meraki_network: 120 | # auth_key: "{{ api_key }}" 121 | # org_name: "{{ org_name }}" 122 | # net_name: "{{ nets.data[0].name }}" 123 | # state: query 124 | # enable_vlans: yes 125 | 126 | # - name: Query all VLANs on network {{ nets.data[0].name }} 127 | # cisco.meraki.meraki_vlan: 128 | # auth_key: "{{ api_key }}" 129 | # org_name: "{{ org_name }}" 130 | # net_name: "{{ nets.data[0].name }}" 131 | # state: query 132 | # register: vlans 133 | 134 | # - ansible.builtin.debug: 135 | # var: vlans 136 | 137 | - name: List SSID(s) on network {{ nets.data[0].name }} 138 | cisco.meraki.meraki_ssid: 139 | auth_key: "{{ api_key }}" 140 | org_name: "{{ org_name }}" 141 | net_name: "{{ nets.data[0].name }}" 142 | state: query 143 | register: ssids 144 | 145 | - ansible.builtin.debug: 146 | var: ssids.data[0] 147 | 148 | # - name: Enable click-through splash page on {{ ssids.data[0].name }} 149 | # cisco.meraki.meraki_ssid: 150 | # auth_key: "{{ api_key }}" 151 | # org_name: "{{ org_name }}" 152 | # net_name: "{{ nets.data[0].name }}" 153 | # state: present 154 | # name: "{{ ssids.data[0].name }}" 155 | # splash_page: Click-through splash page 156 | 157 | 158 | # "orgs": { 159 | # "changed": false, 160 | # "data": [ 161 | # { 162 | # "id": "681155", 163 | # "name": "DeLab", 164 | # "url": "https://n6.meraki.com/o/49Gm_c/manage/organization/overview" 165 | # }, 166 | # { 167 | # "id": "566327653141842188", 168 | # "name": "DevNetAssoc", 169 | # "url": "https://n6.meraki.com/o/dcGsWag/manage/organization/overview" 170 | # }, 171 | # { 172 | # "id": "549236", 173 | # "name": "DevNet Sandbox", 174 | # "url": "https://n149.meraki.com/o/-t35Mb/manage/organization/overview" 175 | # }, 176 | # { 177 | # "id": "52636", 178 | # "name": "Forest City - Other", 179 | # "url": "https://n42.meraki.com/o/E_utnd/manage/organization/overview" 180 | # }, 181 | # { 182 | # "id": "865776", 183 | # "name": "Cisco Live US 2019", 184 | # "url": "https://n22.meraki.com/o/CVQqTb/manage/organization/overview" 185 | # }, 186 | # { 187 | # "id": "463308", 188 | # "name": "DevNet San Jose", 189 | # "url": "https://n18.meraki.com/o/vB2D8a/manage/organization/overview" 190 | # } 191 | # ], 192 | # "failed": false, 193 | # "response": "OK (unknown bytes)", 194 | # "status": 200 195 | # } 196 | -------------------------------------------------------------------------------- /multi-line-config.md: -------------------------------------------------------------------------------- 1 | # Multi-line Config 2 | 3 | ## Variables required 4 | 5 | - `my_devices`: One or more groups or host patterns, separated by colons. 6 | - `my_facts`: Whether to collect facts per device: `yes` or `no`. 7 | - `my_config`: Config to apply. Ex: 8 | 9 | ```yaml 10 | my_config: | 11 | ip access-list standard TEST_ACL 12 | permit 192.0.2.1 13 | remark doc-ip 14 | ``` 15 | 16 | ## Playbook 17 | 18 | Latest version -> [collect-command](collect-command.yml). The following output might be outdated. 19 | 20 | ```yaml 21 | hosts: "{{ my_devices }}" 22 | gather_facts: "{{ my_facts }}" 23 | 24 | tasks: 25 | - name: multiline config 26 | cli_config: 27 | config: "{{ my_config }}" 28 | ``` 29 | 30 | ## Output 31 | 32 | The following output might be outdated. 33 | 34 | ```bash 35 | SSH password: 36 | BECOME password[defaults to SSH password]: 37 | 38 | PLAY [ios] ********************************************************************* 39 | 40 | TASK [multi-line config] ******************************************************* 41 | ip access-list standard TEST_ACL 42 | permit 192.0.2.1 43 | remark doc-ip 44 | changed: [18.208.199.107] 45 | 46 | PLAY RECAP ********************************************************************* 47 | 18.208.199.107 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 48 | ``` 49 | 50 | -------------------------------------------------------------------------------- /multi-line-config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - 4 | hosts: "{{ my_devices }}" 5 | gather_facts: "{{ my_facts }}" 6 | 7 | tasks: 8 | - name: multi-line config 9 | cli_config: 10 | config: "{{ my_config }}" 11 | # config: | 12 | # ipv4 access-list TEST_ACL 13 | # permit 192.0.2.1 14 | # remark doc-ip 15 | -------------------------------------------------------------------------------- /network-restore.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restore router configurations 3 | hosts: routers 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: Do nothing for now 8 | debug: 9 | msg: "Coming soon" 10 | 11 | # - name: load restore role 12 | # include_role: 13 | # name: restore 14 | -------------------------------------------------------------------------------- /network_backup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: setup control node timestamp 3 | hosts: ansible 4 | gather_facts: false 5 | become: true 6 | tasks: 7 | - name: create time stamp for play 8 | set_fact: 9 | datetime: "{{ lookup('pipe','date +%Y-%m-%d-%H-%M') }}" 10 | 11 | # - name: create /backup directory on control node 12 | # file: 13 | # path: /backup 14 | # state: directory 15 | 16 | # - name: create timestamp directory 17 | # file: 18 | # path: "/backup/{{datetime}}" 19 | # state: directory 20 | 21 | - name: retrieve router configurations 22 | hosts: "{{ devices }}" 23 | gather_facts: false 24 | 25 | tasks: 26 | - name: backup configuration 27 | include_role: 28 | name: backup 29 | when: ansible_network_os is defined 30 | 31 | # - name: SAVE CONFIGURATION LOCALLY 32 | # vars: 33 | # ansible_connection: ssh 34 | # copy: 35 | # src: "{{ config_output.backup_path }}" 36 | # dest: "/backup/{{hostvars['ansible'].datetime}}/{{inventory_hostname}}" 37 | # delegate_to: ansible 38 | # when: config_output is defined 39 | # become: yes 40 | 41 | - name: SAVE CONFIGURATION to S3 42 | aws_s3: 43 | bucket: mydemo.run 44 | src: "{{ config_output.backup_path }}" 45 | object: "/configs/{{ hostvars['ansible'].datetime }}/{{ inventory_hostname }}.txt" 46 | mode: put 47 | metadata: 'Content-Type=text/plain' 48 | when: config_output is defined 49 | 50 | - name: print config URL 51 | debug: 52 | msg: "{{ inventory_hostname }} config is at http://mydemo.run/configs/{{ hostvars['ansible'].datetime }}/{{ inventory_hostname }}.txt" 53 | when: config_output is defined 54 | 55 | # - name: backup router configurations 56 | # hosts: ansible 57 | # gather_facts: no 58 | # tasks: 59 | # - name: find backups 60 | # find: 61 | # paths: /backup 62 | # file_type: directory 63 | # register: backups 64 | # run_once: true 65 | # become: yes 66 | 67 | # - name: create restore job template 68 | # tower_job_template: 69 | # name: "Network-Restore" 70 | # job_type: "run" 71 | # inventory: "Workshop Inventory" 72 | # project: "Workshop Project" 73 | # playbook: "network_restore.yml" 74 | # credential: "Workshop Credential" 75 | # survey_enabled: true 76 | # survey_spec: "{{ lookup('template', '{{playbook_dir}}/network_setup/templates/backup.j2') }}" 77 | # validate_certs: no 78 | # tower_username: "{{ lookup('env', 'TOWER_USERNAME') }}" 79 | # tower_password: "{{ lookup('env', 'TOWER_PASSWORD') }}" 80 | # tower_host: "{{ lookup('env', 'TOWER_HOST') }}" 81 | -------------------------------------------------------------------------------- /ntp-compliance-email.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: NTP Server configuration compliance for Network Elements 3 | hosts: "{{ my_devices }}" 4 | connection: network_cli 5 | gather_facts: "{{ my_facts }}" 6 | vars: 7 | erase: false 8 | make_changes: false 9 | required_servers: [] 10 | in_servers: [] 11 | out_servers: [] 12 | 13 | tasks: 14 | - name: Read inputs and prepare configs 15 | set_fact: 16 | required_servers: "{{ required_servers }} + [ 'ntp server {{ item }}' ]" 17 | with_items: "{{ my_ntp_servers }}" 18 | 19 | - name: Check existing NTP Servers 20 | include_role: 21 | name: ntpcheck 22 | when: ansible_network_os is defined 23 | 24 | # Adds NTP server entries in the my_ntp_servers variable if the variable erase is false 25 | - block: 26 | - name: Compare NTP servers and remove erroneous entries 27 | cli_config: 28 | config: no {{ item }} 29 | loop: "{{ configured_servers }}" 30 | when: 31 | - configured_servers | length > 0 32 | - item not in required_servers 33 | 34 | - name: Ensure intended NTP servers are present 35 | cli_config: 36 | config: "{{ item }}" 37 | loop: "{{ required_servers }}" 38 | when: 39 | - not erase|bool 40 | - make_changes 41 | 42 | # - name: Save template to temporary file 43 | # debug: 44 | # msg: "{{ hostvars }}" 45 | 46 | - name: Save template to temporary file 47 | template: 48 | src: report.j2 49 | dest: ./temp.html 50 | mode: '0755' 51 | when: in_servers | length > 0 or out_servers | length > 0 52 | 53 | - name: Send report e-mail using SendGrid 54 | sendgrid: 55 | api_key: "{{ sendgrid_api_key }}" 56 | from_address: "{{ sendgrid_from_address }}" 57 | to_addresses: "{{ sendgrid_to_address }}" 58 | subject: "{{ sendgrid_email_subject }}" 59 | body: "{{ lookup('template', 'report.j2') }}" 60 | html_body: true 61 | delegate_to: localhost 62 | run_once: true 63 | 64 | # Erases all ntp server entries if the variable erase is true 65 | - name: Remove all existing NTP server entries 66 | cli_config: 67 | config: no {{ item }} 68 | loop: "{{ configured_servers }}" 69 | when: 70 | - configured_servers | length > 0 71 | - erase|bool 72 | -------------------------------------------------------------------------------- /ntp-compliance.md: -------------------------------------------------------------------------------- 1 | # NTP Compliance 2 | 3 | ## Pre-requisites 4 | 5 | The boto package is required. 6 | 7 | ``` 8 | sudo pip3 install boto3 9 | ``` 10 | 11 | You need AWS credentials to create a Compliance report on S3. You need to export `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` before executing. 12 | 13 | ## Variables required 14 | 15 | - `my_devices`: One or more groups or host patterns, separated by colons. 16 | - `my_facts`: Whether to collect facts per device: `yes` or `no`. 17 | - `my_bucket`: S3 bucket where to host the report. Ex `mydemo.run`. 18 | - `my_ntp_servers`: List of NTP server IP addresses. 19 | 20 | ## Playbook 21 | 22 | Latest version -> [ios-genie-show-ver][1]. The following output might be outdated. 23 | 24 | ```yaml 25 | hosts: "{{ my_devices }}" 26 | gather_facts: "{{ my_facts }}" 27 | 28 | tasks: 29 | - name: Check existing NTP Servers 30 | include_role: 31 | name: ntpcheck 32 | when: ansible_network_os is defined 33 | 34 | # Adds NTP server entries in the my_ntp_servers variable if the variable erase is false 35 | - block: 36 | - name: Compare NTP servers and remove erroneous entries 37 | cli_config: 38 | config: no {{ item }} 39 | loop: "{{ configured_servers }}" 40 | when: 41 | - configured_servers | length > 0 42 | - item not in required_servers 43 | 44 | - name: Ensure intended NTP servers are present 45 | cli_config: 46 | config: "{{ item }}" 47 | loop: "{{ required_servers }}" 48 | when: not erase|bool 49 | 50 | - name: Save template to temporary file 51 | template: 52 | src: report.j2 53 | dest: ./temp.html 54 | mode: '0755' 55 | when: in_servers | length > 0 or out_servers | length > 0 56 | 57 | # Creates and uploads a report to S3 58 | - name: Upload report to S3 59 | aws_s3: 60 | bucket: "{{ my_bucket }}" 61 | src: "./temp.html" 62 | object: "index.html" 63 | mode: put 64 | metadata: 'Content-Type=text/html' 65 | when: in_servers | length > 0 or out_servers | length > 0 66 | ``` 67 | 68 | ## Output 69 | 70 | The following output might be outdated. 71 | 72 | ```bash 73 | ⇨ ansible-playbook -i hosts ntp-compliance.yml --extra-vars='{"my_devices": "ios, iosxr", "my_facts": no, "my_bucket": "mydemo.run", "my_ntp_servers": [129.6.15.33, 132.163.96.5]}' 74 | 75 | PLAY [NTP Server configuration compliance for Network Elements] ************************************************************************************************************************* 76 | 77 | TASK [Read inputs and prepare configs] ************************************************************************************************************************************************** 78 | ok: [ios-xe-mgmt-latest.cisco.com] => (item=None) 79 | ok: [sbx-iosxr-mgmt.cisco.com] => (item=None) 80 | ok: [ios-xe-mgmt-latest.cisco.com] => (item=None) 81 | ok: [ios-xe-mgmt-latest.cisco.com] 82 | ok: [sbx-iosxr-mgmt.cisco.com] => (item=None) 83 | ok: [sbx-iosxr-mgmt.cisco.com] 84 | 85 | TASK [Check existing NTP Servers] ******************************************************************************************************************************************************* 86 | 87 | TASK [ntpcheck : Check NTP config per vendor OS] **************************************************************************************************************************************** 88 | included: /home/nleiva/Ansible/ansible-networking/roles/ntpcheck/tasks/ios.yml for ios-xe-mgmt-latest.cisco.com 89 | included: /home/nleiva/Ansible/ansible-networking/roles/ntpcheck/tasks/iosxr.yml for sbx-iosxr-mgmt.cisco.com 90 | 91 | TASK [ntpcheck : Get current NTP servers [IOS]] ***************************************************************************************************************************************** 92 | ok: [ios-xe-mgmt-latest.cisco.com] 93 | 94 | TASK [ntpcheck : Remove non config lines [IOS]] ***************************************************************************************************************************************** 95 | ok: [ios-xe-mgmt-latest.cisco.com] 96 | 97 | TASK [ntpcheck : Print current NTP servers [IOS]] *************************************************************************************************************************************** 98 | ok: [ios-xe-mgmt-latest.cisco.com] => { 99 | "configured_servers": [ 100 | "ntp server 129.6.15.32", 101 | "ntp server 132.163.96.6" 102 | ] 103 | } 104 | 105 | TASK [ntpcheck : Generate data for reporting] ******************************************************************************************************************************************* 106 | included: /home/nleiva/Ansible/ansible-networking/roles/ntpcheck/tasks/report/data.yml for ios-xe-mgmt-latest.cisco.com 107 | 108 | TASK [ntpcheck : Determine configuration delta for reporting [ios]] ********************************************************************************************************************* 109 | ok: [ios-xe-mgmt-latest.cisco.com] 110 | 111 | TASK [ntpcheck : Create list of NEW servers to configure [ios]] ************************************************************************************************************************* 112 | ok: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 129.6.15.33) 113 | ok: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 132.163.96.5) 114 | 115 | TASK [ntpcheck : Create list of servers to remove [ios]] ******************************************************************************************************************************** 116 | ok: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 129.6.15.32) 117 | ok: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 132.163.96.6) 118 | 119 | TASK [ntpcheck : Print out fidings for reporting [ios]] ********************************************************************************************************************************* 120 | ok: [ios-xe-mgmt-latest.cisco.com] => { 121 | "msg": [ 122 | "We are missing the following NTP Servers in IOS: ['129.6.15.33', '132.163.96.5']", 123 | "We will delete these NTP Servers in IOS: ['129.6.15.32', '132.163.96.6']" 124 | ] 125 | } 126 | 127 | TASK [ntpcheck : Get current NTP servers [IOS XR]] ************************************************************************************************************************************** 128 | ok: [sbx-iosxr-mgmt.cisco.com] 129 | 130 | TASK [ntpcheck : Remove non config lines [IOS XR]] ************************************************************************************************************************************** 131 | ok: [sbx-iosxr-mgmt.cisco.com] 132 | 133 | TASK [ntpcheck : Print current NTP servers [IOS XR]] ************************************************************************************************************************************ 134 | ok: [sbx-iosxr-mgmt.cisco.com] => { 135 | "configured_servers": [ 136 | "ntp server 129.6.15.32", 137 | "ntp server 132.163.96.6" 138 | ] 139 | } 140 | 141 | TASK [ntpcheck : Generate data for reporting] ******************************************************************************************************************************************* 142 | included: /home/nleiva/Ansible/ansible-networking/roles/ntpcheck/tasks/report/data.yml for sbx-iosxr-mgmt.cisco.com 143 | 144 | TASK [ntpcheck : Determine configuration delta for reporting [iosxr]] ******************************************************************************************************************* 145 | ok: [sbx-iosxr-mgmt.cisco.com] 146 | 147 | TASK [ntpcheck : Create list of NEW servers to configure [iosxr]] *********************************************************************************************************************** 148 | ok: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 129.6.15.33) 149 | ok: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 132.163.96.5) 150 | 151 | TASK [ntpcheck : Create list of servers to remove [iosxr]] ****************************************************************************************************************************** 152 | ok: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 129.6.15.32) 153 | ok: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 132.163.96.6) 154 | 155 | TASK [ntpcheck : Print out fidings for reporting [iosxr]] ******************************************************************************************************************************* 156 | ok: [sbx-iosxr-mgmt.cisco.com] => { 157 | "msg": [ 158 | "We are missing the following NTP Servers in IOSXR: ['129.6.15.33', '132.163.96.5']", 159 | "We will delete these NTP Servers in IOSXR: ['129.6.15.32', '132.163.96.6']" 160 | ] 161 | } 162 | 163 | TASK [Compare NTP servers and remove erroneous entries] ********************************************************************************************************************************* 164 | changed: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 129.6.15.32) 165 | changed: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 129.6.15.32) 166 | changed: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 132.163.96.6) 167 | changed: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 132.163.96.6) 168 | 169 | TASK [Ensure intended NTP servers are present] ****************************************************************************************************************************************** 170 | changed: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 129.6.15.33) 171 | changed: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 129.6.15.33) 172 | changed: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 132.163.96.5) 173 | changed: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 132.163.96.5) 174 | 175 | TASK [Save template to temporary file] ************************************************************************************************************************************************** 176 | changed: [ios-xe-mgmt-latest.cisco.com] 177 | changed: [sbx-iosxr-mgmt.cisco.com] 178 | 179 | TASK [Upload report to S3] ************************************************************************************************************************************************************** 180 | changed: [ios-xe-mgmt-latest.cisco.com] 181 | changed: [sbx-iosxr-mgmt.cisco.com] 182 | 183 | TASK [Remove all existing NTP server entries] ******************************************************************************************************************************************* 184 | skipping: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 129.6.15.32) 185 | skipping: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 129.6.15.32) 186 | skipping: [ios-xe-mgmt-latest.cisco.com] => (item=ntp server 132.163.96.6) 187 | skipping: [sbx-iosxr-mgmt.cisco.com] => (item=ntp server 132.163.96.6) 188 | 189 | PLAY RECAP ****************************************************************************************************************************************************************************** 190 | ios-xe-mgmt-latest.cisco.com : ok=14 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 191 | sbx-iosxr-mgmt.cisco.com : ok=14 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 192 | 193 | ``` 194 | 195 | The report: 196 | 197 | ![NTP report][2] 198 | 199 | 200 | [1]: ios-genie-show-ver.yml 201 | [2]: files/pictures/ntp-report.png -------------------------------------------------------------------------------- /ntp-compliance.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: NTP Server configuration compliance for Network Elements 3 | hosts: "{{ my_devices }}" 4 | connection: network_cli 5 | gather_facts: "{{ my_facts }}" 6 | vars: 7 | erase: false 8 | required_servers: [] 9 | in_servers: [] 10 | out_servers: [] 11 | ee_folder: /tmp 12 | 13 | tasks: 14 | - name: Read inputs and prepare configs 15 | ansible.builtin.set_fact: 16 | required_servers: "{{ required_servers }} + [ 'ntp server {{ item }}' ]" 17 | with_items: "{{ my_ntp_servers }}" 18 | 19 | - name: Check existing NTP Servers 20 | ansible.builtin.include_role: 21 | name: ntpcheck 22 | when: ansible_network_os is defined 23 | 24 | # Adds NTP server entries in the my_ntp_servers variable if the variable erase is false 25 | - block: 26 | - name: Compare NTP servers and remove erroneous entries 27 | ansible.netcommon.cli_config: 28 | config: no {{ item }} 29 | loop: "{{ configured_servers }}" 30 | when: 31 | - configured_servers | length > 0 32 | - item not in required_servers 33 | 34 | - name: Ensure intended NTP servers are present 35 | ansible.netcommon.cli_config: 36 | config: "{{ item }}" 37 | loop: "{{ required_servers }}" 38 | when: not erase|bool 39 | 40 | # - name: Render template 41 | # set_fact: 42 | # rendered_template: "{{ lookup('template', 'report.j2') }}" 43 | 44 | - name: Save template to temporary file 45 | ansible.builtin.template: 46 | src: report.j2 47 | dest: "{{ ee_folder }}/temp.html" 48 | mode: '0755' 49 | when: in_servers | length > 0 or out_servers | length > 0 50 | 51 | # Creates and uploads a report to S3 52 | - name: Upload report to S3 53 | amazon.aws.aws_s3: 54 | bucket: "{{ my_bucket }}" 55 | src: "{{ ee_folder }}/temp.html" 56 | object: "index.html" 57 | mode: put 58 | metadata: 'Content-Type=text/html' 59 | when: in_servers | length > 0 or out_servers | length > 0 60 | 61 | # Erases all ntp server entries if the variable erase is true 62 | - name: Remove all existing NTP server entries 63 | ansible.netcommon.cli_config: 64 | config: no {{ item }} 65 | loop: "{{ configured_servers }}" 66 | when: 67 | - configured_servers | length > 0 68 | - erase|bool 69 | -------------------------------------------------------------------------------- /roles/backup/tasks/eos.yml: -------------------------------------------------------------------------------- 1 | - name: BACKUP THE CONFIG [{{ ansible_network_os | default("unknown OS") }}] 2 | eos_config: 3 | backup: yes 4 | register: config_output 5 | -------------------------------------------------------------------------------- /roles/backup/tasks/ios.yml: -------------------------------------------------------------------------------- 1 | # This task will backup the configuration 2 | - name: BACKUP THE CONFIG [{{ ansible_network_os | default("unknown OS") }}] 3 | ios_config: 4 | backup: yes 5 | register: config_output 6 | 7 | # This task removes lines from the Current configuration... from the top of IOS routers show run 8 | - name: REMOVE NON CONFIG LINES - LINE [{{ ansible_network_os | default("unknown OS") }}] 9 | lineinfile: 10 | path: "{{ config_output.backup_path }}" 11 | line: "Building configuration..." 12 | state: absent 13 | -------------------------------------------------------------------------------- /roles/backup/tasks/iosxr.yml: -------------------------------------------------------------------------------- 1 | # This task will backup the configuration 2 | - name: BACKUP THE CONFIG [{{ ansible_network_os | default("unknown OS") }}] 3 | iosxr_config: 4 | backup: yes 5 | register: config_output 6 | 7 | # This task removes lines from the Current configuration... from the top of NX-OS device show run 8 | - name: REMOVE NON CONFIG LINES - REGEXP [{{ ansible_network_os | default("unknown OS") }}] 9 | lineinfile: 10 | path: "{{ config_output.backup_path }}" 11 | regexp: "{{ item.regexp }}" 12 | state: absent 13 | with_items: 14 | - { regexp: 'Building configuration...' } 15 | - { regexp: '!! IOS XR Configuration version' } 16 | - { regexp: '!! Last configuration change at' } -------------------------------------------------------------------------------- /roles/backup/tasks/junos.yml: -------------------------------------------------------------------------------- 1 | - name: ENSURE NETCONF IS RUNNING [{{ ansible_network_os | default("unknown OS") }}] 2 | vars: 3 | ansible_connection: network_cli 4 | junos_netconf: 5 | 6 | - name: BACKUP THE CONFIG [{{ ansible_network_os | default("unknown OS") }}] 7 | vars: 8 | ansible_connection: netconf 9 | junos_config: 10 | backup: yes 11 | register: config_output -------------------------------------------------------------------------------- /roles/backup/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: backup network device config [{{ ansible_network_os | default("unknown OS") }}] 2 | include_tasks: "{{ ansible_network_os }}.yml" 3 | -------------------------------------------------------------------------------- /roles/backup/tasks/nxos.yml: -------------------------------------------------------------------------------- 1 | # This task will backup the configuration 2 | - name: BACKUP THE CONFIG [{{ ansible_network_os | default("unknown OS") }}] 3 | nxos_config: 4 | backup: yes 5 | register: config_output 6 | 7 | # This task removes lines from the Current configuration... from the top of NX-OS device show run 8 | - name: REMOVE NON CONFIG LINES - REGEXP [{{ ansible_network_os | default("unknown OS") }}] 9 | lineinfile: 10 | path: "{{ config_output.backup_path }}" 11 | regexp: "{{ item.regexp }}" 12 | state: absent 13 | with_items: 14 | - { regexp: '!Command: show running-config' } 15 | - { regexp: '!Running configuration last done at:' } 16 | - { regexp: '!Time:' } -------------------------------------------------------------------------------- /roles/ntpcheck/tasks/ios.yml: -------------------------------------------------------------------------------- 1 | - name: Get current NTP servers [IOS] 2 | ansible.netcommon.cli_command: 3 | command: show run | i ntp server 4 | register: output 5 | 6 | - name: Remove non config lines [IOS] 7 | ansible.builtin.set_fact: 8 | configured_servers: "{{ output.stdout_lines | difference(dummy_list) }}" 9 | vars: 10 | dummy_list: 11 | - "Building configuration..." 12 | 13 | - name: Print current NTP servers [IOS] 14 | ansible.builtin.debug: 15 | var: configured_servers 16 | 17 | ## Generate report data 18 | - name: Generate data for reporting 19 | ansible.builtin.include_tasks: 'report/data.yml' 20 | -------------------------------------------------------------------------------- /roles/ntpcheck/tasks/iosxr.yml: -------------------------------------------------------------------------------- 1 | - name: Get current NTP servers [IOS XR] 2 | ansible.netcommon.cli_command: 3 | command: show run formal | i ntp server 4 | register: output 5 | 6 | - name: Remove non config lines [IOS XR] 7 | ansible.builtin.set_fact: 8 | configured_servers: "{{ output.stdout_lines | difference(dummy_list) }}" 9 | vars: 10 | dummy_list: 11 | - "Building configuration..." 12 | 13 | - name: Print current NTP servers [IOS XR] 14 | ansible.builtin.debug: 15 | var: configured_servers 16 | 17 | ## Generate report data 18 | - name: Generate data for reporting 19 | ansible.builtin.include_tasks: 'report/data.yml' 20 | -------------------------------------------------------------------------------- /roles/ntpcheck/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Check NTP config per vendor OS 2 | ansible.builtin.include_tasks: "{{ ansible_network_os }}.yml" 3 | -------------------------------------------------------------------------------- /roles/ntpcheck/tasks/nxos.yml: -------------------------------------------------------------------------------- 1 | - name: Get current NTP servers [NX-OS] 2 | ansible.netcommon.cli_command: 3 | command: show run | i 'ntp server' 4 | register: output 5 | 6 | - name: Remove use-vrf default [NX-OS] 7 | ansible.builtin.set_fact: 8 | configured_servers: "{{ configured_servers }} + [ '{{ item | replace(' use-vrf default', '') }}' ]" 9 | with_items: "{{ output.stdout_lines }}" 10 | vars: 11 | configured_servers: [] 12 | 13 | - name: Print current NTP servers [NX-OS] 14 | ansible.builtin.debug: 15 | var: configured_servers 16 | 17 | ## Generate report data 18 | - name: Generate data for reporting 19 | ansible.builtin.include_tasks: 'report/data.yml' 20 | -------------------------------------------------------------------------------- /roles/ntpcheck/tasks/report/data.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Determine configuration delta for reporting [{{ ansible_network_os | default("unknown OS") }}] 3 | ansible.builtin.set_fact: 4 | missing_servers: "{{ required_servers | difference(configured_servers) }}" 5 | unnecessary_servers: "{{ configured_servers | difference(required_servers) }}" 6 | 7 | - name: Create list of NEW servers to configure [{{ ansible_network_os | default("unknown OS") }}] 8 | ansible.builtin.set_fact: 9 | in_servers: "{{ in_servers }} + [ '{{ item.split(' ')[-1] }}' ]" 10 | cacheable: true 11 | with_items: "{{ missing_servers }}" 12 | 13 | - name: Create list of servers to remove [{{ ansible_network_os | default("unknown OS") }}] 14 | ansible.builtin.set_fact: 15 | out_servers: "{{ out_servers }} + [ '{{ item.split(' ')[-1] }}' ]" 16 | cacheable: true 17 | with_items: "{{ unnecessary_servers }}" 18 | 19 | - name: Print out findings for reporting [{{ ansible_network_os | default("unknown OS") }}] 20 | ansible.builtin.debug: 21 | msg: 22 | - "We are missing the following NTP Servers in {{ ansible_network_os | upper }}: {{ in_servers | list }}" 23 | - "We will delete these NTP Servers in {{ ansible_network_os | upper }}: {{ out_servers | list }}" 24 | -------------------------------------------------------------------------------- /roles/requirements.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Install a role from GitHub 3 | - src: https://github.com/clay584/parse_genie 4 | name: parse_genie 5 | -------------------------------------------------------------------------------- /roles/restore/tasks/eos.yml: -------------------------------------------------------------------------------- 1 | - debug: 2 | msg: "restoring from /backup/{{rollback_date}}/{{inventory_hostname}}" 3 | 4 | - name: restore the config 5 | eos_config: 6 | replace: config 7 | src: "/backup/{{rollback_date}}/{{inventory_hostname}}" 8 | 9 | - name: print to terminal window 10 | debug: 11 | msg: "Restore is complete for device {{inventory_hostname}} is set to {{rollback_date}} timestamp" 12 | -------------------------------------------------------------------------------- /roles/restore/tasks/ios.yml: -------------------------------------------------------------------------------- 1 | - debug: 2 | msg: "restoring from /backup/{{rollback_date}}/{{inventory_hostname}}" 3 | 4 | - name: restore the config 5 | ios_config: 6 | src: "/backup/{{rollback_date}}/{{inventory_hostname}}" 7 | 8 | - name: print to terminal window 9 | debug: 10 | msg: "Restore is complete for device {{inventory_hostname}} is set to {{rollback_date}} timestamp" 11 | -------------------------------------------------------------------------------- /roles/restore/tasks/junos.yml: -------------------------------------------------------------------------------- 1 | - debug: 2 | msg: "restoring from /backup/{{rollback_date}}/{{inventory_hostname}}" 3 | 4 | - name: restore the config 5 | junos_config: 6 | update: replace 7 | src: "/backup/{{rollback_date}}/{{inventory_hostname}}" 8 | 9 | - name: print to terminal window 10 | debug: 11 | msg: "Restore is complete for device {{inventory_hostname}} is set to {{rollback_date}} timestamp" 12 | -------------------------------------------------------------------------------- /roles/restore/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: load platform module for restore 2 | include_tasks: "{{ ansible_network_os }}.yml" 3 | -------------------------------------------------------------------------------- /show-diff.md: -------------------------------------------------------------------------------- 1 | # Check config diff 2 | 3 | ## Dependencies 4 | 5 | ### Collections 6 | 7 | Install `cisco.ios`. 8 | 9 | ```bash 10 | ansible-galaxy collection install cisco.ios 11 | ``` 12 | 13 | ## Tasks 14 | 15 | Latest version -> [show-diff](show-diff.yml). The following output might be outdated. 16 | 17 | ```yaml 18 | - name: Backup the config 19 | cisco.ios.ios_config: 20 | backup: true 21 | register: config_output 22 | 23 | - name: Print debug message 24 | ansible.builtin.debug: 25 | msg: "Backup generated {{ config_output.date }} at {{ config_output.time }}" 26 | tags: debug 27 | 28 | - name: Configure ACL on Cisco IOS device using ios_config module 29 | cisco.ios.ios_config: 30 | lines: 31 | - 10 permit ip host 192.0.2.1 any log 32 | - 20 permit ip host 192.0.2.2 any log 33 | - 30 permit ip host 192.0.2.3 any log 34 | - 40 permit ip host 192.0.2.4 any log 35 | - 50 permit ip host 192.0.2.5 any log 36 | parents: ip access-list extended ACL-Ansible-CLI 37 | before: no ip access-list extended test 38 | match: exact 39 | save_when: modified 40 | 41 | - name: Configure ACL on Cisco IOS device using ios_acls module 42 | cisco.ios.ios_acls: 43 | state: replaced 44 | config: 45 | - afi: ipv4 46 | acls: 47 | - name: ACL-Ansible-RM 48 | aces: 49 | - sequence: 10 50 | grant: deny 51 | source: 52 | any: true 53 | destination: 54 | address: 198.51.100.0 55 | wildcard_bits: 0.0.0.255 56 | protocol: tcp 57 | - sequence: 20 58 | grant: permit 59 | source: 60 | any: true 61 | destination: 62 | any: true 63 | protocol: tcp 64 | 65 | - name: Compare the Cisco IOS running-config to backup config 66 | cisco.ios.ios_config: 67 | diff_against: intended 68 | intended_config: "{{ lookup('file', '{{ config_output.backup_path }}') }}" 69 | register: diff 70 | ``` 71 | 72 | ## Output 73 | 74 | The following output might be outdated. 75 | 76 | ```bash 77 | ⇨ ansible-playbook --diff show-diff.yml 78 | 79 | PLAY [ios] ******************************************************************************************************************* 80 | 81 | TASK [Backup the config] ***************************************************************************************************** 82 | changed: [sandbox-iosxe-latest-1.cisco.com] 83 | 84 | TASK [Print debug message] *************************************************************************************************** 85 | ok: [sandbox-iosxe-latest-1.cisco.com] => 86 | msg: Backup generated 2023-03-02 at 12:49:35 87 | 88 | TASK [Configure ACL on Cisco IOS device using ios_config module] ************************************************************* 89 | [WARNING]: To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if 90 | present in the running configuration on device 91 | changed: [sandbox-iosxe-latest-1.cisco.com] 92 | 93 | TASK [Configure ACL on Cisco IOS device using ios_acls module] *************************************************************** 94 | changed: [sandbox-iosxe-latest-1.cisco.com] 95 | 96 | TASK [Compare the Cisco IOS running-config to backup config] ***************************************************************** 97 | --- before 98 | +++ after 99 | @@ -148,15 +148,12 @@ 100 | ip ssh rsa keypair-name ssh-key 101 | ip ssh version 2 102 | ip scp server enable 103 | -ip access-list extended ACL-Ansible-CLI 104 | +ip access-list extended test 105 | 10 permit ip host 192.0.2.1 any log 106 | 20 permit ip host 192.0.2.2 any log 107 | 30 permit ip host 192.0.2.3 any log 108 | 40 permit ip host 192.0.2.4 any log 109 | 50 permit ip host 192.0.2.5 any log 110 | -ip access-list extended ACL-Ansible-RM 111 | - 10 deny tcp any 198.51.100.0 0.0.0.255 112 | - 20 permit tcp any any 113 | control-plane 114 | banner motd ^C 115 | Welcome to the DevNet Sandbox for CSR1000v and IOS XE 116 | 117 | changed: [sandbox-iosxe-latest-1.cisco.com] 118 | 119 | PLAY RECAP ******************************************************************************************************************* 120 | sandbox-iosxe-latest-1.cisco.com : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 121 | ``` 122 | 123 | -------------------------------------------------------------------------------- /show-diff.yml: -------------------------------------------------------------------------------- 1 | # ansible-playbook --diff show-diff.yml 2 | 3 | - hosts: ios 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: Backup the config, make changes and then look at the diff 8 | block: 9 | - name: Backup the config 10 | cisco.ios.ios_config: 11 | backup: true 12 | register: config_output 13 | 14 | - name: Print debug message 15 | ansible.builtin.debug: 16 | msg: "Backup generated {{ config_output.date }} at {{ config_output.time }}" 17 | tags: debug 18 | 19 | - name: Configure ACL on Cisco IOS device using ios_config module 20 | cisco.ios.ios_config: 21 | lines: 22 | - 10 permit ip host 192.0.2.1 any log 23 | - 20 permit ip host 192.0.2.2 any log 24 | - 30 permit ip host 192.0.2.3 any log 25 | - 40 permit ip host 192.0.2.4 any log 26 | - 50 permit ip host 192.0.2.5 any log 27 | parents: ip access-list extended ACL-Ansible-CLI 28 | before: no ip access-list extended test 29 | match: exact 30 | save_when: modified 31 | 32 | - name: Configure ACL on Cisco IOS device using ios_acls module 33 | cisco.ios.ios_acls: 34 | state: replaced 35 | config: 36 | - afi: ipv4 37 | acls: 38 | - name: ACL-Ansible-RM 39 | aces: 40 | - sequence: 10 41 | grant: deny 42 | source: 43 | any: true 44 | destination: 45 | address: 198.51.100.0 46 | wildcard_bits: 0.0.0.255 47 | protocol: tcp 48 | - sequence: 20 49 | grant: permit 50 | source: 51 | any: true 52 | destination: 53 | any: true 54 | protocol: tcp 55 | 56 | - name: Compare the Cisco IOS running-config to backup config 57 | cisco.ios.ios_config: 58 | diff_against: intended 59 | intended_config: "{{ lookup('file', '{{ config_output.backup_path }}') }}" 60 | register: diff 61 | 62 | when: ansible_network_os == 'cisco.ios.ios' 63 | -------------------------------------------------------------------------------- /templates/report.j2: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 26 | 27 | 28 |
29 |

Compliance Report

30 |
31 | 32 |
33 |

NTP Servers missing

34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | {% for device in ansible_play_hosts_all|sort %} 46 | {% for ip in hostvars[device].in_servers %} 47 | 48 | 49 | 50 | 51 | 52 | {% endfor %} 53 | {% endfor %} 54 | 55 |
DeviceTypeIP address
{{ hostvars[device].inventory_hostname }}{{ device | replace('-SSH', '') }}{{ ip }}
56 |
57 |
58 |

NTP Servers to be removed

59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | {% for device in ansible_play_hosts_all|sort %} 71 | {% for ip in hostvars[device].out_servers %} 72 | 73 | 74 | 75 | 76 | 77 | {% endfor %} 78 | {% endfor %} 79 | 80 |
DeviceTypeIP address
{{ hostvars[device].inventory_hostname }}{{ device | replace('-SSH', '') }}{{ ip }}
81 |
82 | 83 |

Created with


84 |

The source code to create this report can be found at https://github.com/nleiva/ansible-networking

85 | If you are new to Ansible Automation check out the following links:
86 | Getting Started
87 |
Free hands-on workshops
88 | Youtube Videos
89 |

90 |
91 | 92 | 93 | -------------------------------------------------------------------------------- /test-json-tasks-1.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Print out neighbor data - FORMAT 1 3 | debug: 4 | msg: "Neighbor: {{ data[0].key }}, with address: {{ data[0].value.address }} -> State: {{ data[0].value.state[0:4] }}" 5 | vars: 6 | data: "{{ item.value.neighbors | dict2items }}" 7 | -------------------------------------------------------------------------------- /test-json-tasks-2.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Print out neighbor data - FORMAT 2 3 | debug: 4 | msg: "Neighbor: {{ info.key }}, with address: {{ info.value.address }} -> State: {{ info.value.state[0:4] }}" 5 | vars: 6 | info: "{{ lookup('dict', data) }}" 7 | -------------------------------------------------------------------------------- /test-json-tasks-3.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Print out a WARNING if OSPF state is not FULL 3 | debug: 4 | msg: "WARNING: Neighbor {{ info.key }}, with address {{ info.value.address }} is in state {{ info.value.state[0:4] }}" 5 | vars: 6 | info: "{{ lookup('dict', data) }}" 7 | # when: info.value.state[0:4] != "FULL" 8 | when: info.value.state is not match("FULL.*") 9 | -------------------------------------------------------------------------------- /test-json-tasks-4.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Validate state of the neighbor is FULL 3 | ansible.utils.validate: 4 | data: "{{ data }}" 5 | criteria: 6 | - "{{ lookup('file', './files/schema.json') | from_json }}" 7 | engine: ansible.utils.jsonschema 8 | ignore_errors: true 9 | register: result 10 | 11 | - name: Print the neighbor that does not satisfy the desired state 12 | ansible.builtin.debug: 13 | msg: 14 | - "WARNING: Neighbor {{ info.key }}, with address {{ info.value.address }} is in state {{ info.value.state[0:4] }}" 15 | - "{{ error.data_path }}, found: {{ error.found }}, expected: {{ error.expected }}" 16 | when: "'errors' in result" 17 | vars: 18 | info: "{{ lookup('dict', data) }}" 19 | error: "{{ result['errors'][0] }}" 20 | -------------------------------------------------------------------------------- /test-json.md: -------------------------------------------------------------------------------- 1 | # Parsing JSON outputs 2 | 3 | ## Variables required 4 | 5 | ### Dependencies 6 | 7 | Install `ansible.utils`. 8 | 9 | ```bash 10 | ansible-galaxy collection install ansible.utils 11 | ``` 12 | 13 | ### Inputs 14 | 15 | JSON file [ospf.json](files/ospf.json). 16 | 17 | ```json 18 | { 19 | "parsed": { 20 | "interfaces": { 21 | "Tunnel0": { 22 | "neighbors": { 23 | "203.0.113.2": { 24 | "address": "198.51.100.2", 25 | "dead_time": "00:00:39", 26 | "priority": 0, 27 | "state": "FULL/ -" 28 | } 29 | } 30 | }, 31 | "Tunnel1": { 32 | "neighbors": { 33 | "203.0.113.2": { 34 | "address": "192.0.2.2", 35 | "dead_time": "00:00:36", 36 | "priority": 0, 37 | "state": "INIT/ -" 38 | } 39 | } 40 | } 41 | } 42 | } 43 | } 44 | ``` 45 | 46 | 47 | ## Playbook 48 | 49 | Latest version -> [test-json](test-json.yml). The following output might be outdated. 50 | 51 | ```yaml 52 | - name: Play around with JSON inputs 53 | hosts: localhost 54 | connection: local 55 | become: false 56 | gather_facts: false 57 | vars: 58 | input: "{{ lookup('file','files/ospf.json') | from_json }}" 59 | 60 | tasks: 61 | - name: Create interfaces Dictionary 62 | set_fact: 63 | interfaces: "{{ input.parsed.interfaces }}" 64 | 65 | - name: Print out flatten interfaces input 66 | debug: 67 | msg: "{{ lookup('ansible.utils.to_paths', interfaces) }}" 68 | 69 | - name: TEST 1 70 | block: 71 | - name: Loop over interfaces 72 | include_tasks: test-json-tasks-1.yml 73 | with_items: "{{ interfaces | dict2items }}" 74 | 75 | - name: TEST 2 76 | block: 77 | - name: Create neighbors dictionary (this is now per interface) 78 | set_fact: 79 | neighbors: "{{ interfaces | json_query('*.neighbors') }}" 80 | 81 | - name: Loop over neighbors 82 | include_tasks: test-json-tasks-2.yml 83 | with_items: "{{ neighbors }}" 84 | loop_control: 85 | loop_var: data 86 | 87 | - name: TEST 3 88 | block: 89 | - name: Loop over neighbors 90 | include_tasks: test-json-tasks-3.yml 91 | with_items: "{{ neighbors }}" 92 | loop_control: 93 | loop_var: data 94 | 95 | - name: TEST 4 96 | block: 97 | - name: Loop with deep json_query 98 | debug: 99 | var: "{{ item }}" 100 | with_items: "{{ input | json_query('parsed.interfaces.*.neighbors[].*.[address, state]') }}" 101 | 102 | - name: TEST 5 103 | block: 104 | - name: Loop over neighbors and validate data with a schema 105 | include_tasks: test-json-tasks-4.yml 106 | with_items: "{{ neighbors }}" 107 | loop_control: 108 | loop_var: data 109 | ``` 110 | 111 | ## Output 112 | 113 | The following output might be outdated. 114 | 115 | ```bash 116 | ⇨ ansible-playbook test-json.yml 117 | [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' 118 | 119 | PLAY [Play around with JSON inputs] *********************************************************************************************************************** 120 | 121 | TASK [Create interfaces Dictionary] *********************************************************************************************************************** 122 | ok: [localhost] 123 | 124 | TASK [Print out flatten interfaces input] ***************************************************************************************************************** 125 | ok: [localhost] => { 126 | "msg": { 127 | "Tunnel0.neighbors['203.0.113.2'].address": "198.51.100.2", 128 | "Tunnel0.neighbors['203.0.113.2'].dead_time": "00:00:39", 129 | "Tunnel0.neighbors['203.0.113.2'].priority": 0, 130 | "Tunnel0.neighbors['203.0.113.2'].state": "FULL/ -", 131 | "Tunnel1.neighbors['203.0.113.2'].address": "192.0.2.2", 132 | "Tunnel1.neighbors['203.0.113.2'].dead_time": "00:00:36", 133 | "Tunnel1.neighbors['203.0.113.2'].priority": 0, 134 | "Tunnel1.neighbors['203.0.113.2'].state": "INIT/ -" 135 | } 136 | } 137 | 138 | TASK [Loop over interfaces] ******************************************************************************************************************************* 139 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-1.yml for localhost 140 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-1.yml for localhost 141 | 142 | TASK [Print out neighbor data - FORMAT 1] ***************************************************************************************************************** 143 | ok: [localhost] => { 144 | "msg": "Neighbor: 203.0.113.2, with address: 198.51.100.2 -> State: FULL" 145 | } 146 | 147 | TASK [Print out neighbor data - FORMAT 1] ***************************************************************************************************************** 148 | ok: [localhost] => { 149 | "msg": "Neighbor: 203.0.113.2, with address: 192.0.2.2 -> State: INIT" 150 | } 151 | 152 | TASK [Create neighbors dictionary (this is now per interface)] ******************************************************************************************** 153 | ok: [localhost] 154 | 155 | TASK [Loop over neighbors] ******************************************************************************************************************************** 156 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-2.yml for localhost 157 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-2.yml for localhost 158 | 159 | TASK [Print out neighbor data - FORMAT 2] ***************************************************************************************************************** 160 | ok: [localhost] => { 161 | "msg": "Neighbor: 203.0.113.2, with address: 198.51.100.2 -> State: FULL" 162 | } 163 | 164 | TASK [Print out neighbor data - FORMAT 2] ***************************************************************************************************************** 165 | ok: [localhost] => { 166 | "msg": "Neighbor: 203.0.113.2, with address: 192.0.2.2 -> State: INIT" 167 | } 168 | 169 | TASK [Loop over neighbors] ******************************************************************************************************************************** 170 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-3.yml for localhost 171 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-3.yml for localhost 172 | 173 | TASK [Print out a WARNING if OSPF state is not FULL] ****************************************************************************************************** 174 | skipping: [localhost] 175 | 176 | TASK [Print out a WARNING if OSPF state is not FULL] ****************************************************************************************************** 177 | ok: [localhost] => { 178 | "msg": "WARNING: Neighbor 203.0.113.2, with address 192.0.2.2 is in state INIT" 179 | } 180 | 181 | TASK [Loop with deep json_query] ************************************************************************************************************************** 182 | ok: [localhost] => (item=['198.51.100.2', 'FULL/ -']) => { 183 | "": "VARIABLE IS NOT DEFINED!", 184 | "ansible_loop_var": "item", 185 | "item": [ 186 | "198.51.100.2", 187 | "FULL/ -" 188 | ] 189 | } 190 | ok: [localhost] => (item=['192.0.2.2', 'INIT/ -']) => { 191 | "": "VARIABLE IS NOT DEFINED!", 192 | "ansible_loop_var": "item", 193 | "item": [ 194 | "192.0.2.2", 195 | "INIT/ -" 196 | ] 197 | } 198 | 199 | TASK [Loop over neighbors and validate data with a schema] ************************************************************************************************ 200 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-4.yml for localhost 201 | included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-4.yml for localhost 202 | 203 | TASK [Validate state of the neighbor is FULL] ************************************************************************************************************* 204 | ok: [localhost] 205 | 206 | TASK [Print the neighbor that does not satisfy the desired state] ***************************************************************************************** 207 | skipping: [localhost] 208 | 209 | TASK [Validate state of the neighbor is FULL] ************************************************************************************************************* 210 | fatal: [localhost]: FAILED! => {"changed": false, "errors": [{"data_path": "203.0.113.2.state", "expected": "^FULL", "found": "INIT/ -", "json_path": "$.203.0.113.2.state", "message": "'INIT/ -' does not match '^FULL'", "relative_schema": {"pattern": "^FULL", "type": "string"}, "schema_path": "patternProperties..*.properties.state.pattern", "validator": "pattern"}], "msg": "Validation errors were found.\nAt 'patternProperties..*.properties.state.pattern' 'INIT/ -' does not match '^FULL'. "} 211 | ...ignoring 212 | 213 | TASK [Print the neighbor that does not satisfy the desired state] ***************************************************************************************** 214 | ok: [localhost] => { 215 | "msg": [ 216 | "WARNING: Neighbor 203.0.113.2, with address 192.0.2.2 is in state INIT", 217 | "203.0.113.2.state, found: INIT/ -, expected: ^FULL" 218 | ] 219 | } 220 | 221 | PLAY RECAP ************************************************************************************************************************************************ 222 | localhost : ok=20 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=1 223 | ``` 224 | 225 | -------------------------------------------------------------------------------- /test-json.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # ansible-galaxy collection install ansible.utils 3 | # ansible-playbook test-json.yml 4 | 5 | - name: Play around with JSON inputs 6 | hosts: localhost 7 | connection: local 8 | become: false 9 | gather_facts: false 10 | vars: 11 | input: "{{ lookup('file','files/ospf.json') | from_json }}" 12 | 13 | tasks: 14 | - name: Create interfaces Dictionary 15 | set_fact: 16 | interfaces: "{{ input.parsed.interfaces }}" 17 | 18 | - name: Print out interfaces 19 | debug: 20 | var: interfaces 21 | 22 | - name: Print out flatten interfaces input 23 | debug: 24 | msg: "{{ lookup('ansible.utils.to_paths', interfaces) }}" 25 | 26 | - name: TEST 1 27 | block: 28 | - name: Loop over interfaces 29 | include_tasks: test-json-tasks-1.yml 30 | with_items: "{{ interfaces | dict2items }}" 31 | 32 | - name: TEST 2 33 | block: 34 | - name: Create neighbors dictionary (this is now per interface) 35 | set_fact: 36 | neighbors: "{{ interfaces | json_query('*.neighbors') }}" 37 | 38 | - name: Print out neighbors 39 | debug: 40 | msg: "{{ neighbors }}" 41 | 42 | - name: Loop over neighbors 43 | include_tasks: test-json-tasks-2.yml 44 | with_items: "{{ neighbors }}" 45 | loop_control: 46 | loop_var: data 47 | 48 | - name: TEST 3 49 | block: 50 | - name: Loop over neighbors 51 | include_tasks: test-json-tasks-3.yml 52 | with_items: "{{ neighbors }}" 53 | loop_control: 54 | loop_var: data 55 | 56 | - name: TEST 4 57 | block: 58 | - name: Loop with deep json_query 59 | debug: 60 | var: "{{ item }}" 61 | with_items: "{{ input | json_query('parsed.interfaces.*.neighbors[].*.[address, state]') }}" 62 | 63 | - name: TEST 5 64 | block: 65 | - name: Loop over neighbors and validate data with a schema 66 | include_tasks: test-json-tasks-4.yml 67 | with_items: "{{ neighbors }}" 68 | loop_control: 69 | loop_var: data 70 | -------------------------------------------------------------------------------- /test-list.yml: -------------------------------------------------------------------------------- 1 | # ansible-playbook test-list.yml 2 | 3 | - name: Create sub-list 4 | hosts: localhost 5 | connection: local 6 | become: false 7 | gather_facts: false 8 | vars: 9 | input: "{{ lookup('file','list.json') | from_json }}" 10 | interfacelist: [] 11 | 12 | tasks: 13 | - name: Create interfaces Dictionary 14 | ansible.builtin.set_fact: 15 | interfaces: "{{ input }}" 16 | 17 | - name: Print out interfaces 18 | ansible.builtin.debug: 19 | var: item 20 | when: not item.enabled and 'Gig' in item.name 21 | with_items: "{{ interfaces }}" 22 | 23 | - name: Create list with items in variable interfaces which enable is false 24 | ansible.builtin.set_fact: 25 | interfacelist: "{{ interfacelist + [item.name] }}" 26 | when: not item.enabled and 'Gig' in item.name 27 | with_items: "{{ interfaces }}" 28 | 29 | - name: Print out interfaces variable 30 | ansible.builtin.debug: 31 | var: interfacelist 32 | -------------------------------------------------------------------------------- /use-encrypted-file.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # ansible-playbook --ask-vault-pass use-encrypted-file.yml 3 | 4 | - name: Using data from an encrypted file 5 | hosts: localhost 6 | connection: local 7 | become: false 8 | gather_facts: false 9 | vars: 10 | the_key: "{{ lookup('file', '{{ playbook_dir }}/data.json') }}" 11 | 12 | tasks: 13 | - name: make sure we are running correct Ansible Version 14 | assert: 15 | that: 16 | - ansible_version.major >= 2 17 | - ansible_version.minor >= 9 18 | 19 | - name: Check the file exists 20 | stat: 21 | path: "{{ playbook_dir }}/data.json" 22 | register: stat_result 23 | - debug: 24 | var: stat_result 25 | - name: Complain if input fail doesn't exist 26 | fail: 27 | msg: "We need a file located at {{ playbook_dir }}/data.json" 28 | when: 29 | - not stat_result.stat.exists 30 | 31 | - name: Print output from file 32 | debug: msg="the content of the file is {{ lookup('file', '{{ playbook_dir }}/data.json') }}" 33 | 34 | - name: Print output from var 35 | debug: msg="the content of the file is {{ the_key }}" 36 | 37 | - name: Copy manually provided Key 38 | copy: 39 | content: "{{ the_key2 }}" 40 | dest: "{{ playbook_dir }}/private.pem" 41 | mode: '0400' 42 | vars: 43 | the_key2: "{{ lookup('file', '{{ playbook_dir }}/data.json') }}" 44 | -------------------------------------------------------------------------------- /validate.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Need to add to requirements file these: 3 | # ansible-galaxy collection install ansible.utils # version greater than 2.0.1 4 | # ansible-galaxy collection install ansible.netcommon 5 | # ansible-galaxy collection install cisco.ios 6 | # ansible-galaxy collection install community.general 7 | 8 | - hosts: "{{ my_devices }}" 9 | connection: ansible.netcommon.network_cli 10 | gather_facts: "{{ my_facts }}" 11 | vars: 12 | ansible_network_os: cisco.ios.ios 13 | 14 | tasks: 15 | - name: Parse list of commands 16 | include_tasks: validate_commands.yml 17 | loop: 18 | - show version 19 | - show interfaces 20 | - show lldp neighbors 21 | - show ip bgp summary 22 | - show ip ospf neighbor 23 | loop_control: 24 | loop_var: command 25 | 26 | # - name: Print all structured data 27 | # ansible.builtin.debug: 28 | # var: my_dict 29 | 30 | - name: Validate state from commands 31 | include_tasks: validate_state.yml 32 | ... 33 | -------------------------------------------------------------------------------- /validate_commands.yml: -------------------------------------------------------------------------------- 1 | - name: Fetch {{ command }} and parse it with pyATS 2 | ansible.utils.cli_parse: 3 | command: "{{ command }}" 4 | parser: 5 | name: ansible.netcommon.pyats 6 | register: output 7 | ignore_errors: true 8 | 9 | - name: Add {{ command }} to dictionary with command outputs 10 | set_fact: 11 | my_dict: "{{ my_dict | default({}) | combine( { command: (output['parsed'] | default('empty', true)) }) }}" 12 | 13 | # - name: Print structured {{ command }} data 14 | # ansible.builtin.debug: 15 | # msg: "{{ my_dict[command] }}" 16 | -------------------------------------------------------------------------------- /validate_state.yml: -------------------------------------------------------------------------------- 1 | - name: Show BGP neighbor info 2 | ansible.builtin.debug: 3 | msg: "{{ lookup('ansible.utils.to_paths', bgp_neighbors) }}" 4 | vars: 5 | bgp_neighbors: "{{ my_dict['show ip bgp summary'].vrf.default.neighbor }}" 6 | 7 | - name: Create OSPF neighbors dictionary 8 | set_fact: 9 | ospf_neighbors: "{{ my_dict['show ip ospf neighbor'] | community.general.json_query('interfaces.*.neighbors') }}" 10 | 11 | - name: Loop over OSPF neighbors 12 | include_tasks: test-json-tasks-3.yml 13 | with_items: "{{ ospf_neighbors }}" 14 | loop_control: 15 | loop_var: data 16 | --------------------------------------------------------------------------------