Ansible Full Course: For Beginners
Ansible Full Course: For Beginners
For Beginners
IT Automation Simplified
Learn.sandipdas.in
What is Asible?
Ansible Concepts
Ansible Modules
Ansible Playbook
Ansible Vault
Ansible Galaxy
What is Ansible?
Ansible delivers simple IT automation that ends repetitive
tasks and frees up DevOps teams for more strategic work.
Ansible Concepts
Control Node Managed nodes
Any machine with Ansible installed. We can run Ansible
The network devices (and/or servers) we manage with Ansible.
commands and playbooks by invoking the ansible or ansible-
Managed nodes are also sometimes called “hosts”. Ansible is not
playbook command from any control node. We can use any
installed on managed nodes.
computer that has a Python installation as a control node -
laptops, shared desktops, and servers can all run Ansible.
However, We cannot use a Windows machine as a control node. Inventory
We can have multiple control nodes as well. A list of managed nodes. An inventory file is also sometimes called
a “hostfile”. Our inventory can specify information like IP address
for each managed node. An inventory can also organize managed
Collections
nodes, creating and nesting groups for easier scaling.
Collections are a distribution format for Ansible content that can
typically located at /etc/ansible/hosts, provide a custom inventory
include playbooks, roles, modules, and plugins. We can install and
path using the -i parameter when running commands & playbooks
use collections through Ansible Galaxy
Tasks Modules
The units of action in Ansible. We can execute a single task once
with an ad hoc command. The units of code Ansible executes. Each module has a
particular use, from administering users on a specific type
Playbooks of database to managing VLAN interfaces on a specific type
Ordered lists of tasks, saved so we can run those tasks in of network device. We can invoke a single module with a
that order repeatedly. Playbooks can include variables as task, or invoke several different modules in a playbook.
well as tasks. Playbooks are written in YAML and are easy to
read, write, share and understand
Learn.sandipdas.in
SetUp There are multiple ways to install Ansible, here showing Ubuntu
Example:
$ sudo apt update
A system where the Ansible is
$ sudo apt install software-properties-common
installed and configured to $ sudo add-apt-repository --yes --update ppa:ansible/ansible
connect and execute commands $ sudo apt install ansible
on nodes.
Check Ansible version
Generating Custom SSH Keys
Setting up ssh: ansible –version
sudo apt-get install openssh-server
Testing Connectivity With Managed Nodes
Generating new ssh keys:
Using a Custom SSH Key, checking remote connections
ssh-keygen
ansible all -m ping --private-key=~/.ssh/my_custom_key
For PLaybook:
ssh-copy-id hostname (if it's a password-
ansible-playbook myplaybook.yml --private-
based)
key=~/.ssh/my_custom_key
ssh-copy-id -i ~/.ssh/my_custom_key user@host
Using password:
ansible all -m ping --ask-pass
Time to check SSH Connection
ansible-playbook myplaybook.yml --ask-pass
ssh -i ~/.ssh/my_custom_key user@host
Managing Managed Invetory file example with various parameters
Node Via Inventory File path: /etc/ansible/hosts (or custom location by: -i /path/to/file)
#un-grouped
192.0.2.40
What is a managed node? 192.0.3.56
aserver.example.org
Managed node is a server (node) bserver.example.org
controlled by Ansible Controller Node #by group called appservers
[appservers]
What is Inventory? sample1.example.com ansible_host = 10.0.0.3 #ssh to 10.0.0.3
sample2.example.com ansible_ssh_user = xyz #ssh as user xyz
It's a file that contains information about the servers
#host (DNS will resolve automatically)
Ansible controls, typically located at /etc/ansible/hosts ,
[dbservers]
using the -i parameter we can
one.example.com
provide custom inventory path
two.example.com
three.example.com
Targetting hosts and groups by patterns #dev_servers1 is a group containing other groups
All hosts: all (or *) [dev_servers1:children]
10.0.0.* : All host with IP starting from 10.0.0.* appservers
ungrouped: all hosts that's not within any group dbservers
One host: host1
Example targeting hosts
Multiple hosts: host1:host2 (or host1,host2)
One group: appservers ansible appservers -m ping
Multiple groups: appservers:dbservers ansible appservers -m service -a "name=httpd state=restarted"
Excluding groups: appservers:!dbservers Note: Ansible supports inventory scripts for building dynamic inventory files, this is useful when host
The intersection of groups: appservers:&dbservers changes very often. To know more read documentation here
e.g. ansible all -m ping -i get_inventory.py Learn.sandipdas.in
Ansible Modules Useful Modules based on use cases
What is ad-hoc commands? Managing packages (Install, update and remove packages)
#using yum package manager to install and uninstall packages
ansible appservers -m ansible.builtin.yum -a "name=acme state=present"
Ad-Hoc commands are an easy way to run quick commands to ansible appservers -m ansible.builtin.yum -a "name=acme-1.5 state=present"
perform the actions, and it will not be saved for later. ansible appservers -m ansible.builtin.yum -a "name=acme state=latest"
It uses the /usr/bin/ansible command-line tool to automate a single ansible appservers -m ansible.builtin.yum -a "name=acme state=absent"
#using apt package manager to install and uninstall packages
task on one or more managed nodes.
ansible appservers -m apt -a "name=acme state=latest"
ansible appservers -m apt -a "name=acme-1.5 state=present"
Why use ad-hoc commands and use cases? Managing users and groups (adding , removing users and/or groups )
ansible all -m ansible.builtin.user -a "name=foo password=<crypted password here>"
ad hoc commands are great for tasks we repeat rarely. Below are the use cases: ansible all -m ansible.builtin.user -a "name=foo state=absent"
Syntax : Command hostgroup module/options[arguments]
Specify command : -a parameter | Specify Module: -m parameter Managing services (Start, Stop, Restart Services)
Rebooting servers ansible appservers -m ansible.builtin.service -a "name=httpd state=started"
#reboot all servers in appservers group ansible appservers -m ansible.builtin.service -a "name=httpd state=restarted"
ansible appservers -a "/sbin/reboot" ansible appservers -m ansible.builtin.service -a "name=httpd state=stopped"
#reboot the appservers hosts with 10 parallel forks
ansible appservers -a "/sbin/reboot" -f 10 Deploying From Source Control
#to run To run /usr/bin/ansible from a differet user account (not root) ansible appservers -m git -a "repo=https://foo.example.org/repo.git dest=/src/myapp version=HEAD"
ansible appservers -a "/sbin/reboot" -f 10 -u username Gathering facts
#run commands through privilege escalation ansible all -m ansible.builtin.setup
ansible appservers -a "/sbin/reboot" -f 10 -u username --become [--ask-become-
pass] Click here to know more about Build In Modules Learn.sandipdas.in
Ansible Playbook Ansible Playbook Components
Ansible Playbook is ordered lists of tasks, saved so hosts: Use hosts keyword to target hosts/servers by hostname, group
we can run those tasks in that order repeatedly. name, or any pattern
Playbooks in Ansible are written in YAML format ad Variables: The Variables are the way for Ansible to pass custom values in
easy to read. YAML means "Yet Another Markup tasks. We can define these variables in our playbooks, in our inventory, in
Language". Every YAML file starts with ---. Playbooks re-usable files or roles, or at the command line.
usually stored in source code control e.g. git
Ansible variable is defined in group_vars, host_vars, role vars, CLI vars and
is called in Jinja Templating way: {{ my_variable }}. You can call variables
everywhere in Ansible (tasks, variables, template, ...)
You can have 3 types of variables:
String
List
Dictionary
Example:
Key-Value
Ansible-playbook release.yml --extra-vars "version=1.23.45
other_variable=foo"
Json:
ansible-playbook release.yml --extra-vars
'{"version":"1.23.45","other_variable":"foo"}'
ansible-playbook arcade.yml --extra-vars '{"pacman":"mrs","ghosts":
["inky","pinky","clyde","sue"]}'
From File:
ansible-playbook release.yml --extra-vars "@some_file.json"
Learn.sandipdas.in
Ansible Playbook Tasks What is Ansible Playbook Task?
The Tasks are the actions launched on remote Hosts. Tasks are
written in YAML langage in a descriptive structure way making
the read and write uniform through any tasks.
We can:
Execute tasks with elevated privileges or as a different user
with become
Repeat a task once for each item in a list with loops
Execute tasks on a different machine with delegation
Run tasks only when certain conditions apply with
conditionals and evaluating conditions with tests
Group a set of tasks together with blocks
Run tasks only when something has changed with handlers
Learn.sandipdas.in
Ansible Playbook Handlers
What is Ansible Playbook Handlers?
Ansible Handlers are action triggers called from tasks and run at the end
of a play. A Handler is a task(s) defined by its name and called with its
name.
We can: Trigger Multiple Handlers Use Variables in Handlers
Learn.sandipdas.in
Ansible Playbook Roles
What is Ansible Playbook Roles?
Learn.sandipdas.in
Run Ansible Playbook Running Playbook
Verifying playbooks
Get Infos:
ansible-playbook <YAML> --list-hosts
ansible-playbook <YAML> --list-tasks
Syntax Check
ansible-playbook --syntax-check <YAML>
Learn.sandipdas.in
Ansible Vault Working With Ansible Vault
What is Ansible Vault?
Creating a New Encrypted File
If our Ansible playbooks deal with sensitive data like ansible-vault create credentials.yml
passwords, API keys, and credentials, it is important to
keep that data safe by using an encryption mechanism. Encrypting an Existing Ansible File
Ansible provides ansible-vault to encrypt files and ansible-vault encrypt credentials.yml
variables.
View encrypted file
ansible-vault view credentials.yml
After encrypting a file with this tool, we will only be able
to execute, edit or view its contents by providing the Edit encrypted file
relevant password defined when we first encrypted the ansible-vault edit credentials.yml
file.
Permanently Decrypt a file
Running Playbook with Vault ansible-vault decrypt credentials.yml
Learn.sandipdas.in
Learn.sandipdas.in
Contact Me [email protected]