2-Introduce Ansible
2-Introduce Ansible
(RHCE - EX294)
Introduce Ansible
What is Ansible?
• Ansible is an open-source software provisioning, configuration management, and application-
deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can
configure both Unix-like systems as well as Microsoft Windows (Wikipedia)
• In simple words: Ansible is a free automation tool that can automate IT tasks on local machine
where it is running and on remote machines
SSH
1 1 4 7 10 ……
SSH
2
SSH
2 5 8 11 ……
• Please note: Ansible is written in python language, but it does not mean you need python
knowledge to use Ansible
By: Imran Afzal
www.utclisolutions.com
What is Ansible?
• Ansible can be used to:
• Need?
• Infrastructure management (specially virtualization)
• Configuration management (System or Application configuration)
• Multi-tier application automation (e.g. app, web and db servers)
• Single point of automation (having scripts on each system vs. one
automation management platform).
• A big plus + for job seekers and those who want to level up their career
• Modules
• Module is a command meant to be executed on the client-side
• Most of the IT tasks modules are already created and can be found on Ansible website
• www.docs.ansible.com → search for module index
• www.galaxy.ansible.com
• Example of modules:
1. Install http
2. Enable http service Task
3. Start http service
• Task
• A task is a section that consists of a single procedure to be completed. A task can have
multiple modules
By: Imran Afzal
www.utclisolutions.com
Terminologies in Ansible
• Playbook
• Automation file with step-by-step execution of multiple tasks
• YAML
• A Playbook written in YAML language (Yet another markup language)
• Inventory
• File that has information about remote clients where tasks are executed
• Tag
• A reference or alias to a specific task
• Variable
• Variables are like containers that holds the defined value which can be used repetitively
• Role
• Splitting of Playbook into smaller groups. Roles let you automatically load related vars, files, tasks,
handlers, and other Ansible artifacts based on a known file structure. After you group your content in
roles, you can easily reuse them and share them with other users
By: Imran Afzal
www.utclisolutions.com
How Ansible Works?
• Each specific Task in Ansible is written through a Module(s)
• Multiple Modules are written in sequential order
• Multiple Modules for related Tasks is called a Play
• All Plays together makes a Playbook
• Playbook is written as a file format called YAML
Database-servers
• Login to db Module
Task • Create a table Module Play
• restart db Module
By: Imran Afzal
www.utclisolutions.com
How Ansible Works?
Commands examples:
To run modules through yaml file:
# ansible-playbook example.yml
Home Environment
Desktop or Laptop
Required package = Python3
VM1
VM3 Client2
Ansible VM2 Client1
Control Node
VM1 Client2
VMWare VM3
Workstation Player Virtualization Oracle VirtualBox
Control Node
3 VM1
2 Virtualization
Home lab
VM2 VM3
3 VM1 4
2 Virtualization
Home lab
By: Imran Afzal
www.utclisolutions.com
Installing Ansible
IMPORTANT: Take VM snapshot
• CentOS/Red Hat/Fedora 7
• yum/dnf install epel-release
• yum/dnf install ansible
• For version 8:
• yum install epel-release
• yum install python –y (should be installed already)
• yum install ansible ansible-doc
• Check Ansible version and run ping module without Playbook to check Ansible status
• # ansible --version
• # ansible localhost –m ping
Please note:
If you have SELinux enabled on remote nodes, you will also want to
install libselinux-python on them before using any
copy/file/template related functions in Ansible. You can use the yum
module or dnf module in Ansible to install this package on remote
systems that do not have it. By: Imran Afzal
www.utclisolutions.com
Ansible Configuration File
• Every application or package installed on an operating system comes with a configuration file
• Ansible when installed has its own configuration file.
• Some of the most common parameters that can be defined in ansible.cfg file is as follow
[defaults]
remote_user = ansible Name of the user account used to connect to the clients
host_key_checking = false Specifies whether SSH host keys should be checked. If a host
inventory = inventory is reinstalled and has a different key in ‘known_hosts’, this
will result in an error message until corrected
[privilege_escalation]
become = True Specify the location of the host file
become_method = sudo
become_user = root Whether escalation is needed for root
become_ask_pass = False
What should be used for privileged access
Prompt for root needed when running escalated commands Indicate the name of the user account used to run escalated
commands
✓ YAML Playbook files can be placed anywhere on the filesystem as long as they are being
executed with absolute path
✓ When a flat file is written in YAML format to execute tasks/plays then it is called
playbook
tasks:
Define the name of the task
- name: Install Apache httpd
yum: Run task module yum
name: httpd Name of the package
state: present
What to do? -- Install
- name: 2nd task
service:
name: httpd
state: started
• Ansible modules and options
https://docs.ansible.com/ansible/2.5/modules/
Declare task By: Imran Afzal
www.utclisolutions.com