- Network Automation Cookbook
- Karim Okasha
- 522字
- 2025-04-04 13:10:13
How it works...
In this recipe, we start by creating the roles directory within our main folder. By default, when using roles, Ansible will look for roles in the following location in this order:
- The roles folder within the current working directory
- /etc/ansible/roles
Consequently, we create the roles folder within our current working directory (ch1_ansible) in order to host all the roles that we will create in this folder. We create the role using the ansible-galaxy command with the init option and the role name (basic_config), which will create the following role structure inside our roles folder:
$ tree roles/
roles/
└── basic_config
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
As can be seen from the preceding output, this folder structure is created using the ansible-galaxy command and this command builds the role in keeping with the best practice role layout. Not all these folders need to have a functional role that we can use, and the following list outlines the main folders that are commonly used:
- The tasks folder: This contains the main.yml file, which lists all the tasks that should be executed when we use this role.
- The templates folder: This contains all the Jinja2 templates that we will use as part of this role.
- The vars folder: This contains all the variables that we want to define and that we will use in our role. The variables inside the vars folder have very high precedence when evaluating the variables while running the playbook.
- The handlers folder: This contains the main.yml file, which includes all the handlers that should run as part of this role.
The role that we created has a single purpose, which is to build the basic configuration for our devices. In order to accomplish this task, we need to define some Ansible tasks as well as use a number of Jinja2 templates in order to generate the basic configuration for the devices. We list all the tasks that we need to run in the tasks/main.yml file and we include all the necessary Jinja2 templates in the templates folder. We define any requisite variable that we will use in our role in the vars folder.
We create a new playbook that will use our new role in order to generate the configuration for the devices. We call all the roles that we want to run as part of our playbook in the roles parameter. In our case, we have a single role that we want to run, which is the basic_config role.
Once we run our playbook, we can see that a new directory called basic_config is created with the following content:
$ tree basic_config/
basic_config/
├── csr1.cfg
├── csr2.cfg
├── mx1.cfg
└── mx2.cfg