Skip to content

Commit c2a5570

Browse files
authored
Support for Ansible Tags (#133)
* Initial commit for tag support * Fix github actions issues * Fix remove tags and add timeouts * Add missing tags * Fix typo * Add role level tags * Enable role level tags in test_common role * Remove test role defaults * README Updates * Readme updates * Readme updates * Document tags for create role * Document tags * Fix tag docs * Fix create role tag * Few doc updates
1 parent 4fd8a9e commit c2a5570

File tree

34 files changed

+983
-640
lines changed

34 files changed

+983
-640
lines changed

README.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
[![Actions Status](https://github.com/netascode/ansible-dc-vxlan/workflows/CI/badge.svg)](https://github.com/netascode/ansible-dc-vxlan/actions)
44

5-
Ansible collection for configuring Cisco VXLAN/EVPN fabric using Cisco Nexus Dashboard Fabric Controller (NDFC). Utilizing a foundation of a data model, this collection simplifies the configuration of VXLAN fabrics by abstracting the automation and utilizing a data model that represents the desired state of the fabric. An operator utilizing this collection will only have to modify the configuration state in the data model instead of creating the modules and the associated parameters.
5+
Ansible Collection for configuring Cisco VXLAN EVPN fabrics using the Cisco Nexus Dashboard Fabric Controller (NDFC). This collection simplifies the configuration of VXLAN fabrics by abstracting the automation using a data model that represents the desired state of the fabric. With this collection, an operator only needs to modify the configuration state in the data model instead of creating custom playbooks with modules and the associated parameters.
66

7-
This approach allows for a more consistent and repeatable configuration of VXLAN fabrics and is aligned with the methodology of infrastructure as code, where the configuration of NDFC would be saved in a version control system and managed as code.
7+
This approach allows for consistent and repeatable configuration of VXLAN fabrics and aligns with Infrastructure as Code (IaC) methodology, where the configuration state of NDFC is saved in a version control system and managed as code.
88

9-
Infrastructure as code (IaC) is a DevOps methodology that uses code to manage and provision IT infrastructure, instead of manual procedures. IaC uses a descriptive coding language to automate provisioning servers, operating systems, network devices and more. The NetAsCode VXLAN EVPN collection allows you to configure, in easy-to-understand YAML, data structures with the intended configuration state of a VXLAN fabric using Cisco Nexus Dashboard Fabric Controller.
9+
Infrastructure as code (IaC) is a DevOps methodology that uses code to manage and provision IT infrastructure, bypassing manual procedures. IaC uses a descriptive coding language to automate the provisioning of servers, operating systems, network devices and more.
1010

11-
The NetAsCode VXLAN collection provides the capability to create a declarative method of configuration of VXLAN for [Cisco Nexus](https://www.cisco.com/site/us/en/products/networking/cloud-networking-switches/index.html) datacenter solution utilizing [Cisco Nexus Dashboard](https://www.cisco.com/site/us/en/products/networking/cloud-networking/nexus-platform/index.html). This allows the separation of data from execution logic. With little to no knowledge about automation, you can instantiate a VXLAN EVPN fabric with this collection.
11+
The NetAsCode VXLAN EVPN collection allows you to configure, in easy-to-understand YAML, data structures describing the configuration state of a VXLAN fabric for [Cisco Nexus](https://www.cisco.com/site/us/en/products/networking/cloud-networking-switches/index.html) datacenters and then deploys this state using Ansible and the [Cisco Nexus Dashboard](https://www.cisco.com/site/us/en/products/networking/cloud-networking/nexus-platform/index.html). With little to no knowledge about automation, you can use this collection to instantiate a VXLAN EVPN fabric.
1212

13-
This is achieved by creating YAML files that contain a pre-determined data schema that is translated into underlying Ansible modules and resources. The core Ansible collection is open source and available. The collection is designed to be used in a CI/CD pipeline, which allows you to establish a declarative method of configuration of VXLAN for Cisco Nexus datacenter solution.
13+
YAML files are created that contain a pre-determined data schema which is translated into underlying Ansible modules and resources. The core Ansible Collection is open source and available. This collection is designed to be used in a CI/CD pipeline in order to drive this declarative method of configuring a VXLAN fabric.
1414

1515
> **Note**: For complete support and additional capabilities, Cisco provides a professional services capability under the Services as Code portfolio of services which can provide feature creation, end to end support and more.
1616
@@ -57,39 +57,38 @@ vpc_peering_delete_mode: false
5757

5858
### Advantages of the Roles in the Workflow
5959

60-
The primary advantage of the workflow is that you can insert these in different stages of the data model preparation and changes without having to worry about impacts to the network. In a SCM repository environment, pipelines can be configured to run the validate role before approvals in pull requests.
61-
62-
The roles are designed to be idempotent and only make changes when there are changes in the data model. For different stages of changes in the network, you can comment out the roles that are not required to be executed. You can leave the final full execution potentially to only happen from a pipeline, yet allow for operators to validate changes before they are executed.
60+
These roles when run in sequence (validate, create, deploy, remove) are designed to build out the entire fabric and can be executed by a pipeline. The roles can also be run in isolation by simply commenting out the roles that are not required during testing and fabric buildout to validate incremental changes.
6361

6462
## Quick Start Guide
6563

6664
### Set Environment for the Collection
6765

68-
The first procedure for execution of the collection is going to be the installation of a virtual environment to be able to install the collection and it's requirements. Recommendation is to utilize [pyenv](https://github.com/pyenv/pyenv) which provides a robust python virtual environment capability that also includes management of python versions. These instructions will be detailed around pyenv. For the pipeline execution please refer to *pipeline section* where it is documented at container level.
66+
Installation of a Python virtual environment is needed in order to install the collection and it's requirements. We recommend [pyenv](https://github.com/pyenv/pyenv) which provides a robust Python virtual environment capability that also allows for management of different Python versions. The following instructions are detailed around using pyenv. For pipeline execution please refer to the pipeline section which is documented at container level.
6967

7068
#### Step 1 - Installing the Example Repository
7169

72-
To simplify the usage of the collection we are providing you with an [example repository](https://github.com/netascode/ansible-dc-vxlan-example) that you can clone from GitHub which creates the proper skeleton required, including examples for pipelines. To clone the repository requires the installation of [git client](https://git-scm.com/downloads) that is available for all platforms.
70+
To simplify getting started with this collection we provide you with an [example repository](https://github.com/netascode/ansible-dc-vxlan-example). Simply clone this repo from GitHub to create the required skeleton, including examples for pipelines. Cloaning the repository requires the installation of [git client](https://git-scm.com/downloads) which is available for all platforms.
7371

7472
Run the following command in the location of interest.
7573

7674
```bash
7775
git clone https://github.com/netascode/ansible-dc-vxlan-example.git nac-vxlan
7876
```
7977

80-
This will clone the example repository into the directory nac-vxlan. Then you will delete the `.git` repository to remove the connection to the example repository. Which then allows you to create your own repository from this built structure.
78+
This will clone the example repository into the directory `nac-vxlan`. Next delete the `.git` repository to remove the connection to the example repository. Now you can create your own repository from this pre-built structure.
8179

8280
#### Step 2 - Create the Virtual Environment with pyenv
8381

84-
In this directory you will now create the new virtual environment. For pyenv to work you must install a version of Python that you want to utilize. At the _time of this writing_, a common version utilized is python version 3.10.13 so to install this with pyenv would be the command `pyenv install 3.10.13`. For detailed instructions please visit the [pyenv](https://github.com/pyenv/pyenv) site.
82+
In this directory create a new virtual environment and install a Python version of your choice. At the time of this writting, a commonly used version is Python version 3.10.13. Command pyenv install 3.10.13 will install this version. For detailed instructions please visit the [pyenv](https://github.com/pyenv/pyenv) site.
83+
8584

8685
```bash
8786
cd nac-vxlan
8887
pyenv virtualenv <python_version> nac-ndfc
8988
pyenv local nac-ndfc
9089
```
9190

92-
The final command is `pyenv local` which sets the environment so that whenever you enter the directory it will change into the right virtual environment.
91+
Executing command pyenv local nac-ndfc sets the environment so that whenever the directory is entered it will change into the right virtual environment.
9392

9493
#### Step 3 - Install Ansible and Additional Required Tools
9594

@@ -116,9 +115,9 @@ If you wish to install the galaxy collection inside the repository you are creat
116115
ansible-galaxy collection install -p collections/ansible_collections/ -r requirements.yaml
117116
```
118117

119-
You will need to then configure your ansible.cfg file to point to the correct location of the collection.
118+
The `ansible.cfg` file needs to be configured to point to the location of the collection.
120119

121-
This is the path for all the python modules and libraries of the virtual environment that were created. If you look in that directory, you will find the collections package locations. Here is the base ansible.cfg, you will need to adjust the collection_path to your environment paths:
120+
This is the path for all the python modules and libraries of the virtual environment that were created. If you look in that directory, you will find the collections package locations. Here is the base ansible.cfg, you will need to adjust the collections_path to your environment paths:
122121

123122
```bash
124123
[defaults]
@@ -254,7 +253,7 @@ PEP440 is the schema used to describe the versions of Ansible.
254253

255254
## Building the Primary Playbook
256255

257-
The playbook for the NDFC as Code collection is the execution point of the this automation collection. In difference to other automation with collections, what is in this playbook is mostly static and not going to change. What is executed during automation is based on changes in the data model. Hence as changes happen in the data model, the playbook will call the modules and based on what has changed in the data model, is what is going to execute.
256+
The following playbook for the NDFC as Code collection is the central execution point for this collection. Compared to automation in other collections, this playbook is designed to be mostly static and typically will not change. What gets executed during automation is based entirely on changes in the data model. When changes are made in the data model, the playbook will call the various roles and underlying modules to process the changes and update the NDFC managed fabric.
258257

259258
The playbook is located in the root of the repository and is called `vxlan.yaml`. It contains the following:
260259

@@ -269,27 +268,51 @@ The playbook is located in the root of the repository and is called `vxlan.yaml`
269268
roles:
270269
# Prepare service model for all subsequent roles
271270
#
272-
# - role: cisco.nac_dc_vxlan.validate
271+
- role: cisco.nac_dc_vxlan.validate
272+
tags: 'role_validate'
273273
274274
# -----------------------
275275
# DataCenter Roles
276276
# Role: cisco.netascode_dc_vxlan.dtc manages direct to controller NDFC workflows
277277
#
278278
- role: cisco.nac_dc_vxlan.dtc.create
279+
tags: 'role_create'
280+
279281
- role: cisco.nac_dc_vxlan.dtc.deploy
282+
tags: 'role_deploy'
283+
280284
- role: cisco.nac_dc_vxlan.dtc.remove
285+
tags: 'role_remove'
281286
```
282287

283-
The `host` is defined as nac-ndfc1 which references back to the inventory file. The `roles` section is where the collection is going to be called.
288+
The `host` is defined as nac-ndfc1 which references back to the `inventory.yaml` file. The `roles` section is where the various collection roles are called.
289+
290+
The first role is `cisco.nac_dc_vxlan.validate` which is going to validate the data model. This is a required step to ensure that the data model is correct and that the data model is going to be able to be processed by the subsequent roles.
291+
292+
The subsequent roles are the `cisco.nac_dc_vxlan.dtc.create`, `cisco.nac_dc_vxlan.dtc.deploy`, and `cisco.nac_dc_vxlan.dtc.remove` roles. These roles are the primary roles that will invoke changes in NDFC as described earlier.
284293

285-
The first role is `cisco.nac_dc_vxlan.validate` which is going to validate the data model. This is a required step to ensure that the data model is correct and that the data model is going to be able to be processed by the subsequent roles. **This role is going to execute by default even if not defined as it is required for the subsequent roles to execute.** In this example we are commenting out the role to show that it is not required to be defined in the playbook, but if you want to define a playbook that just runs the validation, you would uncomment this role.
286294

287-
The next roles are the `cisco.nac_dc_vxlan.dtc.create`, `cisco.nac_dc_vxlan.dtc.deploy`, and `cisco.nac_dc_vxlan.dtc.remove`. These roles are the primary roles that will invoke change in NDFC. The `create` role will create all the templates and variable parameters. The `deploy` role will deploy those changes to the NDFC controller. The `remove` role would remove the data model from the devices in the inventory.
295+
> **Note**: For your safety as indicated ealier, the `remove` role also requires setting some variables to `true` under the `group_vars` directory. This is to avoid accidental removal of configuration from NDFC that might impact the network. This will be covered in more detail below.
288296

289-
> **Note**: For your safety, the `remove` role also requires settings some variables to true under the `group_vars` directory. This is done to avoid accidental removal of configuration from NDFC that might impact the network. This will be covered in a section below.
297+
The playbook can be configured to execute only the roles that are required. For example, as you are building your data model and familiarizing yourself with the collection, you may comment out the `deploy` and `remove` roles and only execute the `validate` and `create` roles. This provides a quick way to make sure that the data model is structured correctly.
290298

299+
------
300+
**Role Level Tags:**
291301

292-
Since each of these roles are separate, you may configure the playbook to only execute the roles that are required. For example, as you are building your data model and getting to know the collection, you may comment out the `deploy` and `remove` roles to only execute the `validate` and `create` role. This provides a quick way to make sure that the data model is structured correctly.
302+
To speed up execution when only certain roles need to be run the following role level tags are provided:
303+
304+
* role_validate - Select and run `cisco.nac_dc_vxlan.validate` role
305+
* role_create - Select and run `cisco.nac_dc_vxlan.create` role
306+
* role_deploy - Select and run `cisco.nac_dc_vxlan.deploy` role
307+
* role_remove - Select and run `cisco.nac_dc_vxlan.remove` role
308+
309+
The validate role will automatically run if tags `role_create, role_deploy, role_remove` are specified.
310+
311+
Example: Selectively Run `cisco.nac_dc_vxlan.create` role alone
312+
313+
```bash
314+
ansible-playbook -i inventory.yml vxlan.yml --tags role_create
315+
```
293316

294317
### See Also
295318

plugins/action/dtc/verify_tags.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
# this software and associated documentation files (the "Software"), to deal in
5+
# the Software without restriction, including without limitation the rights to
6+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
# the Software, and to permit persons to whom the Software is furnished to do so,
8+
# subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all
11+
# copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
#
20+
# SPDX-License-Identifier: MIT
21+
22+
from __future__ import absolute_import, division, print_function
23+
24+
25+
__metaclass__ = type
26+
27+
from ansible.utils.display import Display
28+
from ansible.plugins.action import ActionBase
29+
30+
display = Display()
31+
32+
33+
class ActionModule(ActionBase):
34+
35+
def run(self, tmp=None, task_vars=None):
36+
# self._supports_async = True
37+
results = super(ActionModule, self).run(tmp, task_vars)
38+
results['failed'] = False
39+
40+
all_tags = self._task.args['all_tags']
41+
play_tags = self._task.args['play_tags']
42+
43+
if 'all' in play_tags:
44+
return results
45+
46+
for tag in play_tags:
47+
if tag not in all_tags:
48+
results['failed'] = True
49+
results['msg'] = "Tag '{0}' not found in list of supported tags".format(tag)
50+
results['supported_tags'] = all_tags
51+
52+
return results

roles/common_global/tasks/main.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
# this software and associated documentation files (the "Software"), to deal in
5+
# the Software without restriction, including without limitation the rights to
6+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
# the Software, and to permit persons to whom the Software is furnished to do so,
8+
# subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all
11+
# copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
#
20+
# SPDX-License-Identifier: MIT
21+
---
22+
23+
- name: Verify User Tags
24+
cisco.nac_dc_vxlan.dtc.verify_tags:
25+
all_tags: "{{ nac_tags.all }}"
26+
play_tags: "{{ ansible_run_tags }}"
27+
tags: "{{ ansible_run_tags }}"

0 commit comments

Comments
 (0)