Crazy ansible for ceph

George Shuklin
2 min readJun 22, 2016

--

I want to show everyone how crazy am I ^w^w^w cool Ansible can be.

Here is a snippet from playbook to create directory-based OSD for Ceph.

It expects dir_osd to to be list of dicts with ‘num’ and ‘uuid’ key. It generates list of osd to create (those who are not yet created) in variable ‘osd_to_create’.

- name: check if OSD exists
command: cat /var/lib/ceph/osd/ceph-{{item.num}}/whoami
register: whoami
with_items: ‘{{dir_osd}}’
failed_when: whoami|success and whoami.stdout != ‘{{item.num}}’
changed_when: false
when: dir_osd is defined
- name: Marking OSD for creation
set_fact:
osd_to_create: “{{osd_to_create|default([]) + [{ ‘num’: item.0.num, ‘uuid’:item.0.uuid }] }}”
with_together:
— “{{dir_osd}}”
— “{{whoami.results}}”
when: dir_osd is defined and {{item.1|failed}}

I’m crazy^w cool, aren’t I?

Look at this beauty: I check every dir_osd item if it exists or not by reading their ‘whoami’ file. If whoami contains wrong information, play stops. If no such file found, we register this and continue. Because I use ‘register’ with ‘with_items’ I receive variable containing list of results, not just result.

Next step is the main magic. I do double iteration over original list and results of execution of previous step. Each step would be performed only of execution of corresponding task in previous task failed (means no such OSD exist). That allows me skip all existing OSD. In the same task I register fact (set_fact) ‘osd_to_create’ by joining new values and old value of osd_to_create (if no old value it defaults to empty list). This is just ‘append to list’ operation.

New value on each iteration of this task is a dict consisting of num and uuid (from iterated dir_osd).

I really proud of myself to able to create such thing, but I’d like to note, that I prefer to see Ansible allowing to perform list filtration/mapping on less extravagant basis (in other words: I don’t like it. I proud of it, but I don’t like it).

--

--

George Shuklin

I work at Servers.com, most of my stories are about Ansible, Ceph, Python, Openstack and Linux. My hobby is Rust.