Update create_user_ssh/check_authorizzed_key.yml
This commit is contained in:
parent
e7fe212fba
commit
ca113a58a4
|
@ -3,31 +3,26 @@
|
||||||
hosts: "{{ host }}"
|
hosts: "{{ host }}"
|
||||||
gather_facts: yes
|
gather_facts: yes
|
||||||
|
|
||||||
|
vars:
|
||||||
|
public_key_to_add: "{ key_to_add}" # Replace with the public key you want to add
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Read authorized_keys file
|
- name: Read authorized_keys file
|
||||||
shell: cat ~/.ssh/authorized_keys | grep "{{ key_to_add }}"
|
shell: cat ~/.ssh/authorized_keys | grep "{{ public_key_to_add }}"
|
||||||
register: authorized_key_output
|
register: authorized_key_output
|
||||||
ignore_errors: yes
|
ignore_errors: yes
|
||||||
|
|
||||||
- name: Check if authorized key exists
|
- name: Check if authorized key exists
|
||||||
set_fact:
|
set_fact:
|
||||||
authorized_key_exists: "{{ authorized_key_output.stdout | length > 0 }}"
|
authorized_key_exists: "{{ authorized_key_output.stdout_lines | length > 0 }}"
|
||||||
when: authorized_key_output.rc == 0
|
|
||||||
|
|
||||||
- name: Display result
|
- name: Display result
|
||||||
debug:
|
debug:
|
||||||
msg: "Authorized key exists: {{ authorized_key_exists | default(false) }}"
|
msg: "Authorized key exists: {{ authorized_key_exists | default(false) }}"
|
||||||
|
|
||||||
- name: Add authorized key if it does not exist
|
- name: Add authorized key
|
||||||
block:
|
authorized_key:
|
||||||
- name: Add authorized key
|
user: root # Replace with the remote user's name
|
||||||
authorized_key:
|
state: present
|
||||||
user: root # Replace with the remote user's name
|
key: "{{ public_key_to_add }}"
|
||||||
state: present
|
|
||||||
key: "{{ key_to_add }}"
|
|
||||||
rescue:
|
|
||||||
- name: Handle error when key already exists
|
|
||||||
debug:
|
|
||||||
msg: "The authorized key already exists. Skipping addition."
|
|
||||||
|
|
||||||
when: not authorized_key_exists | default(false)
|
when: not authorized_key_exists | default(false)
|
||||||
|
|
Loading…
Reference in New Issue