vn-ansible/create_user_ssh/check_authorizzed_key.yml

29 lines
862 B
YAML
Raw Normal View History

---
- name: Check and Add Authorized Key
hosts: "{{ host }}"
gather_facts: yes
vars:
public_key_to_add: "{ key_to_add}" # Replace with the public key you want to add
tasks:
- name: Read authorized_keys file
shell: cat ~/.ssh/authorized_keys | grep "{{ public_key_to_add }}"
register: authorized_key_output
ignore_errors: yes
- name: Check if authorized key exists
set_fact:
authorized_key_exists: "{{ authorized_key_output.stdout_lines | length > 0 }}"
- name: Display result
debug:
msg: "Authorized key exists: {{ authorized_key_exists | default(false) }}"
- name: Add authorized key
authorized_key:
user: root # Replace with the remote user's name
state: present
key: "{{ public_key_to_add }}"
when: not authorized_key_exists | default(false)