Answer the question
In order to leave comments, you need to log in
Ansible + SSH keys, how to do it?
You need to deploy keys to servers, but the trick is that there are a lot of keys and they should not all fall on all servers. Now it is implemented as follows.
in file roles/authorized_keys/vars/main.yml
ssh_users:
- name: pupkin
key: "{{ lookup('file', 'roles/authorized_keys/vars/pupkin.pub') }}"
state: present
- name: root
key: "{{ lookup('file', 'roles/authorized_keys/vars/guru.pub') }}"
state: present
- name: root
key: "{{ lookup('file', 'roles/authorized_keys/vars/user.pub') }}"
state: absent
- name: Add ssh key.
authorized_key: user={{ item.name }} key="{{ item.key }}" state={{ item.state }}
with_items: ssh_users
---
- hosts: '{{ hosts }}'
vars_files:
- '{{ vars }}'
roles:
- { role: authorized_keys }
- name: pupkin
key: "{{ lookup('file', 'roles/authorized_keys/vars/pupkin.pub') }}"
servers: web,database,12.12.12.12
state: present
Answer the question
In order to leave comments, you need to log in
actually nothing prevents you from doing this, you need to change the description of the keys in roles/authorized_keys/vars/main.yml like this:
ssh_users:
- name: pupkin
key: "{{ lookup('file', 'roles/authorized_keys/vars/pupkin.pub') }}"
servers:
- host1
- host2
state: present
- name: root
key: "{{ lookup('file', 'roles/authorized_keys/vars/guru.pub') }}"
servers:
- host1
- host2
state: present
- name: root
key: "{{ lookup('file', 'roles/authorized_keys/vars/user.pub') }}"
servers:
- host3
- host4
state: absent
- name: Add ssh key.
authorized_key: user="{{ item.name }}" key="{{ item.key }}" state="{{ item.state }}"
when: "inventory_hostname in item.servers"
with_items: "{{ ssh_users }}"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question