Skip to content

Commit 142b3bf

Browse files
committed
Add facts for get ssh key. Each node add the ssh pub key of others nodes
1 parent 2a7208e commit 142b3bf

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

ceph-cluster.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
- hosts: ceph-cluster
2+
gather_facts: no
3+
tasks:
4+
- name: list local facts and register it in a variable
5+
local_action: shell basename `ls -1 ./files/facts/*.fact`
6+
register: list_local_facts
7+
- name: ensure /etc/ansible/facts.d/ exists
8+
file: path=/etc/ansible/facts.d state=directory owner=root group=root mode=0755
9+
- name: copy all facts on nodes
10+
copy: src=./files/facts/{{item}} dest=/etc/ansible/facts.d/{{item}} mode=0755
11+
with_items: list_local_facts.stdout_lines
12+
- name: gathering facts
13+
setup:
14+
# - name: debug toto
15+
# debug: var=hostvars[item]['ansible_local']['ssh_key_pub'].get("ssh_key_pub")
16+
# with_items: groups['ceph-cluster']
217
roles:
3-
- common
4-
- ceph
18+
- common
19+
- ceph

files/facts/ssh_key_pub.fact

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
FILE=/root/.ssh/id_rsa
4+
5+
SSH_PUB_KEY=`cat ${FILE}.pub`
6+
7+
cat <<EOF
8+
{
9+
"ssh_key_pub" : "$SSH_PUB_KEY"
10+
}
11+
EOF

roles/ceph/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# Here will be included the differents tasks for a ceph node
2+
- name: gathering facts
3+
setup:
4+
- include: ssh_key.yml

roles/ceph/tasks/ssh_key.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- name: put all nodes ssh pub key
2+
lineinfile: dest=/root/.ssh/authorized_keys create=yes owner=root group=root mode=0644 state=present line="{{hostvars[item]['ansible_local']['ssh_key_pub'].get('ssh_key_pub')}}"
3+
with_items: groups['ceph-cluster']

roles/common/tasks/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
# Common task for all nodes
33

4+
- name: gathering facts
5+
setup:
46
- include: ssh_keys.yml
57
- include: repo.yml
68
- include: packages.yml

roles/common/tasks/ssh_keys.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
- name: Add ssh keys
33
lineinfile: dest=/root/.ssh/authorized_keys create=yes owner=root group=root mode=0644 state=present line="{{item}}"
44
with_items: keys
5-
65
- name: check if ssh keys are present for root
76
stat: path=/root/.ssh/id_rsa.pub
87
register: p
9-
108
#TODO: if /root/.ssh/id_rsa exists but is a directory, we will have a problem..
119
#TODO: maybe we could put the path in a variable
1210
- name: create ssh keys if necessary
1311
command: "ssh-keygen -t rsa -b 2048 -q -f /root/.ssh/id_rsa -N ''"
1412
when: p.stat.exists == false
15-
16-
#- debug: var=ssh_pub_key

0 commit comments

Comments
 (0)