3. What are we going to do today?
● Learn what is Ansible
● Do automation with Ansible
● Ask the instructor many question
3
4. What is Ansible?
● IT Automation Tool
● Focuses on simplicity and ease-of-use
● Open Source
○ More than 4,000 contributors
○ More than 40,000 contributions
4
5. Why using Ansible?
● “Why not simply use shell scripts?”
● “Why specifically Ansible and not Puppet or Chef or …?”
5
6. Let’s install a package with a shell script
6
YUM_CMD=$(which yum) # or dnf
APT_GET_CMD=$(which apt-get)
...
if [[ ! -z $YUM_CMD ]]; then
dnf install ntpdate
elif [[ ! -z $APT_GET_CMD ]]; then
apt-get install ntpdate
elif ...
...
else
echo "error can't install package ntpdate"
exit 1;
fi
7. Now let’s install the same package with Ansible
7
- name: install ntpdate
package:
name: ntpdate
state: present
● Works on multiple distributions
● Readable
● Describes desired status rather than the action
This is called a
“task” in Ansible
8. Why specifically Ansible?
8
● Python & SSH is all you need
● Simple to use
● Agentless
● Over 3300 modules!
● Great community
9. Time to have fun! but first, installation
9
# Fedora, RHEL 8, CentOS 8
$ dnf install ansible -y
# For older releases, use yum instead
# Ubuntu
$ apt-get install ansible -y
# Verify you can ssh without using a password to the machine you’ll use
$ ssh x.x.x.x
# No? Then run the following
$ ssh-copy-id <user>@x.x.x.x
10.
11. Inventory
11
● The hosts/servers you manage with Ansible
● Types of inventory
○ Static
○ Dynamic
● Default inventory
○ /etc/ansible/hosts
We will define a static inventory
12. Update you inventory
12
[remote]
x.x.x.x
$ sudo vi /etc/ansible/hosts
Let’s test it! :)
ansible remote -m ping
remote | SUCCESS => {
"changed": false,
"ping": "pong"
}
x.x.x.x [remote]
X.x.x.x ansible_user=vagrant
Or Or
Using this, you can
later reference in
Ansible multiple
servers with one
name
13. Write your first Playbook
13
- hosts: remote
tasks:
- name: Create the file /tmp/x
file:
path: /tmp/x
state: touch
$ vi first_task.yml
* A playbooks is a collection
plays
* A play is a collection of tasks
running on a single or multiple
hosts
14. Run your first Playbook
14
PLAY [remote] **************************************************
TASK [Gathering Facts] *****************************************
ok: [remote]
TASK [Create the file /tmp/x] **********************************
changed: [remote]
PLAY RECAP *****************************************************
remote : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0
$ ansible-playbook first_playbook.yml
20. Variables
20
● Used for storing values you can reference multiple times in your
playbook, in different tasks
● It’s common to find them used in conditionals, loops, ...
- hosts: remote
vars:
file_path: /tmp/x
tasks:
- name: Create the file "{{ file_path }}"
file:
name: "{{ file_path }}"
state: touch
21. Loops
21
● What if I need to create 10 files or install 30 package?
- hosts: remote
tasks:
- name: Create multiple files
file:
name: "{{ item }}"
state: touch
loop:
- /tmp/a
- /tmp/b
- /tmp/c
29. 29
{{ [32, 3, 5, 121, 6, 7] | max }}
Filters
● Transformation of data
{{ [32, 3, 5, 121, 6, 7] | min }}
{{ [32, 3, 5, 121, 6, 7] | random }}
{{ some_path | dirname }}
{{ some_string | quote }}
30. 30
- hosts: remote
tasks:
- name: Create multiple files
file:
name: "{{ item }}"
state: touch
loop: “{{ files_list | default([]) }}” What is the result of
the task?
OK? Changed?
Skipped?
● Also useful for dealing with undefined variables
31. Exercise #3
31
● Write a playbook which will:
○ Install the package htop only if the major version of the
operating system is 14
○ Install the package netcat only if the major version of the
operating system is 16
○ Add one random user of the following
■ [‘ed’, ‘eddy’, ‘eddie’]
# Commands
ansible-playbook
ansible-doc -l
ansible-doc <module_name>
ansible remote -m setup | less
32. AD-HOC
32
● Run tasks without writing playbooks
● For quick and non-repeating changes
ansible [hosts_pattern] -m [module_name] -a “[module options]”
34. Exercise #4
34
● Create locally a file with the content
○ “I came here from a far far server”
● Using ad-hoc commands:
○ Add a user called “mario”
○ Run the service “cron” (Ubuntu) or “crond” (RHEL, Fedora)
○ Copy the file you create to the remote server
# Commands
ansible-playbook
ansible-doc -l
ansible-doc <module_name>
ansible remote -m setup | less
ansible remote -m module -a “...”
ansible [hosts_pattern] -m [module_name] -a “[module options]”
35. Command & Shell modules
35
● “I want to run a customized script on a remote host”
● “I’m unable to find a module for what I need”
● Solution
○ command or shell module
name: Get blipblop version
become: yes
command: "rpm -q --qf %{VERSION} blipblop"
register: blipblop_rpm_version
36. Register Result
36
● By registering the result of a task we can “connect” between tasks
- name: Get blipblop version
become: yes
command: "rpm -q --qf %{VERSION} blipblop"
register: blipblop_rpm_version
- name: Get blipblop version
become: yes
shell: |
echo “blip blop begin”
ls -l
echo “blip blop end”
when: blipblop_rpm_version.stdout == “1.2.3”
37. Register: Another Example
37
● Configure an app and run it only if configuration was successful
- name: configure X app
command: configure
args:
chdir: /x_app
register: x_app_config
- name: run X app
command: run
args:
chdir: /x_app
when: x_app_config.rc == 0
38. Register: One More Example
38
● Run until return code is 0
- name: run some_command
command: some_command
register: result
until: result.rc == 0
retries: 5
39. Exercise #5
39
● Write the following playbook:
○ Download the file
https://gist.github.com/corysimmons/8b94c08421dec18bbaa4
○ If the download was successful, log the message “I managed to
download file with Ansible. So excited…”
# Commands
ansible-playbook
ansible-doc -l
ansible-doc <module_name>
ansible remote -m setup | less
ansible remote -m module -a “...”
# Directives
loop:
when:
become: become_user:
args:
until:
# Variables
{{ x }}
{{ x | default(“”) }}
43. Credits
Special thanks to all the people who made and released
these awesome resources for free:
✘ Presentation template by SlidesCarnival
✘ Photographs by Unsplash
43