---
- action: file dest=/var/lib/ceph/mon/ceph-{{ceph_idx}} state=directory
  tags:
    - ceph

# To do only on the *first* mon client
- name: Create keyring on the first mon node
  action: shell ceph-authtool /etc/ceph/ceph.mon.keyring --list | grep -A 1 '\[mon\.\]' | 
          tail -1 |sed 's/\s*key\s*=\s*//g' | grep =$ ||
          ceph-authtool /etc/ceph/ceph.mon.keyring --create-keyring --gen-key -n mon. -p --cap mon 'allow *' | tail -1 | grep =$
  register: mon_key
  when: inventory_hostname == groups.ceph_mon[0]
  tags:
    - ceph

- name: Create key for client.admin on the first mon node
  action: shell ceph-authtool /etc/ceph/ceph.mon.keyring --list | grep -A 1 '\[client\.admin\]' | 
          tail -1 |sed 's/\s*key\s*=\s*//g' | grep =$ ||
          ceph-authtool /etc/ceph/ceph.mon.keyring --gen-key -n client.admin -p  --cap mon 'allow *' --cap osd 'allow *' --cap mds 'allow *' | grep =$
  register: client_admin_key 
  when: inventory_hostname == groups.ceph_mon[0]
  tags:
    - ceph


- name: Create key for OSD.n on the first mon node
  action: shell idx={{hostvars.{{item}}.ceph_idx}};
          ceph-authtool /etc/ceph/ceph.mon.keyring --list | grep -A 1 "\[osd\.$idx\]" | 
          tail -1 |sed 's/\s*key\s*=\s*//g' | grep =$ ||
          ceph-authtool /etc/ceph/ceph.mon.keyring --gen-key -n osd.$idx -p --cap osd 'allow *' --cap mon 'allow rwx'| grep =$
  when: inventory_hostname == groups.ceph_mon[0]
  with_items: groups.ceph_osd
  tags:
    - ceph

- name: Create key for MDS.n on the first mon node
  action: shell idx={{hostvars.{{item}}.ceph_idx}};
          ceph-authtool /etc/ceph/ceph.mon.keyring --list | grep -A 1 "\[mds\.$idx\]" | 
          tail -1 |sed 's/\s*key\s*=\s*//g' | grep =$ ||
          ceph-authtool /etc/ceph/ceph.mon.keyring --gen-key -n mds.$idx -p --cap mds 'allow *' --cap osd 'allow *' --cap mon 'allow rwx'| grep =$
  when: inventory_hostname == groups.ceph_mon[0]
  with_items: groups.ceph_mds
  tags:
    - ceph
