Ansible Tutorial
Ansible Tutorial
Inventory Resource:
How to install
Ansible server ssh query:
On DigitalOcean
Create Droplet - Authentication - New SSH Key
Ansible 1
Generate public-private key on ansible server / controller computer:
$ ssh-keygen
Generating public/private rsa key pair. Enter file in which to save the key (/
Next you will be asked to create and confirm a passphrase for the key (highly recommended):
This will generate two files, by default called id_rsa and id_rsa.pub . Next, add this public key.
cat ~/.ssh/id_rsa.pub
Also:
Be able to become root user and run sudo without password.
$ vim /etc/sudoers.d/90-cloud-init-users
Ansible ping
Ansible server ssh query:
Ansible 2
Ansible commands
$ ansible all -i root@ip1,root@ip2 -m ping
$ ansible all -i root@ip1,root@ip2 -m command --args 'uptime'
$ ansible all -i root@ip1,root@ip2 -m command --args 'apt-get install -y apach
P.S. Not version-controlled by easy to update in the system, for example, when IPs change frequently.
Config file example:
//File ~/.ssh/config
Host database* dotcms* ansible server* web*
User root
IdentifyFile ~/.ssh/id_rsa_aws
Host web01
Hostname 161.35.14.48
Host web02
Hostname 67.205.181.1
[database]
database01
[dotcms]
dotcms01
[web]
web01
web02
Running ansible-playbook
Playbook is a list of commands to be run.
Ansible 3
$ ansible-playbook -i inventory playbook.yaml --vault-password-file .vault-pas
---
- hosts: all #play1
become: true
tasks:
- name: install jre #play1-task1
package: name=openjdk-8-jre state=installed
- name: group #play1-task2
group:
name: dotcms
state: present
- hosts: balancer #play2
become: true
tasks:
- name: install haproxy #play2-task1
package: name=haproxy state=installed
Ansible 4
Tasks and handlers
Tasks are the smallest units of automation.
Ansible 5
Example above, when database configuration file is copied from ansible server to remote server, then
restart the database server on remote server.
[database]
database01 ansible_host=67.205.181.1 ansible_user=root
[web]
web01 ansible_host=161.35.14.48 ansible_user=root
Ansible 6
web02 ansible_host=67.205.181.1 ansible_user=root
[dotcms]
dotcms01
Ansible Become
Allows ansible to change from root to another user to apply run commands.
[database]
database01 ansible_host=67.205.181.1
[web]
web01 ansible_host=161.35.14.48
web02 ansible_host=67.205.181.1
[balancers]
balancer01
[ubuntu:children]
database
web
[ubuntu:vars]
Ansible 7
ansible_user=root
[alpine:children]
balancers
[alpine:vars]
ansible_user=alpine_user
---
- hosts: dotcms
become: true
tasks:
- name: install jre
package: name=openjdk-8-jre state=installed
tag: dotcmstask
- name: group
group:
name: dotcms
state: present
- hosts: balancer
become: true
tasks:
- name: install haproxy
package: name=haproxy state=installed
tag: balancertask
Shell command:
Ansible 8
$ ansible-playbook -i inventory playbook.yaml --tags balacertask
P.S. Is also possible to run both delimiters -l and --tags to narrow it down even more.
Using roles
Playbooks quickly get large and hard to maintain, so we want to breakup our tasks into modules to be
reused.
Roles can modularize:
files - regular files to be copied over such as configuration files, and templates, that are files that can be
modified before copying them to ansible servers
Ansible 9
How the playbook for roles looks like:
With ansible 2.7 is possible to run specific roles from command line:
$ cd /path/to/ansible/
$ ansible localhost -m include_role -a name=<role_name>
localhost | SUCCESS => {
"changed": false,
"include_variables": {
"name": "<role_name>"
}
}
localhost | SUCCESS => {
"msg": "<role_name>"
}
Ansible modules
Modules are commands/funtionalities that ansible host can run onto ansible servers.
https://docs.ansible.com/ansible/2.8/modules/modules_by_category.html
Ansible 10
Some ansible files modules:
Ansible 11
Some system modules:
Ansible Galaxy
It's also possible to get roles from Ansible Galaxy.
Reference:
Ansible 12
Ansible Galaxy
Jump start your automation project with great content from the Ansible community
https://galaxy.ansible.com/
Playbook.yaml file:
---
- hosts: all
become: true
roles:
- geerlingguy.java
Command line:
Role dependencies
Folder structure: role-folder > meta > main.yaml
Dependent roles will run before/first, then our role will run.
Template module
Gets a source template file, add variables, check if this file is the same as the one on the server, if not the
same, then copy it to destination server.
Ansible 13
Template module relies on python library Jinja 2:
It's also possible to include conditional sections in template files based on conditions and variables.
Ansible 14
It also allows us to loop over variables to populate templates using for statement.
Variable file example:
Ansible 15
Variables can also be expressed with dictionaries instead of strings, to be more easily readable.
Ansible 16
Macros: .j2
Reusable blocks
Blocks can be inherited from a father/base, such as haproxy.http.cfg (child) extends haproxy.base.cfg
(base). In this example, both files need to be in the same template directory.
Ansible 17
However, in the command line has top priority and even overwrites playbook variables:
Ansible Facts
What are facts?
Facts are variables that Ansible collects automatically when controlling a remote system
Ansible 18
In the example above, the variables are being pulled from group_var/all.yaml variable file, which are
variables available for all groups, hosts and systems.
https://www.udemy.com/course/devops-automate-your-infrastructure-using-ansible-in-9hours/learn/lecture/12479508#questions
3. Group variables
4. Role variables
Ansible 19
Using variables to control tasks
Use variable for conditional tasks.
Example, we have a task to install java on ansible node.
Terminal results:
Ansible 20
Compound conditions:
Ansible 21
Registering results in variables
For example, command register gets the response from stat and insert into variable vp, which is used on
later conditional when statements.
Using lists and dictionaries to repeat tasks and populate template files
To use a list to repeat tasks, use with_items:
Can also use lists and dictionaries as variables to do the same as above. Reference:
https://www.udemy.com/course/devops-automate-your-infrastructure-using-ansible-in-9hours/learn/lecture/12479532#questions
Ansible 22
Using directories across tasks and templates to populate files inside /etc/profile.d/ folder. In the example
below, ansible will run the template command 3 times, one for java key/component, another for maven and
latter for gradle key/component.
Version control makes it easy to share Ansible configuration with a team and to see how content changes
over time.
Can use Atom editor with Git or GitHub.
Ansible 23
Ansible Vault
Ansible Vault is used in order to be able to check in sensitive files into version control.
It's important to note that Ansible Vault works with any data file - variables, templates or plain files.
Encrypt files
Example, file /group_vars/all.yaml before encryption:
Ansible 24
P.S. Type in the (new) vault password.
To run it, add --ask-vault-pass to command line, and type in the vault password.
The simplest way to edit a vault encrypted file is to use the command:
Ansible 25
Able to edit it in terminal:
P.S. After the edit, it encrypts the file and save to its original location.
Decrypt files
Use code:
Example:
Example:
Ansible 26
Example:
Example:
https://www.udemy.com/course/devops-automate-your-infrastructure-using-ansible-in-9hours/learn/lecture/12479548#questions
Ansible 27
Ansible 28