Ansible 407 Prep
Ansible 407 Prep
Ansible 407 Prep
This exam is based on Red Hat® Enterprise Linux® 7.3 and Ansible 2.3.
Ansible tips
If you decide to use Ansible, make sure you set the proper configurations in the
/etc/ansible/ansible.cfg file. The bare minimum if you're using ssh keys (which I
highly recommend) instead of using root is set the following settings:
host_key_checking = False
Ansible has two components: Ansible Core and Ansible Tower. Core provides the Ansible
runtime that executes
Ansible keeps track of all of the servers that it knows about through a "hosts" file.
We need to set up this file first before we can begin to communicate with our other
computers.
sudo vi /etc/ansible/hosts
On the server2, edit the /etc/ssh/sshd_config file and set the following options:
PasswordAuthentication no
PubkeyAuthentication yes
Ansible uses playbook to describe automation jobs and uses YAML – It is in human
readable form
Ansible Galaxy is Ansible’s official community hub for sharing Ansible roles. A role is
the Ansible way of bundling automation content and making it reusable.
1. learn by example looking at the code other people have written and;
2. develop great playbooks by orchestrating roles that have been created by the
community.
Ansible Basics
Ansible Engine – The Ansible runtime and CLI which executes playbooks. The
engine runs on a management node, from where you want to drive automation.
Playbook – A series or group of tasks that automate a process. Playbooks are
written in YAML.
Inventory – Determines what hosts you want to run playbook against. Inventories
can be dynamic or static.
Configuration – The configuration for Ansible is stored in ansible.cfg by
default under /etc/ansible/ansible.cfg. It determines default settings for
Ansible, where the inventory is located and much more. You can have many
ansible.cfg files.
Role – Ansible way to build re-usable automation components. A role groups
together playbooks, templates, variables and everything needed to automate a
given process in a pre-packaged unit of work.
Ansible Galaxy – Ansible roles provided by the Ansible community. Not only can
you get access to 1000s of roles, but you can share as well.
Ansible Modules – Used in Ansible to perform tasks, install software, enable
services, etc. Modules are provided with Ansible (core modules) and provided by
the community. They are mostly written in python which is used by Ansible
itself.
Ansible Tower – API and UI for Ansible to handle scheduling, reporting,
visualization and support teamwork for organizations running many playbooks
across many teams.
Ansible Complete Beginners Tutorial:
DONT MISS THIS IF YOU ARE SERIOUSLY INTERESTED TO LEARN "ANSIBLE" FROM BEGINNING. LINKS
ARE ARRANGED TO MAKE YOU UNDERSTAND EASILY.
Spend few minutes and start working on Ansible. Make use of it, Share to your friends
and groups.
Youtube: https://www.youtube.com/watch?v=kyS_AP0XHyU
Youtube: https://www.youtube.com/watch?v=KuTPh4GUGdc
Also there are more other videos on Puppet, Openstack cloud, VCS Cluster....
For more free documents and tutorial videos,
Like us on Facebook : https://www.facebook.com/learnitguide
Subscribe on Youtube : https://www.youtube.com/learnitguide
Follow us on Twitter : https://www.twitter.com/learnitguide
Visit our Website : https://www.learnitguide.net
####################################################################################
Exam Tips
1. Use the ansible-playbook --limit parameter, to try out your playbook against a
single host first before applying it to all your hosts.
2. Make sure you can run Ansible modules as ad-hoc commands. They can come handy when
you want to undo the effects of running an erroneous playbook.
3. Use ansible-doc --list to look up a module that can achieve your goal and ansible-
doc <module> to learn about the module parameters and example usage.
4.
Managed hosts are listed in an inventory, which also organizes those systems into
groups for easier collective management. The inventory can be defined in a static text
file, or dynamically determined by scripts that get information from external sources.
A play performs a series of tasks on the host or hosts, in the order specified by the
play. These plays are expressed in YAML format in a text file. A file that contains one
or more plays is called a playbook.
In the following example, the host inventory defines two host groups, webservers and
db-servers.
[webservers]
web1.example.com
web2.example.com
192.0.2.42
[db-servers]
db1.example.com
db2.example.com
• The all host group contains every host explicitly listed in the inventory.
• The ungrouped host group contains every host explicitly listed in the inventory that
The /etc/ansible/hosts file is considered the system's default static inventory file.
to clearly identify which version of Ansible is installed, and which configuration file
is being used.
#useradd -m mildred
#usermod -aG wheel mildred
NB: Always create a projects directory to distinguish between different tasks and add an
inventory file for every project.
Roles
A role is a collection of tasks and templates (among other things, but those are the most
common) focused on one very specific goal. For instance, you can have a role that installs
nginx, another that deploys ssh keys for admins, etc…
Nginx role will install and configure nginx. Nothing else. It won’t create DNS entries, trim
logs, add a ftp server or anything. It just installs nginx. Period.
Another one: Roles are a way to make code in playbooks reusable by putting the functionality
into generalized "libraries" that can be then used in any playbook as needed.
Roles are a way to group tasks together into one container. You could have a role for setting up
MySQL, another one for setting up Postfix etc.
A playbook defines what is happening where. This is the place where you define the hosts
(hostgroups, see below) and the roles which will be applied to those hosts.
Inventories
An inventory is a list of hosts, eventually assembled into groups, on which you will run
ansible playbooks. Ansible automatically puts all defined hosts in the aptly named group all.
For instance, you could have hosts www1 and www2, assembled in group webservers, and later
reference the group or individual hosts, depending on your needs.
Inventories can also come with variables applied to hosts or groups (including all).
Inventories can be dynamic. If the inventory file is executable, Ansible will run it and use
its output as the inventory (note that, in this case, the format is not the same as static
inventory).
You can of course have multiple inventories, segregated from each other. We will take
advantage from this later on.
Playbooks
The last piece of the puzzle is the playbook. The playbook is the pivot between and inventory
and roles. This is where you basically tell Ansible: please install roles foo, bar and baz on
machines alice, bob and charlie.
# Playbooks consist of tasks which are executed in the order which they are listed in If a
tasks fails Ansible will stop executing further tasks on that specific system.
Playbooks are written in YAML syntax which is a data serialization format like XML and JSON but much
better human readable. A convention of YAML is to start every file with three dashes (—).
A simple Playbook which makes sure the user mikhail exists might look like this.
users.yml:
---
- hosts: all
sudo: yes
tasks:
- name: Configure Users
user: name=mikhail comment='Martin and Mildred’s son' group=admins
groups=wheel
When run this playbook will make sure that the user mikhail is present, that his primary group will be
admins and that he will be member of the group wheel. If this is not the case the system will ‘make it so’
and if it’s already the case it will do nothing, this is called ‘idempotent’ in configuration management
lingo, it doesn’t matter how many times you run the command, the endstate will always be the same.
You can specify the inventory by pointing to either a file, a script, or a directory:
in command-line:
ansible-playbook playbook.yml -i /path/to/inventory
The playbook directory is simply the one in which the playbook is stored.
in ansible.cfg:
inventory = /path/to/inventory
NB: In order for that line in ansible.cfg to work it MUST be in the [defaults] section or ansible will
refuse to use it
A play is a set of tasks or roles (or both) inside a playbook. In most cases (and examples) a
playbook will contain only one single play. But you can have as many as you like. That means
you could have a playbook which will run the role postfix on the hostgroup mail_servers and
the role mysql on the hostgroup databases:
- hosts: mail_servers
roles:
- postfix
- hosts: databases
roles:
- mysql
AFAIK you have to provide the path to the playbook when invoking ansible-playbook. So ansible-
playbook someplaybook.yaml would expect someplaybook.yaml to be in you current directory.
But you can provide the full path: ansible-playbook /path/to/someplaybook.yaml