|
| 1 | +--- |
| 2 | +# Copyright Red Hat |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Distribute keyring and conf files to a set of clients |
| 6 | +# |
| 7 | +# Uses ceph_defaults |
| 8 | +# - local_client_dir: determines the dir name for the config files on the ansible host |
| 9 | +# - ceph_defaults_ceph_client_pkgs: list of pre-req packages that must be on the client |
| 10 | +# |
| 11 | +# Required run-time variables |
| 12 | +# ------------------ |
| 13 | +# keyring : full path name of the keyring file on the admin[0] host which holds the key for the client to use |
| 14 | +# fsid : fsid of the cluster to extract the keyring and conf from |
| 15 | +# |
| 16 | +# Optional run-time variables |
| 17 | +# ------------------ |
| 18 | +# conf : full path name of the conf file on the admin[0] host to use (undefined will trigger a minimal conf) |
| 19 | +# ceph_defaults_client_group : ansible group name for the clients to set up |
| 20 | +# keyring_dest : full path name of the destination where the keyring will be copied. (default: /etc/ceph/ceph.keyring) |
| 21 | +# |
| 22 | +# Example |
| 23 | +# ------- |
| 24 | +# ansible-playbook -i hosts cephadm-clients.yml -e fsid=BLA -e ceph_defaults_client_group=fs_clients -e keyring=/etc/ceph/fs.keyring |
| 25 | +# |
| 26 | + |
| 27 | + |
| 28 | +- name: Confirm local readiness |
| 29 | + hosts: all |
| 30 | + gather_facts: false |
| 31 | + tasks: |
| 32 | + - name: Confirm local readiness |
| 33 | + run_once: true |
| 34 | + delegate_to: localhost |
| 35 | + block: |
| 36 | + - name: Import_role ceph_defaults |
| 37 | + ansible.builtin.import_role: |
| 38 | + name: ceph_defaults |
| 39 | + |
| 40 | + - name: Fail if the fsid parameter is missing |
| 41 | + ansible.builtin.fail: |
| 42 | + msg: > |
| 43 | + You must supply an 'fsid' parameter for the corresponding ceph cluster |
| 44 | + when: fsid is undefined |
| 45 | + |
| 46 | + - name: Fail if admin group doesn't exist or is empty |
| 47 | + ansible.builtin.fail: |
| 48 | + msg: | |
| 49 | + You must define a group [admin] in your inventory which provides the |
| 50 | + keyring that you want to distribute |
| 51 | + when: "'admin' not in groups or groups['admin'] | length < 1" |
| 52 | + |
| 53 | + - name: Fail if ceph_defaults_client_group is NOT in the inventory |
| 54 | + ansible.builtin.fail: |
| 55 | + msg: > |
| 56 | + Variable ceph_defaults_client_group '{{ ceph_defaults_client_group }}' is not defined in the inventory |
| 57 | + when: ceph_defaults_client_group not in groups |
| 58 | + |
| 59 | + - name: Fail if keyring variable is missing |
| 60 | + ansible.builtin.fail: |
| 61 | + msg: | |
| 62 | + You must supply a 'keyring' variable that defines the path to the key |
| 63 | + that you want to distribute to your client machines |
| 64 | + when: keyring is not defined |
| 65 | + |
| 66 | + |
| 67 | +- name: Confirm admin host is ready |
| 68 | + hosts: admin[0] |
| 69 | + become: true |
| 70 | + gather_facts: false |
| 71 | + tasks: |
| 72 | + - name: Check fsid is present on {{ inventory_hostname }} |
| 73 | + ansible.builtin.stat: |
| 74 | + path: /var/lib/ceph/{{ fsid }} |
| 75 | + register: fsid_stat |
| 76 | + |
| 77 | + - name: Fail if fsid is not present |
| 78 | + ansible.builtin.fail: |
| 79 | + msg: > |
| 80 | + The given fsid ({{ fsid }}), is not present in /var/lib/ceph on {{ inventory_hostname }} |
| 81 | + when: |
| 82 | + - not fsid_stat.stat.exists | bool |
| 83 | + - not fsid_stat.stat.isdir | bool |
| 84 | + |
| 85 | + - name: Check keyring status on {{ inventory_hostname }} |
| 86 | + ansible.builtin.stat: |
| 87 | + path: "{{ keyring }}" |
| 88 | + register: keyring_stat |
| 89 | + |
| 90 | + - name: Fail if keyring not found on {{ inventory_hostname }} |
| 91 | + ansible.builtin.fail: |
| 92 | + msg: > |
| 93 | + The keyring path provided '{{ keyring }}' can not be found on {{ inventory_hostname }} |
| 94 | + when: not keyring_stat.stat.exists | bool |
| 95 | + |
| 96 | + - name: Check conf is OK to use |
| 97 | + ansible.builtin.stat: |
| 98 | + path: "{{ conf }}" |
| 99 | + register: conf_stat |
| 100 | + when: conf is defined |
| 101 | + |
| 102 | + - name: Fail if conf supplied is not on {{ inventory_hostname }} |
| 103 | + ansible.builtin.fail: |
| 104 | + msg: | |
| 105 | + The conf file '{{ conf }}' can not be found on {{ inventory_hostname }} |
| 106 | + when: |
| 107 | + - conf is defined |
| 108 | + - not conf_stat.stat.exists | bool |
| 109 | + - not conf_stat.stat.isreg | bool |
| 110 | + |
| 111 | + |
| 112 | +- name: Assemble client payload |
| 113 | + hosts: admin[0] |
| 114 | + become: true |
| 115 | + gather_facts: false |
| 116 | + tasks: |
| 117 | + - name: Import_role ceph_defaults |
| 118 | + ansible.builtin.import_role: |
| 119 | + name: ceph_defaults |
| 120 | + |
| 121 | + - name: Slurp the keyring |
| 122 | + ansible.builtin.slurp: |
| 123 | + src: "{{ keyring }}" |
| 124 | + register: client_keyring |
| 125 | + no_log: true |
| 126 | + |
| 127 | + - name: Slurp the conf if it's supplied |
| 128 | + ansible.builtin.slurp: |
| 129 | + src: "{{ conf }}" |
| 130 | + register: ceph_config |
| 131 | + when: |
| 132 | + - conf is defined |
| 133 | + - conf | length > 0 |
| 134 | + |
| 135 | + - name: Create minimal conf as a default |
| 136 | + ansible.builtin.command: cephadm shell -- ceph config generate-minimal-conf |
| 137 | + register: minimal_ceph_config |
| 138 | + when: conf is undefined |
| 139 | + |
| 140 | + |
| 141 | +- name: Distribute client configuration |
| 142 | + hosts: "{{ ceph_defaults_client_group }}" |
| 143 | + become: true |
| 144 | + gather_facts: true |
| 145 | + tasks: |
| 146 | + - name: Import_role ceph_defaults |
| 147 | + ansible.builtin.import_role: |
| 148 | + name: ceph_defaults |
| 149 | + |
| 150 | + - name: Install ceph-common on rhel |
| 151 | + ansible.builtin.command: dnf install --allowerasing --assumeyes ceph-common |
| 152 | + changed_when: false |
| 153 | + register: result |
| 154 | + until: result is succeeded |
| 155 | + when: ansible_facts['os_family'] == 'RedHat' |
| 156 | + |
| 157 | + - name: Install ceph client prerequisites if needed |
| 158 | + ansible.builtin.package: |
| 159 | + name: "{{ ceph_defaults_ceph_client_pkgs }}" |
| 160 | + state: present |
| 161 | + register: result |
| 162 | + until: result is succeeded |
| 163 | + |
| 164 | + - name: Copy configuration and keyring files to the clients |
| 165 | + ansible.builtin.copy: |
| 166 | + content: "{{ item.content }}" |
| 167 | + dest: "{{ item.dest }}" |
| 168 | + owner: ceph |
| 169 | + group: ceph |
| 170 | + mode: '0600' |
| 171 | + backup: true |
| 172 | + loop: |
| 173 | + - { content: "{{ hostvars[groups['admin'][0]]\ |
| 174 | + ['client_keyring']['content'] | b64decode }}", |
| 175 | + dest: "{{ keyring_dest | default('/etc/ceph/ceph.keyring') }}", |
| 176 | + copy_file: True } |
| 177 | + - { content: "{{ hostvars[groups['admin'][0]]\ |
| 178 | + ['minimal_ceph_config']['stdout'] | default('') }}{{ '\n' }}", |
| 179 | + dest: '/etc/ceph/ceph.conf', |
| 180 | + copy_file: "{{ conf is undefined }}" } |
| 181 | + - { content: "{{ hostvars[groups['admin'][0]]\ |
| 182 | + ['ceph_config']['content'] | default('') | b64decode }}", |
| 183 | + dest: '/etc/ceph/ceph.conf', |
| 184 | + copy_file: "{{ hostvars[groups['admin'][0]]\ |
| 185 | + ['ceph_config']['skipped'] is undefined }}" } |
| 186 | + when: item.copy_file | bool |
| 187 | + no_log: true |
0 commit comments