Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playbook for node SSH change successful #25

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node/rocky-iso/**
node/*.iso
node/output-generated
testbed/output-generated/**
*.box
*.box
.ssh/**
2 changes: 2 additions & 0 deletions deploy-playbook.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
- name: Create a set new SSH key for clusters and routers
ansible.builtin.import_playbook: networking/add-ssh-key-to-nodes-playbook.yaml
- name: Accept ssh keys for the first time
ansible.builtin.import_playbook: networking/accept-ssh-keys-playbook.yaml
- name: Pre-setup - get correct interfaces
Expand Down
Empty file modified dev/generate-ansible-inventory.sh
100644 → 100755
Empty file.
77 changes: 77 additions & 0 deletions networking/add-ssh-key-to-nodes-playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
- name: Add custom SSH key to nodes
hosts: all
gather_facts: false

tasks:
- name: Create SSH directory # noqa: run-once[task]
run_once: true
connection: local
ansible.builtin.file:
path: '{{ playbook_dir | dirname }}/.ssh'
state: directory
mode: '0700'

- name: Fetch the public key from the node
ansible.builtin.command: cat .ssh/authorized_keys
register: key
changed_when: false

- name: Parse the public key
ansible.builtin.set_fact:
node_public_key: "{{ key.stdout.split(' ')[1] }}"

- name: Generate SSH key # noqa: run-once[task]
Fixed Show fixed Hide fixed
connection: local
run_once: true
community.crypto.openssh_keypair:
path: '{{ playbook_dir | dirname }}/.ssh/new_key'
comment: "When I wrote this code, only God and I understood what I did. Now only God knows."
type: "ed25519"
register: local_ssh_key

- name: Cat the public key to pass it to the node
connection: local
ansible.builtin.command: 'cat {{ playbook_dir | dirname }}/.ssh/new_key.pub'
changed_when: local_ssh_key.changed
register: new_ssh_key

- name: Add the key and modify inventory if cluster
when: inventory_hostname | regex_replace('\d', '') == 'cluster'
throttle: 1
block:
- name: Propagate the publicg key
become: true
ansible.posix.authorized_key:
user: ni
state: present
key: "{{ new_ssh_key.stdout }}"
exclusive: "{{ not dev_cluster }}"
- name: Modify the inventory
connection: local
ansible.builtin.lineinfile:
path: '{{ inventory_dir }}/ansible-inventory-dev.ini'
regexp: '^{{ inventory_hostname }}(.*)ansible_ssh_private_key_file=(.*)( .*)?'
line: '{{ inventory_hostname }}\1ansible_ssh_private_key_file={{ playbook_dir | dirname }}/.ssh/new_key\3'
backrefs: true

- name: Add the key and mofidy inventory if router
when: inventory_hostname | regex_replace('\d', '') == 'router'
block:
- name: Propagate public key to this node
ansible.posix.authorized_key:
user: vagrant
state: present
key: "{{ new_ssh_key.stdout }}"
exclusive: "{{ not dev_cluster }}"
changed_when: false

- name: Modify the inventory
connection: local
ansible.builtin.lineinfile:
path: '{{ inventory_dir }}/ansible-inventory-dev.ini'
regexp: '^{{ inventory_hostname }}(.*)ansible_ssh_private_key_file=(.*) +(.*)'
line: '{{ inventory_hostname }}\1ansible_ssh_private_key_file={{ playbook_dir | dirname }}/.ssh/new_key \3'
backrefs: true

- name: Refresh the inventory
ansible.builtin.meta: refresh_inventory
1 change: 1 addition & 0 deletions requirements.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
collections:
- ansible.posix
- community.crypto
Loading