Ansible
Ansible
Installation
• Mainly, there are two types of machines when we
talk about deployment:
– Control machine: Machine from where we can
manage other machines.
– Remote machine: Machines which are
handled/controlled by control machine.
• There can be multiple remote machines which
are handled by one control machine. So, for
managing remote machines we have to install
Ansible on control machine.
Control Machine Requirements
• Ansible can be run from any machine with
Python 2 (versions 2.6 or 2.7) or Python 3
(versions 3.5 and higher) installed
• Playbook – the yml file where Ansible commands are written and
yml is executed on a machine.
Ansible – Playbooks
• Playbooks are the files where Ansible code is
written. Playbooks are written in YAML
format.
• Playbooks contain the steps which the user
wants to execute on a particular machine.
• Each playbook is an aggregation of one or
more plays in it.
Playbook Structure
• The function of a play is to map a set of instructions
defined against a particular host.
Tags Used
• name: This tag specifies the name of the Ansible
playbook. As in what this playbook will be doing. Any
logical name can be given to the playbook.
• Roles have no explicit setting for which host the role will
apply to.
Creating a New Role
• Role Structure:
– An Ansible role has a defined directory structure
with eight main standard directories.
– You must include at least one of these directories
in each role.
– You can omit any directories the role does not use.
– The role name is the directory name within the
/roles directory.
>cd ~/Ansible/roles
>sudo ansible-galaxy init <role-name>
Role Directory Structure
Roles Directory
• tasks/main.yml - the main list of tasks that the role executes.
• handlers/main.yml - handlers, which may be used within or outside
this role.
• library/my_module.py - modules, which may be used within this role
• defaults/main.yml - default variables for the role. These variables
have the lowest priority of any variables available, and can be easily
overridden by any other variable, including inventory variables.
• vars/main.yml - other variables for the role
• files/main.yml - files that the role deploys.
• templates/main.yml - templates that the role deploys.
• meta/main.yml - metadata for the role, including role dependencies.
Roles Files
• You can add other YAML files in some directories.
Storing and finding roles
• By default, Ansible looks for roles in the following
locations:
– in collections, if you are using them
– in a directory called roles/, relative to the playbook file
– in the configured roles_path. The default search path is
~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles.
– in the directory where the playbook file is located
---
- hosts: webservers
roles:
- role: '/path/to/my/roles/common'
Using Roles
• You can use roles in three ways: