Posts tagged as ansible
Since Ansible version 2.5 there is a lot of discussion and confusion about the loop syntax. There is also discussion if with_...:
will be replaced by loop:
deprecating the with_...
keywords. Even Ansibles documentation is not clear about this.
Should I use loop:
or with_...:
, in fact nobody really knows. What would the correct syntax be?
---
- name: Loops with with_ and lookup
hosts: localhost
connection: local
gather_facts: no
vars:
people:
- john
- paul
- mary
drinks:
- beer
- wine
- whisky
tasks:
- name: with nested
debug:
msg: "with_nested: item[0] is '{{ item[0] }}' and item[1] is '{{ item[1] }}'"
with_nested:
- "{{ people }}"
- "{{ drinks }}"
- name: nested and loop
debug:
msg: "nested_loop: item[0] is '{{ item[0] }}' and item[1] is '{{ item[1] }}'"
loop:
- "{{ people }}"
- "{{ drinks }}"
Read more »
I am a long time Ansible user and contributor (since 2012) and I have been struggling with a decent setup for a multi-environment case. I have been designing and re-designing a lot, until I came up with this design. And what a coincidence, a customer wanted a setup that was exactly this. So this concept is a real world setup, working in a production environment.
Did I get your attention? Read after the break, but take your time. it is a long read.
Read more »
Some time ago I created a playbook to show the content of a rendered template. When you keep digging in the Ansible documentation, you suddenly stumble over the template
lookup-plugin. And then it turns out that my playbook is a bit clumsy.
A nicer and shorter way to do it:
---
#
# This playbook renders a template and shows the results
# Run this playbook with:
#
# ansible-playbook -e templ=<name of the template> template_test.yml
#
- hosts: localhost
become: false
connection: local
tasks:
- fail:
msg: "Bailing out. The play requires a template name (templ=...)"
when: templ is undefined
- name: show templating results
debug:
msg: "{{ lookup('template', templ) }}"
A couple of days ago a client asked me if I could solve the following problem:
They have a large number of web servers, all running a plethora of PHP versions. These machines are locally managed with DirectAdmin, which manages the PHP configuration files as well. They are also running Ansible for all kind of configuration tasks. What they want is a simple playbook that ensures a certain line in all PHP ini
files for all PHP versions on all webservers.
All the PHP directories match the pattern /etc/php[0-9][0-9].d
.
Thinking about this, I came up with this solution (took me some time, though) 
---
- name: find all ini files in all /etc/php directories
hosts: webservers
user: ansible
become: True
become_user: root
tasks:
- name: get php directories
find:
file_type: directory
paths:
- /etc
patterns:
- php[0-9][0-9].d
register: dirs
- name: get files in php directories
find:
paths:
- "{{ item.path }}"
patterns:
- "*.ini"
loop: "{{ dirs.files }}"
register: phpfiles
- name: show all found files
debug:
msg: "Files is {{ item.1.path }}"
with_subelements:
- "{{ phpfiles.results }}"
- files
The part with the with_subelements
did the trick. Of course this line can be written as:
loop: "{{ query('subelements', phpfiles.results, files) }}"
During my last Ansible training the students needed to create some Ansible templates for them selfs. As I do not want to run a testing template against some, or all, machines under Ansible control I created a small Ansible playbook to test templates.
Read more »
Last Saturday I attended Loadays in Antwerp, Belgium.
After listening to Jan Piet Mens’s talk about Ansible, I was up for it.
At 11:30 sharp, I started my own presentation for an almost packed room. It’s called “Ansible, why and how I use it” and you can find it on SpeackerDeck.
It was a lovely talk, with a very knowledgeable crowd.
Please, have a look at it and if you have any questions, let me know.
Thanks to the crew for organizing such a lovely event, every year.
Photos of the event where taken by Robert Keerse and you can see them at his Google Plus page. Do enjoy!!
For those of you with a strong stomach, the complete presentation is on Youtube as well. Have a look at the Youtube stream
Last couple of days I attended Configuration Managememt Camp in Ghent, Belgium. On Monday morning we started of with presentations of Mark Burgess (CFEngine), Luke Kanies (Puppet) and Adam Jacob (Chef). Good talks about the future of things.
After lunch it got nerdy ans I joined the Ansible room, to see how things went and at 17:00 I started my own presentation for a completely packed room. It’s called ‘Ansible, why and how I use it’ and you can find it on SpeackerDeck.
Later that night we joined the social event and I must say that I was really glad that Toshaan ran out of beer-tickets. Four Triple Karmeliet is more than enough for one evening 
The Tuesday was a complete Puppet-day, starting of with a Q-and-A with Luke Kanies. Things started a little slow, but as usual we ran out of time because of the amount of questions.
After stocking up with Belgian Beer the two days ended. Lots of new things learned and to look into. Two days well spent.
Today I’m attending the first full day Ansible configuration meeting. This meeting is in Antwerp, Belgium, a drive of almost 2 hours. Thanks to Multi Mho (Maurice Verheesen) I didn’t need to drive, he wanted to try out his nice, new car. It drives perfectly and we arrived about 30 minutes early.
For a first meeting of a new tool there where a lot of attendants, amongst others (and I don’t want to forget anybody, so I won’t even try to give a complete list), but below are the people I think that where there.
- Ton Kersten
- Maurice Verheesen
- Christopher Ranschaert
- Colin Petrie
- Dag Wieers
- Inigo Ortiz de Urbina Cazenave
- Jan Piet Mens
- Jeroen Hoekx
- Jochen Moes
- Joost Ringoot
- Kevin Clymans
- Kristof Wevers
- Lee Van Steerthem
- Mattias Gees
- Nic De Muyer
- Serge van Ginderachter
- Toshaan Bharvani
- Vincent Van der Kussen
All very knowledgeable people and nice company to be around.
After Jan Piet talked about Ansible Fest in Boston he supplied us with all the goodies he brought home. We all got…. drum-roll…. 1 sticker each. The T-shirts didn’t show up at the Ansible Fest. Not that bad, because I already have one. Thanks to Jan Piet for the time and effort to get some goodies, even though he couldn’t get them.
After this we started discussing and talking about things to improve Ansible. We all agreed things are great and will be greater by time.
All in all a good day to be at and I would like to thank everybody who attended and who helped organize this day. And last but not least Michael deHaan for creating Ansible.
PS: Also thanks to the guys that brought the beer. The Vicaris Triple is a fantastic one.
Playing with Ansible I did get the idea to make
a nice welcome message when you log in to a server. This message needs
to be placed in a file, which is configured in /etc/ssh/sshd_config
with the banner
option. I call this file /etc/issue
.
Of course I want to deploy this file with Ansible, so I first defined
an entry in the hosts
file. This looks like this:
# Settings for master
[master]
master
# Variables for master
[master:vars]
location=cow shed
room=ESX5i
issueremarks=This is the master Ansible server. Please be carefull!!
and this is parsed through a Jinja2 template which looks like
{{ figlethost.stdout }}
------------------------------------------------------------------------------
-- W A R N I N G --
UNAUTHORIZED ACCESS STRICTLY PROHIBITED!!
------------------------------------------------------------------------------
System Name : {{ "%-25s"|format(ansible_hostname) }} Location : {{ location }}
Managed by : {{ "%-25s"|format(name) }} Room : {{ room }}
------------------------------------------------------------------------------
{% if issueremarks is defined %}
{{ issueremarks.center(80) }}
------------------------------------------------------------------------------
{% endif %}
When combining this all into a playbook this results in
---
- hosts: all
tasks:
- name: install figlet
yum: pkg=figlet state=latest
- name: figlet name
command: /usr/bin/figlet -c -w 80 ${ansible_hostname}
register: figlethost
- name: deploy issue file
template: src=issue.in dest=/etc/issue owner=root mode=0444
As you can see there is a registered variable, called figlethost
which
contains the hostname parsed through figlet
. And
putting it all together this gives
_
_ __ ___ __ _ ___| |_ ___ _ __
| '_ ` _ \ / _` / __| __/ _ \ '__|
| | | | | | (_| \__ \ || __/ |
|_| |_| |_|\__,_|___/\__\___|_|
------------------------------------------------------------------------------
-- W A R N I N G --
UNAUTHORIZED ACCESS STRICTLY PROHIBITED!!
------------------------------------------------------------------------------
System Name : master Location : cow shed
Managed by : Ton Kersten Room : ESX5i
------------------------------------------------------------------------------
This is the master Ansible server. Please be carefull!!
------------------------------------------------------------------------------