Ansible 101

Configuration management could not be simpler!

Allan Denot
May 23, 2015 · 9 min read

Basic Concepts

Terminology

---
- hosts: all
tasks:
- ping:
[localhost]
127.0.0.1 variable1=value1 variable2=value2
[webservers]
webserver.awesomecompany.ly ansible_ssh_host=55.44.33.22

Setting up the Environment

# Debian/Ubuntu:
apt-get install python-pip
# RedHat/CentOS/Fedora:
yum install python-pip
# MacOSX (more info):
sudo easy_install pip
# Windows: Unfortunately Ansible does not work on Windows, but you can easily setup an Linux virtual machine with Vagrant and SSH into it.
pip install ansible
ansible --help

Inventory

[localhost]
127.0.0.1

Config

[defaults]
inventory = hosts
remote_user = ubuntu
host_key_checking = False

First Playbook

---
- hosts: all
connection: local
tasks:
- debug: msg="Hello World!"
    - module_name: parameter1=value parameterX=value

Running a Playbook

ansible-playbook hello.yml
Environmentally friendly green

ERROR: Unable to find an inventory file, specify one with -i ?

Going Remote

[webservers]
55.44.33.22

Accessing Remote Hosts


SSH Keys

private_key_file=/etc/ansible/keys/access.pem
[webservers]
55.44.33.22 ansible_ssh_private_key_file=/etc/ansible/keys/web.pem
[webservers:vars]
ansible_ssh_private_key_file=/etc/ansible/keys/web.pem
Host 55.44.33.22 *.awesomecompany.ly
IdentityFile /etc/ansible/keys/access.pem
IdentityFile /etc/ansible/keys/web.pem


ssh -i /etc/ansible/keys/web.pem ubuntu@55.44.33.22

Targeting Remote Hosts

[webservers]
55.44.33.22
[webservers:vars]
ansible_ssh_private_key_file=/etc/ansible/keys/web.pem
---
- hosts: webservers
sudo: yes
tasks:
- apt: name=apache2 state=present
ansible-playbook webservers.yml
Oh no, my IP address is revealed!
Idempotent FTW

Tasks are (or should be) idempotent. Meaning that nothing is changed if the desired state is already there.


Common Issues

sudo all the things!
Dude, where's my host?
Y U NO

Bonus: Command Line Tricks

Verbosity

ansible-playbook -vv webservers.yml
Colors!
ansible-playbook -vvvv webservers.yml

Check Mode

ansible-playbook --check webservers.yml

Limiting Hosts

---
- hosts: all
tasks:
- debug: msg="Hello World!"
ansible-playbook -l webservers webservers.yml

Different Inventory

ansible-playbook -i my_other_inventory webservers.yml
ansible-playbook -i 55.44.33.22, webservers.yml

Next Steps

Conclusion

If this tutorial helped you in any way
If you want more tutorials like this
Please click Recommend below

Thanks for reading!

Allan Denot

Written by

Cloud, container platforms and occasionally ML