Ansible - Part1
Ansible - Part1
Ansible
Agenda
Ansible
● Introduction Instructions / Notes
● Starts: 16 May, 7:30am PDT/PST or 16 May, 8pm IST
● Ansible pre-requiste
● 2 Hours session with 10 minutes Break
● Inventory Management
● Except Instructor, everyone is on mute
● Adhoc Command ● Please use the Q/A Window to Ask Questions
● Playbook ● The recording & slides will be shared after session
● Roles
● Ansible Vault
● Build K8 Cluster using Ansible
Ashok Singh
Ansible
Introduction
Ansible
Learning Objective
● What is ansible?
● Ansible terminologies
● Ansible Installation
Ansible
What is ansible?
Ansible
Why do we use ansible?
● Ansible is open source
● Ansible is agentless
● Developed in Python
● Large community
Ansible
Ansible Terminologies
● PLAYBOOKS
● TASK
● ACTION
● FACTS
● HOSTS
● GROUPS
● HANDLERS
● AD-HOC
Ansible
Ansible Terminologies
● INVENTORY ● ROLES
● MODULES ● TEMPLATES
● LIBRARY ● WHEN
● NOTIFY ● YAML
● ORCHESTRATION
● PUSHMODE
● PULLMODE
Ansible
Ansible Installation
Using Apt
• apt-get install ansible
Using Yum
• yum install ansible
Ansible
Lab01: Launch new EC2 from bastion
Task: Prepare host and validate it by launching playbook that will create a new
ec2 instance
Step1 : Launch EC2 instance . Tag it as bastion and connect to bastion host
ashoksingh@Ashoks-Air ~/ ssh -i aws-key.pem ec2-user@<PUBLIC_IP>
The authenticity of host '3.8.176.4 (3.8.176.4)' can't be established.
ECDSA key fingerprint is SHA256:qLP4m9hCsRZc2Nnz1kWn0PgjdLay4GyZmzxz+RggHfY.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '3.8.176.4' (ECDSA) to the list of known hosts.
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip-172-31-20-142 ~]$
Ansible
Lab01: Launch new EC2 from bastion
Ansible
Lab01: Launch new EC2 from bastion
Step5 : Create virtual environment for python3
[root@ip-172-31-24-224 aws-ansible]# python3 -m venv ansible
[root@ip-172-31-24-224 aws-ansible]# source /root/aws-ansible/ansible/bin/activate
(ansible) [root@ip-172-31-24-224 aws-ansible]# pip install pip --upgrade
Ansible
Lab01: Launch new EC2 from bastion
Step7 : Configure aws credentials
(ansible) [root@ip-172-31-24-224 aws-ansible]# aws configure
AWS Access Key ID [None]: XXXXXXXXXXXXXXXXXXXXXXXXX
AWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXX
Default region name [None]: eu-west-2
Default output format [None]: json
Ansible
Lab01: Launch new EC2 from bastion
Step9 : Key-pair is the ssh key used to connect to ec2-instance.
image: ami-01a6e31ac994bbc09
subnet: subnet-082213e51ccfdcaa4
Ansible
Lab01: Launch new EC2 from bastion
Step10 : Run the ansible playbook
(ansible) [root@ip-172-31-24-224 aws-ansible]# ansible-playbook -i
inventory ec2.yml
Ansible
Lab01: Launch new EC2 from bastion
Step11 : Validate new ec2 is created in AWS console.
Ansible
Set up SSH access between servers
Ansible
ssh keys based access
Ansible works on assumption that all server can be accessed by ansible
host using ssh key
Server1
Bastion Server2
Server3
Ansible
SSH Key distribution
s3 bucket
pem
aws
- key.
s
aw Server1
-ke
y.pe
u b
s a.p
m
aws-key.pem - r
id
Bastion Server2
Laptop id-rsa.pub
id-
ssh key pair rsa
.pu
id-rsa.pem b
id-rsa.pub
Server3
AWS
Ansible
SSH Key distribution
Step 1: Login to bastion host and update ec2.yaml file to launch 3 ec2 instances
...
- name: Launch instance
ec2:
count: 3
...
Ansible
Launch 3 instances
Step 1: Login to bastion host and update ec2.yaml file to launch 3 ec2 instances
...
- name: Launch instance
ec2:
count: 3
...
Ansible
Method-1: pusk aws-key to s3 bucket and copy to bastion
● push private key to s3-bucket
ashoksingh@Ashoks-Air ~/ aws s3 mb s3://devops-bucket-new
make_bucket: devops-bucket-new
ashoksingh@Ashoks-Air ~/ aws s3 cp aws-key.pem s3://devops-bucket-new
upload: ./aws-key.pem to s3://devops-bucket-new/aws-key.pem
ashoksingh@Ashoks-Air ~/ aws s3 ls
2020-05-15 10:40:50 devops-bucket-new
Ansible
Method-2: copy aws-key private key to bastion
● Copy private key
Ansible
Test connectivity from bastion to server
● Connect to server1
[root@ip-172-31-24-224 aws-ansible]# ssh -i aws-key.pem ec2-user@3.8.157.226
(ansible) [root@ip-172-31-24-224 aws-ansible]# ssh -i aws-key.pem
ec2-user@3.8.157.226
The authenticity of host '3.8.157.226 (3.8.157.226)' can't be established.
...
[ec2-user@ip-172-31-39-247 ~]$
● Connect to server2
[root@ip-172-31-24-224 aws-ansible]# ssh -i aws-key.pem ec2-user@18.132.39.66
(ansible) [root@ip-172-31-24-224 aws-ansible]# ssh -i aws-key.pem
ec2-user@3.8.157.226
The authenticity of host '3.8.157.226 (3.8.157.226)' can't be established.
...
[ec2-user@ip-172-31-40-250 ~]$
Ansible
key pair
ssh is based on public and private key . recommended bit size 4096
Ansible
Generate ssh key pair
Step 3: Login to bastion host generate key pair
(ansible) [root@ip-172-31-24-224 aws-ansible]# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:btj2YMEDuvHNa3K9x1OKm1L/FR0Fq+zyUy9Lz1gsISE
root@ip-172-31-24-224.eu-west-2.compute.internal
The key's randomart image is:
+---[RSA 4096]----+
..
..
+----[SHA256]-----+
(ansible) [root@ip-172-31-24-224 aws-ansible]# ls -l ~/.ssh/id*
-rw------- 1 root root 3247 May 15 01:27 /root/.ssh/id_rsa
-rw-r--r-- 1 root root 774 May 15 01:27 /root/.ssh/id_rsa.pub
(ansible) [root@ip-172-31-24-224 aws-ansible]#
Ansible
Copy new public key on servers
● copy public key to all server. Below is example of server1
(ansible) [root@ip-172-31-24-224 aws-ansible]#
cat ~/.ssh/id_rsa.pub | ssh -i aws-key.pem ec2-user@3.8.157.226 "cat >>
~/.ssh/authorized_keys"
(ansible) [root@ip-172-31-24-224 aws-ansible]# ssh ec2-user@172.31.39.247
Last login: Fri May 15 15:31:35 2020 from 3.8.125.192
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
1 package(s) needed for security, out of 10 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-39-247 ~]$
Ansible
Copy new public key on servers
● copy public key to all server. Below is example of server1
(ansible) [root@ip-172-31-24-224 aws-ansible]#
cat ~/.ssh/id_rsa.pub | ssh -i aws-key.pem ec2-user@3.8.157.226 "cat >>
~/.ssh/authorized_keys"
(ansible) [root@ip-172-31-24-224 aws-ansible]# ssh ec2-user@3.8.157.226
Last login: Fri May 15 15:31:35 2020 from 3.8.125.192
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
1 package(s) needed for security, out of 10 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-39-247 ~]$
Ansible
Copy new public key on servers
● copy public key server2 and server3
(ansible) [root@ip-172-31-24-224 aws-ansible]#
cat ~/.ssh/id_rsa.pub | ssh -i aws-key.pem ec2-user@<IP_SERVER2> "cat >>
~/.ssh/authorized_keys"
(ansible) [root@ip-172-31-24-224 aws-ansible]#
cat ~/.ssh/id_rsa.pub | ssh -i aws-key.pem ec2-user@<IP_SERVER3> "cat >>
~/.ssh/authorized_keys"
Ansible
Ansible ping test
Create a host file with list of IP address
(ansible) [root@ip-172-31-21-72 aws-ansible]# cat hosts
[servers]
172.31.44.11
172.31.40.165
172.31.41.94
[external]
35.176.249.69
3.9.178.160
3.8.28.212
(ansible) [root@ip-172-31-21-72 aws-ansible]# ansible -m ping -i hosts
servers -u ec2-user
Ansible
Inventory Management
Ansible
What is Inventory ?
Ansible works against multiple managed nodes or "hosts" in your
infrastructure at the same time, using a list or group of lists known as
inventory
Source: Ansible.com
Ansible
Inventory file
● Default location and file is
○ /etc/ansible/hosts
● Custom inventory files can be specified command line with option “-i”
○ -i <path>
Ansible
Inventory file format
● Host are grouped in heading in square bracket in INI format of indentation in YAML
all: mail.example.com
hosts:
mail.example.com: [webservers]
children: foo.example.com
webservers: bar.example.com
hosts:
foo.example.com: [dbservers]
bar.example.com: one.example.com
dbservers: two.example.com
hosts: three.example.com
one.example.com:
two.example.com:
three.example.com:
INI
YAML
Ansible
Host Ranges
[webservers]
www01.example.com
www02.example.com [webservers]
www[01:04].example.com
www03.example.com
www04.example.com
[dbservers]
db-a.example.com
[dbservers]
db-b.example.com db[a:c].example.com
db-c.example.com
Ansible
Host and Group Variables
● Assign a variable to a single host, then use it later in playbooks
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=myuser
● If all hosts in a group share a variable value, you can apply that variable
to an entire group at once
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
Ansible
Inventory :Best Practise
Each host can be in more than one group. For example a production
appserver in a datacenter in Atlanta might be included in groups called
[prod] and [atlanta] and [appservers]. You can create groups that track:
● What - An application, stack or microservice. (For example, database
servers, web servers, etc).
● Where - A datacenter or region, to talk to local DNS, storage, etc. (For
example, east, west).
● When - The development stage, to avoid testing on production
resources. (For example, prod, test).
Ansible
What , where and when sample Inventory
all: west:
hosts: hosts:
mail.example.com: bar.example.com:
children: three.example.com:
webservers: prod:
hosts: children:
foo.example.com: east:
bar.example.com: test:
dbservers: children:
hosts: west:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
Ansible
Adhoc Command
Ansible
What is Adhoc Command ?
Ad-hoc commands are task which are not executed rarely , but on all
server. These are one liner command that is executed on specified hosts
Ansible
User Case 1: Executing shell command
● Install git on all group of host called servers
Complete!
Ansible
User Case 2: Managing files
● Copy file from host to remove servers
Ansible
User Case 4: Managing users and groups
● create, manage, and remove user accounts on your managed nodes with
ad-hoc tasks:
Ansible
User Case 5: Managing services
● Ensure a service is started on all servers:
$ ansible servers -m service -a "name=httpd state=started" -i host
--become
Ansible
User Case 6: Gathering facts
Facts are discovered variables on remote host . They are uses to implement
conditional execution of tasks but also to get information about your systems.
Ansible
Playbook
Ansible
What is Playbook ?
● Playbooks are Ansible's configuration, deployment, and
orchestration language.
● Running playbooks
○ ansible-playbook playbook.yml -f 10
Ansible
Playbook - ec2.yml
---
- name: Create a sandbox instance
hosts: localhost
gather_facts: False
vars:
keypair: aws-key
instance_type: t2.micro
security_group: default
image: ami-09e5afc68eed60ef4
region: eu-west-2
subnet: subnet-082213e51ccfdcaa4
Ansible
Playbook - ec2.yml
tasks:
- name: Launch instance
ec2:
key_name: "{{ keypair }}"
group: "{{ security_group }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
wait: true
region: "{{ region }}"
vpc_subnet_id: "{{ subnet }}"
assign_public_ip: yes
register: ec2
Ansible
Ansible module parameter (help?)
ansible-doc <module-name>
ansible-doc yum
Ansible
Lab02: Create a playbook to deploy flaskapp
TASK:1 - Write a task to deploy Deploy docker. Copy below content in
flaskapp.yml file
---
- hosts: centos
become: true
tasks:
- name: Install docker
yum: name=docker state=latest
Ansible
Lab02: Create a playbook to deploy flaskapp
TASK:3- Run the playbook to deploy docker on remote host - centos
(ansible) [root@ip-172-31-21-72 aws-ansible]# ansible-playbook flask-app.yml -i host
PLAY RECAP
***********************************************************
172.31.35.243 : ok=2 changed=1 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
Ansible
Lab02: Create a playbook to deploy flaskapp
TASK:4- Add Task to start docker service
- name: Start docker service
service: name=docker state=started
Ansible
Lab02: Create a playbook to deploy flaskapp
TASK:7- Run the playbook
(ansible) [root@ip-172-31-21-72 aws-ansible]# ansible-playbook flask-app.yml -i host
Ansible
Lab02: Create a playbook to deploy flaskapp
TASK:7- Access the flaskapp url
http://www.<public-url>:80
Ansible
Verifying playbook
● playbooks validation can be done with option --check, --diff, --list-hosts,
--list-tasks and --syntax-check
(
Ansible
List hosts in playbook
● List all the host which are impacted in playbook
playbook: flask-app.yml
Ansible
List tasks in playbook
● List all the tasks from
playbook: flask-app.yml
Ansible
Handlers- playbook
● Handlers are tasks that only run
when notified.
Ansible
Configuration management
Ansible
Ansible Configuration file
● Configuration file define # config file for ansible -- https://ansible.com/
# ===============================================
location of other input file
or default parameter # nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first
● Default file location
[defaults]
/etc/ansible/ansible.cfg
# some basic default values...
inventory = new-hosts
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
Ansible
Override inventory file
● Check which configuration is used by ansible
(ansible) [root@ip-172-31-21-72 aws-ansible]# ansible -v -m shell -a "ls" all
Using /etc/ansible/ansible.cfg as config file
172.31.35.243 | CHANGED | rc=0 >>
Ansible
Override inventory file
● Create configuration in local directory and specify path for inventory file
Ansible
create a new inventory file
● copy default host file to local directory and edit it to point to wrong ip address
(ansible) [root@ip-172-31-21-72 aws-ansible]# cp /etc/ansible/hosts new-hosts
(ansible) [root@ip-172-31-21-72 aws-ansible]# echo 172.22.22.22 >> new-hosts
Ansible
Configuration Management
● Different hosts need different type of configuration parameter
● Organise configuration file and inventory file in different directory structure
Ansible