Ansible
Ansible
Ansible
Agenda
• What is Ansible?
• Why Ansible?
• How Ansible works?
• SSH
• installation
• Inventory
• Playbook
• Modules
• Roles
• Let’s write scripts
• Ansible Galaxy
2 21 January 2020
C2 General
What is Ansible?
• Ansible is an open-source configuration management and provisioning tool, similar
to Chef, Puppet or Salt.
• It uses SSH to connect to servers and run the configured Tasks. Ansible lets you
control and configure nodes from a single machine.
• What makes it different from other management software is that Ansible uses SSH
infrastructure.
• The project was founded in 2013 and bought by Red Hat in 2015.
3 21 January 2020
C2 General
Why Ansible?
• No Agent- As long as the box can be ssh’d into and it has python, it can be configured with
Ansible.
• Declarative Not Procedural- Other configuration tools tend to be procedural do this and then
do that and so on. Ansible works by you writing a description of the state of the machine that
you want and then it takes steps to fulfill that description.
• Tiny Learning Curve- Ansible is quite easy to learn. It doesn’t require any extra knowledge.
4 21 January 2020
C2 General
5
C2 General
SSH configuration
Ansible installation
Inventory
• The Inventory is a description of the nodes that can be accessed by Ansible. By
default, the Inventory is described by a configuration file, whose default location is in
/etc/ansible/hosts
• The configuration file lists either the IP address or hostname of each node that is
accessible by Ansible.
8 21 January 2020
C2 General
Example of an inventory file
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
9 21 January 2020
C2 General
Playbook
• Playbooks are simple YAML files. These files are descriptions of the desired state of
your systems.
• Ansible then does the hard work of getting your systems to that state no matter
what state they are currently in
10 21 January 2020
C2 General
Example of an ansible playbook
---
- hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: ensure apache is running
service: name=httpd state=started enabled=yes
11 21 January 2020
C2 General
Modules
• There are over 1000 modules provided by Ansible to automate every part of the
environment. Modules are like plugins that do the actual work in Ansible, they are
what gets executed in each playbook task.
• One of the guiding properties of modules is idempotency, which means that even if
an operation is repeated multiple times, it will always place the system into the
same state.
12 21 January 2020
C2 General
Example of modules
13 21 January 2020
C2 General
Roles
• Roles are a way to group tasks together into one container. We could have a role
for setting up MySQL, another one for configuring iptables etc.
• Roles makes it easy to configure hosts. Any role can be performed on any host or
group of hosts such as:
- hosts: all
roles:
- role_1
- role_2
14 21 January 2020
C2 General
Let’s write ansible scripts
• simple yum install
• remote user
• variables
• debug
• Templates
• conditions
• loops
• handlers
16 21 January 2020
C2 General
Ansible Galaxy
• Ansible Galaxy refers to the Galaxy website where users can share roles, and to a
command line tool for installing, creating and managing roles.
• The ansible-galaxy command comes bundled with Ansible, and you can use it to
install roles from Galaxy or directly from a git based SCM. You can also use it to
create a new role, remove roles, or perform tasks on the Galaxy website.
17 21 January 2020
C2 General
18
C2 General
Thanks