Using Ansible roles
Ansible roles promote code reusability and provide a simple method for packaging Ansible code in a simple way that can be shared and consumed. An Ansible role is a grouping of all the required Ansible tasks, variables, handlers, and Jinja2 templates that are packaged together in a specific structure. A role should be designed in order to deliver a specific function/task to facilitate its reusability. In this recipe, we will outline how to create an Ansible role and how to use it in your playbooks:
Let’s start out by creating a new directory in the ch03 folder, called roles:
root@cookbook-lab:~/cookbook-lab/chapters/ch03# mkdir roles && cd roles
Next, you will use the ansible-galaxy utility function to generate your role boilerplate code (this is a great way to get started!):
root@cookbook-lab:~/cookbook-lab/chapters/ch03/roles# ansible-galaxy init basic_config
- Role basic_config was created successfully
Note
Ansible...