Professional Documents
Culture Documents
Ansiblecookbook en PDF
Ansiblecookbook en PDF
René Moser
1
Ansible Cookbook 2014 Contents
Contents
Intro 4
Basics 5
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
How do use all hosts in the host group except the current host ? 7
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Testing 9
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 2
Ansible Cookbook 2014 Contents
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Networking 17
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 3
Ansible Cookbook 2014 Intro
Intro
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 4
Ansible Cookbook 2014 Basics
Basics
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 5
Ansible Cookbook 2014 How do I use all hosts data in my play or template?
Solution
In a task:
In a template:
Explanation
Often you want to use every host in inventory, like for a monitoring play or for a dns zone file. The first thing
you see is to set the scope to all hosts in inventory e.g. hosts: all and then use delegate_to in the tasks.
This might work in some cases but it looks not the right way. The more elegant way is to tool over the special
group all containing all hosts.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 6
Ansible Cookbook 2014 How do use all hosts in the host group except the current host ?
How do use all hosts in the host group except the current host ?
Solution
In a task:
- hosts: webservers
tasks:
- debug: msg=”{{ item }}”
with_items: groups.webservers
when: ”item != inventory_hostname”
In a template:
Explanation
In many cluster management you need to set all other nodes in the cluster. This can be done dynamically using
with_items and when or similar logic in a template.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 7
Ansible Cookbook 2014 How to make a command or shell run in check mode ?
Solution
Explanation
Commands and shell actions are skipped in check mode. If you want to force exection you can use always_run:
yes. Also useful in this case is to add changed_when: false to always get a ok response instead of changed.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 8
Ansible Cookbook 2014 Testing
Testing
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 9
Ansible Cookbook 2014 How do I test Ansible Playbooks locally?
Solution
Explanation
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 10
Ansible Cookbook 2014 How do I test Ansible roles?
Solution
Explanation
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 11
Ansible Cookbook 2014 Secuirty and Privacy
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 12
Ansible Cookbook 2014 How do I store private data in git for Ansible?
However, even if you use a private git repo on GitHub, your data are accessable by GitHub employees and and
federal law enforcement).
So how can we make sure, anyone can get access to your data, but the users you want?
Solution
Requirements: GnuPG
Create a password file for ansible-vault, I use pwgen just because I am lazy. After that, we make sure this plain
text password file will never be added to our git repo.
Now we use GnuPG to encrypt the password file used for ansible-vault:
The above command will create an encrypted version of our password file in a new file vault-password.txt.gpg.
As you can see we added a few recipient public keys. All owners of the corresponding private keys, and only
them, are able to decrypt the encrypted password file.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 13
Ansible Cookbook 2014 How do I store private data in git for Ansible?
This alias will automatically decrypt the vault-password.txt.gpg if necessary and uses the encrypted file with
option --vault-password-file.
Explanation
Ansible has a tool for encryption of var files like group_vars and host_vars sind version 1.5, ansible-vault.
However, with ansible-vault you can only encrypt and decrypt var files. Another downside is you have to
remember the password and this password must be shared accross your team members.
That is why ansible-playbook has an other option for letting you write the password in a file and pass
--vault-password-file <filename> to ansible-playbook. So you do not have to writing it over and over again
every time you run a playbook or also meant for automated execution of ansible.
But as you can guess, the password is stored in plain text in a file and we likely want this file to be in a public
git repo.
This is the part where asymetric cryptology and GnuPG comes into the game. GnuPG lets you encrypt any file
without shareing a password, even for a group of people.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 14
Ansible Cookbook 2014 How do I manage root’s authorized_keys file with Ansible?
Solution
We usually create a separate repo for all ssh public keys. So everyone can send pull request for updating their
ssh public key. Of course you should verify them. :)
.
├── user1.pub
├── user2.pub
├── user3.pub
└── user4.pub
To make sure we always use the latest version of this repo. We set up a playbook, in which we checkout the
lastest state to our local workstation.
# filename: sshkeys.yml
---
- hosts: localhost
connection: local
tasks:
- name: check out latest ssh public keys
git: repo=https://github.com/example/ssh-pubkeys.git dest=./files/ssh-public-keys/
The next step is to setup the vars. Here we define, who should have access to all host by creating the vars
section in group_vars/all:
# group_vars/all
---
ssh_root_users:
- key: ”{{ lookup(’file’, ’./files/sshkeys/user1.pub’) }}”
- key: ”{{ lookup(’file’, ’./files/sshkeys/user2.pub’) }}”
- key: ”{{ lookup(’file’, ’./files/sshkeys/user3.pub’) }}”
- key: ”{{ lookup(’file’, ’./files/sshkeys/user4.pub’) }}”
# group_vars/webservers
---
ssh_root_users:
- key: ”{{ lookup(’file’, ’./files/sshkeys/user1.pub’) }}”
- key: ”{{ lookup(’file’, ’./files/sshkeys/user2.pub’) }}”
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 15
Ansible Cookbook 2014 How do I manage root’s authorized_keys file with Ansible?
# filename: sshkeys.yml
---
- hosts: localhost
connection: local
tasks:
- name: check out latest ssh public keys
git: repo=https://github.com/example/ssh-pubkeys.git dest=./files/sshkeys/
- hosts: all
remote_user: root
tasks:
- name: add ssh root keys
authorized_key: user=”root” key=”{{ item.key }}”
with_items: ssh_root_users
Explanation
The easiest way is to have a single authorized_keys file and use the copy task, of course.
However, often we can not reuse the same authorized_keys on every machine or group of machines. I wanted
to have something which has the maximum of flexibily, but to be simple and comprehensible at the same time.
The above solution is a typical example how you can achive this with Ansible.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 16
Ansible Cookbook 2014 Networking
Networking
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 17
Ansible Cookbook 2014 How do I add nodes to DNS?
Solution
Explanation
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 18
Ansible Cookbook 2014 How do I make manage config of a failover cluster?
Solution
Make use of serial keyword in you play. This also called Rolling Update in Ansible’s Docs.
---
- hosts: database-cluster
serial: 1
Explanation
A failover cluster often consist of 2 nodes only, so we set serial: 1 to make sure, we do all tasks on one node
after the other.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 19
Ansible Cookbook 2014 Helpers and Tools
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 20
Ansible Cookbook 2014 How do I run Puppet agent by Ansible?
Solution
# filename: run-puppet.yml
---
- hosts: all
remote_user: root
serial: 3
tasks:
- command: puppet agent --test --no-noop creates=/var/lib/puppet/state/agent_catalog_run.lock
register: puppet_result
changed_when: puppet_result.rc == 2
failed_when: puppet_result.rc == 1
Explanation
The playbook has target to all hosts, but of course you can limit it using --limit e.g.:
The Puppet agent returns code 1 on failure, and 2 on change. That is why we must use register to grap the
return code.
We also use serial to perform a Rolling Update and make sure to skip the command if Puppet agent is already
running.
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 21
Ansible Cookbook 2014 Where can I find an cheat sheet of Ansible?
Solution
Visit http://wall-skills.com/2014/ansible-cheat-sheet
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 22
Ansible Cookbook 2014 Application and Deployments
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 23
Ansible Cookbook 2014 How do I deploy PHP web applications?
Even there are many good tools which help a lot doing it right, like Capistrano or Fabric.
But why you should learn another tool if Ansible can do the job even better? I want to show you, how I deploy
PHP web applications:
Solution
One characteristic of a web application is, that most of the time we are dealing with intepreted scripting
languages, like PHP, Python or Ruby. So one solution to deploy an application is to checkout the code from
your source code management tool and run some installation sciprts like database migration.
# filename: app-deployment.yml
---
- hosts: appservers
vars:
app_version: v1.0.0
tasks:
- name: Checkout the application from git
git: repo=git://github.com/example/repo.git dest=/srv/www/myapp version={{ app_version }}
register: app_checkout_result
But wait, what about database backup? Monitoring downtime? Inform team members? As simple as that.
# filename: app-deployment.yml
---
- hosts: appservers
vars:
app_version: v1.0.0
tasks:
- name: inform team members about start
local_action: jabber
user=deployment@example.net password=secret
to=friend@example.net
msg=”Deployment of App version {{ app_version }} started”
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 24
Ansible Cookbook 2014 How do I deploy PHP web applications?
My applications are usually in a git repo. This git repo may be a private repo, so you even have to authenticate
for read access. Further git is a dependency of our installation. We may or can not have git installed on the
webservers. In this case, we just checkout the repo locally and sync the files using rsync or git-ftp:
# filename: app-deployment.yml
---
- hosts: appservers
vars:
app_version: v1.0.0
tasks:
...
- name: Checkout the application from git
local_action: git repo=git://github.com/example/repo.git dest=/srv/www/myapp version={{ app_version }}
register: app_checkout_result
...
Explanation
v 1.0.0 - Licensed under CC BY-NC-SA 3.0 - Tue Nov 25 23:13:45 UTC 2014 25