Installation And Usage AnsibleGalaxy

 

Roles are a basic concept in Ansible. In order to be able to reuse roles in different playbooks, it is interesting to organize the roles in separate folders and have a repository for each of them.
Given the possibility of organizing the roles in this way, a community for the publication and exchange of roles called Ansible Galaxy has been organized . Each role in Ansible Galaxy is linked to its source code.

Installing an Ansible Galaxy role
It is therefore convenient to have a folder where we have all the roles stored (eg roles). Then, at a higher level, we will have the playbooks and inventory files corresponding to each project. But maybe it would be better to have all the playbooks and inventory files in one folder at the same level as the roles. In this case the playbooks would go up one level and then down the folder rolesto use the corresponding roles.
Example: Organization of playbooks and roles
.
├── playbooks
│   ├── nginx-hosts.cfg
│   ├── nginx-playbook.yml
│   ├── php-hosts.cfg
│   ├── php-playbook.yml
│   ├── phpwebserver-hosts.cfg
│   └── phpwebserver-playbook.yml
└── roles
    ├── testrole.git
    │   ├── ...
    ├── testrole.php
    │   ├── ...
    ├── itinfs.apache
    │   ├── ...
    └── itinfs.apache2
        ├── ...
Let be rolesthe folder where we keep all our roles and let be testrole.php the role we want to install, available in Ansible Galaxy. To download and install the role locally we would write:
$ ansible-galaxy install testrole.php –p roles
Then, in our playbooks folder, we would create the host inventory file for our project and the playbook.
Example: The filephp-hosts.cfg
20.0.1.11
20.0.1.4
Example: The filephp-playbook.yml
---
- hosts: all
  become: true
  roles:
    - ../roles/testrole.php
To run this playbook from the playbooks folder, simply:
$ ansible-playbook -i nginx-hosts.cfg nginx-playbook.yml
Create a role with Ansible Galaxy
Ansible Galaxy also allows role creation. This has the advantage of initializing a series of folders and files that will make our roles follow the standards established for Ansible development and followed by the Ansible community.
To create a role, on the folder we roleswill execute the following command to create a role called itinfs.apache. We will follow as a naming rule a username (eg the username in Ansible Galaxy) followed by a period ( .) and the name of the role. Thus, we could have several similar roles, but from different users and use each of them as appropriate.
$ ansible-galaxy init itinfs.apache
This will create the following structure:
itinfs.apache
├── defaults #1
│   └── main.yml
├── files #2
├── handlers #3
│   └── main.yml
├── meta #4
│   └── main.yml
├── README.md #5
├── tasks #6
│   └── main.yml
├── templates #7
├── tests #8
│   ├── inventory
│   └── test.yml
└── vars #9
    └── main.yml

  1. Default values for variables used in the role. They will be overwritten by those defined in vars
  2. Files required for role execution. These files, unlike those located in templates cannot be manipulated.
  3. Handlers folder with pending execution tasks generated by notify tasks already executed (eg restarting services after a configuration change)
  4. Metadata that Ansible Galaxy uses to publish the role (eg minimum version of Ansible, supported platforms, dependencies, …)
  5. Descriptive and usage information for the role
  6. Role tasks
  7. Files to process in the deployment process and that will be modified according to the variables used
  8. Test cases to support continuous integration systems such as Jenkins or Travis
  9. Variables used in the role. They overwrite those that appear in defaults

For example, we can include the following task in the file tasks/main.yml to ensure that Apache is installed.

---
# tasks file for itinfs.apache
- name: Install Apache
  apt: name=apache2 state=present
Organization of playbooks and roles
Over time, the folder roles will grow with the roles used and developed. All of them will be reused in the different projects in which they are useful. Below is an example of the proposed organization for playbooks and roles.
.
├── playbooks #1
│   ├── nginx-hosts.cfg
│   ├── nginx-playbook.yml
│   ├── php-hosts.cfg
│   ├── php-playbook.yml
│   ├── phpwebserver-hosts.cfg
│   └── phpwebserver-playbook.yml
└── roles #2
    ├── testrole.git #3
    │   ├── ...
    ├── testrole.php #4
    │   ├── ...
    └── itinfs.apache #5
        ├── ...

  1. Folder for playbooks and inventory files
  2. Role folder
  3. Git install role
  4. PHP install role
  5. Apache installation own role

If now we want to develop a playbook with Apache and PHP that uses the roles itinfs.apacheand testrole.php, it would be enough to create a new playbook like the following
Example: Playbook phpwebserver-playbook.yml for the installation of an Apache and PHP web server

---
- hosts: all
  become: true
  roles:
    - ../roles/itinfs.apache
    - ../roles/testrole.php
To run it, from the playbooks folder we would write:
$ ansible-playbook -i phpwebserver-hosts.cfg phpwebserver-playbook.yml #1 

  1. The file phpwebserver-hosts.cfgwould contain the list of hosts you want to run the playbook on

You could also move the inventory files out of the playbooks folder and put them in a separate folder (eg inventory).

No comments

Powered by Blogger.