Ansible Galaxy

No section on Ansible roles would be complete without a mention of Ansible Galaxy. Ansible Galaxy is a community-driven collection of Ansible roles, hosted by Ansible at https://galaxy.ansible.com/. It contains a great many community-contributed Ansible roles, and if you can conceive of an automation task, there is a good chance someone has already written a role to do exactly what you want it to do. It is well worth exploring and can get your automation project off the ground quickly as you can start work with a set of ready-made roles.

In addition to the web site, the ansible-galaxy client is included in Ansible, and this provides a quick and convenient way for you to download and deploy roles into your playbook structure. Let's say that you want to update the message of the day (MOTD) on your target hoststhis is surely something that somebody has already figured out. A quick search on the Ansible Galaxy website returns (at the time of writing) 106 roles for setting the MOTD. If we want to use one of these, we could download it into our roles directory using the following command:

$ ansible-galaxy role install -p roles/ arillso.motd

That's all you need to doonce the download is complete, you can import or include the role in your playbook just as you would for the manually created roles we have discussed in this chapter. Note that if you don't specify -p roles/, ansible-galaxy installs the roles into ~/.ansible/roles, the central roles directory for your user account. This might be what you want, of course, but if you want the role downloaded directly into your playbook directory structure, you would add this parameter.

Another neat trick is to use ansible-galaxy to create an empty role directory structure for you to create your own roles inthis saves all of the manual directory and file creation we have been undertaking in this chapter, as in this example:

$ ansible-galaxy role init --init-path roles/ testrole
- Role testrole was created successfully
$ tree roles/testrole/
roles/testrole/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml

That should give you enough information to get started on your journey into Ansible roles. I cannot stress highly enough how important it is to develop your code as roles—it might not seem important initially, but as your automation use cases expand, and your requirement to reuse code grows, you will be glad that you did. In the next section, let's expand our look at Ansible playbooks with a discussion of the ways in which conditional logic can be used in your Ansible code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.87.251