Saturday, March 15, 2025
CONFIGURATION MANAGEMENT
ANSIBLE – ANSIBLE – ANSIBLE – ANSIBLE
what we will cover in this section.
a. Ansible introduction
b. Ansible inventory
c. Type of ansible inventory
d. How to create ansible – static inventory
e. Practicing using the LionTech Ansible lab
f. Ansible modules - via ansible adhoc commands.
g. Ansible dynamic inventory
h. Ansible playbooks
i. Ansible roles
j. Ansible vaults
k. Ansible galaxy
l. Ansible pipeline
m.
Ansible best practices and certifications.
1. WHAT IS ANSIBLE:
1. Ansible is used to deploy and automate configurations to
remote servers [ remote hosts]
2. Ansible is a configuratio management tool [ technologyies]
3. Ansible is agentless.
4. Ansible uses the SSH protocol for communication with
the remote hosts.
5.
Examples : deploy whatsap deskstop to all the computers in
lab034 [ 5000 computers]
- 2000 = ubuntu
- 1000 = redhat
- 1000 = windows
- 1000 = macos
Ansible master [ controller] : this is the server on which
ansible is installed.
Ansible hosts/inventory -remote hosts.
Remote hosts in ansible are hosts/servers to be managed by
the ansible master/controller.
Ansible Local host : this is the server on which ansible is
installed.
ANSIBLE AND PYTHON:
Ansible is built on python. We will be using python modules to
manage remote hosts .
ANSIBLE AND DEVOPS.
Ansible is used for ;
- application deployment, task automation, and IT
orchestration.
CREATING AND ESTASBLISHING ANSIBLE STATIC INVENTORY
REMOTES HOSTS.
A. JOINING THE LIONTECH LAB ENVIRONMENT FOR HANDS ON
1. Log into the ansible server as ansible using password 4040
ssh ansible@-. Login usingf any ssh tool [ git bash , wsl ]
3. Create your own user name on the master control and login using
your username
# sudo adduser devops
4. Switch to devops user account
ssh devops@-. Create your inventory file with the touch command
# touch inventory
6. Paste the inventory content into the inventory file
7. Create keys pairs for your user.
# ssh-keygen
8. Test connection using the ping module
# ansible [all] -m [m =module ] -I [ I = inventory]
# ansible all -m ping -I inventory
9. Clone the ansible reponsitory
# https://github.com/Lion-Technology-Solutions/ansible.git
CONSTRUCTING STATIC INVENTORY FILE
10 MINUTES BREAK
1. Create 6 more remote hosts and classify under two inventory
groups [ stage and prod]
[stage]- ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/maven-key.pem-
ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/maven-key.pem- ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/maven-key.pem
[prod]- ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/maven-key.pem- ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/maven-key.pem- ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/maven-key.pem
Test connection using the ping module .
# ansible prod -m ping -I inventory
# ansible stage -m ping -I inventory
2. Deploy the user ‘devops’ to the prod and stage hosts
STATIC INVENTORY GROUPS
1. # dev
2. # db
3. # stage
4. # prod
ANSIBLE MODULES - ANSIBLE ADHOC COMMANDS
Adhoc commds are a powerful way yo perform quick tasks on remote
hosts without writing a full playbook. Adhoc commands uses ansible
amoduels to perform specific actions
Ansible modules.
1. The ping module - This modules checks for connectivity
# ansible [host-group] -m [m =module] -I [inventory]
# ansible db -m ping -I inventory
# ansible all -m ping -I inventory
2. The shell module
The shell module is used to execute shell commands on
remote hosts
e.g : check for uptime on specific servers
# ansible all -m shell -a “uptime”
# ansible all -m shell “mkdir ansible-projects”
# ansible -m shell “ls -ltr”
3. The command module
The command module execute ceertain commandon remote
hosts
# ansible all -m command -a “df -h”
# ansible all -m commad -a “free -m”
4. The copy module : this is used to copy files or directories
from master to remote controller
Parameters with copy module.
- File must exist
- Define source
- Define the destination on the remote host.
e.g : create a file called your name-demo
copy the file to all the remote servers in a directory called demo
solutions:
1. Create the file ;
# touch prince-demo
Destination: /devops/ ansible-projects
# ansible all -m copy
-a "src=prince-demo dest=/home/devops/ansible-projects" -i
inventory
Sunday, March 16, 2025
1.
Ansible modules – Adhoc commands part 2
Best Practices for Adhoc commands
a. The use of admin previllages.
- Become root : this allows you to execute admin tasks
Options : -b
--become
# ansible all -m user -a “adduser demond” –become [-b]
2.
Ansible modules continuation:
1. File module
This module is used to manage files and directories on
remote hosts.
Manage files:
- Create
- Read
- Write
- Delete
- Edit
- Manage permissions/ permits
Example:
# ansible all -m file -a “path=path/to/directory
state=directory”
# ansible all -m file -a “path=/home/devops/march16
state=directory”
# ansible all -m shell -a "ls /home/shella" -i inventory
# ansible all -m file -a "path=/home/ubuntu/march16
state=directory" -i inventory
2. The package module
Package management
Operating system Package manager Examples
Windows os
# chocolatey
# choco install git
Ubuntu
# apt
# sudo apt install
git
Redhat
# yum
# yum install git
# macos
# homebrew
# brew
a. # yum module
This is used to manage package on redhat based systems
# ansible all -m yum -a “name=git state=present” -I
inventory
b.The apt module
This is used to manage packages on Debian-Based systems
# ansible all -m apt -a “update_cache=yes” -I inventory
3.
The service module
The service module manage services on the remote hosts
Examples: start and restart apache /. Tomcat
# ansible all -m service -a “name=sonarqube
state=started”
The Setup module
This module father facts about the remote hosts
4.
# ansible all -m setup
The user module
# this is used manage users on the remote hosts
5.
Add a user:
# ansible all -m user -a “name=fai state=present” -I
inventory -b
Delete the user
# ansible all -m user -a “name=fai state=absent” -I
inventory -b
15 minute break
7:50pm
1.
Ansible playbooks and Ansible playbooks components
What is an ansible playbook:
- A playbook is a YAML file that defines a set of tasks ,
roles and configurations to be applied to remote hosts.
# touch 01-deploy-git.yaml
# what is the different between a play and task in ansible?
# what is your experience with ansible playbooks?
- Create, manage and maintain playbooks.
Update git version to version-
- Playbooks are the core of ansible automations ,
allowing us to describe the desired state of our
infrastructure.
PLAYBOOK STRUCUTRE/COMPONENTS:
1. Hosts: the target hostss/servers or groups from the
inventory. Hosts = hosts = Host
2. Tasks: the actions to be performed on the hosts
3. The variables: customer values used to configure tasks.
4. Roles:
# KEY COMPONENTS OF A PLAYBOOK.
1. Hosts: the servers in the inventory.
2. Tasks
- Each task calls an ansible module
# create a user = user module
# create a file /directory = file
3. Variables
# sonarqube_port = 9000 / 9001
# sonarqube_user = sonar / elvis / paul
# username = paul / john
4. Roles
5. Handlers