RED HAT SYSTEM ADMINISTRATION III:
LINUX AUTOMATION
Content
ORIENTATION TO THE CLASSROOM ENVIRONMENT
WHAT IS ANSIBLE?
Ansible is an open-source automation platform. It is a simple automation language that can
perfectly describe an IT application infrastructure in Ansible Playbooks. It is also an automation
engine that runs Ansible Playbooks.
ANSIBLE CONCEPTS AND ARCHITECTURE
There are two types of machines in the Ansible architecture: control nodes and managed hosts.
Ansible is installed and run from a control node. Managed hosts are listed in an inventory, which
also organizes those systems into groups for easier collective management.
The Ansible architecture is agentless. Typically, when an administrator runs an Ansible
Playbook or an ad hoc command, the control node connects to the managed host using SSH
(by default) or WinRM.
Installing Ansible
[student@workstation ~]$ sudo yum install ansible
[student@workstation ~]$ ansible --version
Chapter 02
DEPLOYING ANSIBLE
Configure Ansible to manage hosts and run ad hoc
Ansible commands
DEFINING THE INVENTORY
An inventory defines a collection of hosts that Ansible will manage. These hosts can
also be assigned to groups, which can be managed collectively. Groups can contain
child groups and hosts can be members of multiple groups. The inventory can also set
variables that apply to the hosts and groups that it defines.
Default inventory: /etc/ansible/hosts
Custom: You can define inventory anywhere on the server but the best practice define
your inventory in your working directory or use defaults ones
CREATE INVENTORY FILE
Ungrouped hosts | grouped hosts | nested group | range specify
-------------------------------------------------------------------------------------------------
[Link]
[Link]
[Link]
[dev]
[Link]
[Link]
[prod]
[Link]
[web]
server[a:d].[Link]
[com:children]
dev
prod
--------------------------------------------------------------------------------------------------
VERIFY INVENTORY
Using system default ansible inventory file
[student@workstation ~]$ ansible dev --list-hosts
[student@workstation ~]$ ansible proc --list-hosts
[student@workstation ~]$ ansible all --list-hosts
[student@workstation ~]$ ansible localhost --list-hosts
[student@workstation ~]$ ansible ungrouped --list-hosts
Using a custom ansible inventory file
[student@workstation ~]$ ansible dev -i /home/dep/inventory --list-hosts
[student@workstation ~]$ ansible proc -i /home/dep/inventory --list-hosts
[student@workstation ~]$ ansible all -i inventory --list-hosts
[student@workstation ~]$ ansible ungrouped -i inventory --list-hosts
MANAGING ANSIBLE CONFIGURATION FILES
The behavior of an Ansible installation can be customized by modifying settings in the
Ansible configuration file. Ansible chooses its configuration file from one of several
possible locations on the control node.
[Link] file priority
1 currect working Directory
2 home directory but it need to define hidden ansible file
vim /home/thilanka/.[Link]
3 ansible orginal location
/etc/ansible/[Link]
it look at the original inventory file place /etc/ansible/hosts
ansible --version | ansible server --list-hosts -v | - both display the active Ansible
configuration file
ANSIBLE CFG FILE WITH PRIVILEGE ESCALATION
[defaults]
Inventory=inventory
Remote_user=devops
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
RUNNING AD HOC COMMANDS WITH ANSIBLE
An ad hoc command is a way of executing a single Ansible task quickly, one that you do
not need to save to run again later. They are simple, online operations that can be run
without writing a playbook.
ansible host-pattern -m module [-a 'module arguments'] [-i inventory]
-o option : display the output of Ansible ad hoc commands in a single line format
Ex:
ansible all -m file -a 'path=/tmp/abcfile state=touch' -i Inventory -o
ansible dev -m dnf -a 'name=nano state=latest' -u devops -b
ansible dev -m file -a 'path=/opt/[Link] state=touch' -u thilanka -b
ansible localhost -m copy -a 'content="Managed by Ansible123\n" dest=/etc/motd' -u thilanka -b
ansible all -m command -a "df -h " -u devops -b
VIMRC FILE CONFIGURATION
vim ~/.vimrc
set ai ts=2 et
save and exit
GRANT SUDO PRIVILEGE & COPY SSH-IDS
#ssh-copy-id jone@[Link]
grant sudo privilages
01 sudo usermod -aG wheel jone
02 vim /etc/sudoers
03 vim /etc/sudoers.d
CHAPTER 3
IMPLEMENTING PLAYBOOKS
Write a simple Ansible Playbook and run it to automate tasks on
multiple hosts.
Executing a Dry Run
You can use the -C option to perform a dry run of the playbook execution. This causes Ansible
to report what changes would have occurred if the playbook were executed, but does not make
any actual changes to managed hosts.
ansible-playbook -C [Link]
ansible-playbook --syntax-check [Link]
ansible-playbook [Link]
ansible-playbook [Link] -vvv
Perform a series of task in single playbook
ANSIBLE DOC
Ansible-doc -l: list all modules
ansible-doc yum: specific module
ansible-doc -s yum: example output -s option
The ansible-doc command also offers the -s option, which produces example output that
can serve as a model for how to use a particular module in a playbook. This output can serve as
a starter template, which can be included in a playbook to implement the module for task
execution.
Multi playbook
CHAPTER 4
MANAGING VARIABLES AND FACTS
Write playbooks that use variables to simplify the management of the
playbook, and facts to reference information about the managed
hosts