How can I get Ansible to populate the correct file when my /etc/ssh/sshd_config has AuthorizedKeysFile set to /etc/ssh/authorized_keys/%u? Ansible seems to ignore the setting and places keys in $HOME/.ssh/authorized_keys
playbook:
---
- hosts: all
vars:
vars_files:
- ../group_vars/ssh_root_authorized_keys.yml
gather_facts: false
tasks:
- name: Set up multiple authorized keys
authorized_key:
user: root
state: present
key: '{{ item.key }}'
with_items: "{{ root_auth_keys }}"
ssh_root_authorized_keys.yml
root_auth_keys:
- name: backup@host
key : "{{ lookup('file', '../group_vars/pubkeys/[email protected]') }}"
- name: nagios@host
key : "{{ lookup('file', '../group_vars/pubkeys/[email protected]') }}"
From the documentation:
There are a couple of steps to prepare this functionality. First, get the value of the parameter. There might be more options, e.g. by default
For example, get the first one
gives
The parameter AuthorizedKeysFile may contain
%u
and%h
. See Location of the Authorized Keys FilePrepare the database of the home directories
By default, the module getent stores the database passwd in the dictionary getent_passwd. Home is the 4th attribute, e.g.
gives
Now, given the data
you can test the functionality
gives
If you change the parameter
the play will get the correct location of the authorized keys file