Skip to content

Commit d839b68

Browse files
committed
cephadm-ansible playbooks import
Signed-off-by: Teoman ONAY <tonay@ibm.com>
1 parent a42d787 commit d839b68

File tree

12 files changed

+1008
-7
lines changed

12 files changed

+1008
-7
lines changed

galaxy.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
namespace: "ceph"
77
name: "automation"
8-
version: 1.0.1
8+
version: 1.1.0
99
readme: README.md
1010
authors:
11-
- Teoman ONAY <tonay@ibm.com>
11+
- Teoman ONAY <tonay@ibm.com>
1212

1313
description: Ceph automation modules
1414
license_file: LICENSE
1515
# TO-DO: update the tags based on your content type
16-
tags: ["linux", "tools"]
17-
dependencies: {}
16+
tags: [ "linux", "tools" ]
17+
dependencies: { ansible.posix, community.general }
1818

1919
repository: https://github.com/ceph/ceph.automation
2020
documentation: https://docs.ceph.com/projects/ceph.automation
@@ -26,9 +26,10 @@ issues: https://github.com/ceph/ceph.automation/issues
2626
# uses 'fnmatch' to match the files or directories. Some directories and files like 'galaxy.yml', '*.pyc', '*.retry',
2727
# and '.git' are always filtered. Mutually exclusive with 'manifest'
2828
build_ignore:
29-
- .gitignore
30-
- changelogs/.plugin-cache.yaml
31-
- ".*"
29+
- .gitignore
30+
- changelogs/.plugin-cache.yaml
31+
- ".*"
32+
3233
# A dict controlling use of manifest directives used in building the collection artifact. The key 'directives' is a
3334
# list of MANIFEST.in style
3435
# L(directives,https://packaging.python.org/en/latest/guides/using-manifest-in/#manifest-in-commands). The key

playbooks/cephadm-clients.yml

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
# Copyright Red Hat
3+
# SPDX-License-Identifier: Apache-2.0
4+
# Author: Guillaume Abrioux <gabrioux@redhat.com>
5+
#
6+
# This playbook copies an SSH public key to a specified user on remote hosts.
7+
#
8+
# Required run-time variables
9+
# ------------------
10+
# admin_node : The name of a node with enough privileges to call `cephadm get-pub-key` command.
11+
# (usually the bootstrap node).
12+
#
13+
# Optional run-time variables
14+
# ------------------
15+
# fsid : The fsid of the Ceph cluster.
16+
# cephadm_ssh_user : ssh username on remote hosts.
17+
# cephadm_pubkey_path : Full path name of the ssh public key file *on the ansible controller host*.
18+
# If not passed, the playbook will assume it has to get the key from `cephadm get-pub-key` command.
19+
#
20+
# Example
21+
# -------
22+
# ansible-playbook -i hosts cephadm-distribute-ssh-key.yml -e cephadm_ssh_user=foo -e cephadm_pubkey_path=/home/cephadm/ceph.key -e admin_node=ceph-node0
23+
#
24+
# ansible-playbook -i hosts cephadm-distribute-ssh-key.yml -e cephadm_ssh_user=foo -e admin_node=ceph-node0
25+
26+
- hosts: all
27+
become: true
28+
gather_facts: false
29+
tasks:
30+
- name: Fail if admin_node is not defined
31+
ansible.builtin.fail:
32+
msg: "You must set the variable admin_node"
33+
run_once: true
34+
delegate_to: localhost
35+
when: admin_node is undefined
36+
37+
- name: Get ssh public key from a file on the Ansible controller host
38+
when: cephadm_pubkey_path is defined
39+
block:
40+
- name: Get details about {{ cephadm_pubkey_path }}
41+
ansible.builtin.stat:
42+
path: "{{ cephadm_pubkey_path }}"
43+
register: cephadm_pubkey_path_stat
44+
delegate_to: localhost
45+
run_once: true
46+
47+
- name: Fail if {{ cephadm_pubkey_path }} doesn't exist
48+
ansible.builtin.fail:
49+
msg: "{{ cephadm_pubkey_path }} doesn't exist or is invalid."
50+
run_once: true
51+
delegate_to: localhost
52+
when:
53+
- not cephadm_pubkey_path_stat.stat.exists | bool
54+
or not cephadm_pubkey_path_stat.stat.isfile | bool
55+
56+
- name: Get the cephadm ssh pub key
57+
ansible.builtin.command: "cephadm shell {{ '--fsid ' + fsid if fsid is defined else '' }} ceph cephadm get-pub-key"
58+
changed_when: false
59+
run_once: true
60+
register: cephadm_get_pub_key
61+
delegate_to: "{{ admin_node }}"
62+
when: cephadm_pubkey_path is undefined
63+
64+
- name: Allow ssh public key for {{ cephadm_ssh_user | default('root') }} account
65+
ansible.posix.authorized_key:
66+
user: "{{ cephadm_ssh_user | default('root') }}"
67+
key: "{{ lookup('file', cephadm_pubkey_path) if cephadm_pubkey_path is defined else cephadm_get_pub_key.stdout }}"
68+
69+
- name: Set cephadm ssh user to {{ cephadm_ssh_user }}
70+
ansible.builtin.command: "cephadm shell {{ '--fsid ' + fsid if fsid is defined else '' }} ceph cephadm set-user {{ cephadm_ssh_user | default('root') }}"
71+
changed_when: false
72+
run_once: true
73+
delegate_to: "{{ admin_node }}"

0 commit comments

Comments
 (0)