ansible.posix.authorized_key module – Adds or removes an SSH authorized key
Note
This module is part of the ansible.posix collection (version 1.6.2).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install ansible.posix
.
To use it in a playbook, specify: ansible.posix.authorized_key
.
New in ansible.posix 1.0.0
Synopsis
Adds or removes SSH authorized keys for particular user accounts.
Parameters
Parameter |
Comments |
---|---|
Change the comment on the public key. Rewriting the comment is useful in cases such as fetching it from GitHub or GitLab. If no comment is specified, the existing comment will be kept. |
|
Whether to remove all other non-specified keys from the authorized_keys file. Multiple keys can be specified in a single This option is not loop aware, so if you use If you want multiple keys in the file you need to pass them all to Choices:
|
|
Follow path symlink instead of replacing it. Choices:
|
|
The SSH public key(s), as a string or (since Ansible 1.9) url (https://github.com/username.keys). |
|
A string of ssh key options to be prepended to the key in the authorized_keys file. |
|
Whether this module should manage the directory of the authorized key file. If set to Be sure to set See the example below. Choices:
|
|
Alternative path to the authorized_keys file. The default value is the Most of the time, it is not necessary to set this key. Use the path to your target authorized_keys if you need to explicitly point on it. |
|
Whether the given key (with the given key_options) should or should not be in the file. Choices:
|
|
The username on the remote host whose authorized_keys file will be modified. |
|
This only applies if using a https url as the source of the keys. If set to This should only set to Prior to 2.1 the code worked as if this was set to Choices:
|
Examples
- name: Set authorized key taken from file
ansible.posix.authorized_key:
user: charlie
state: present
key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
- name: Set authorized keys taken from url
ansible.posix.authorized_key:
user: charlie
state: present
key: https://github.com/charlie.keys
- name: Set authorized keys taken from url using lookup
ansible.posix.authorized_key:
user: charlie
state: present
key: "{{ lookup('url', 'https://github.com/charlie.keys', split_lines=False) }}"
- name: Set authorized key in alternate location
ansible.posix.authorized_key:
user: charlie
state: present
key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
path: /etc/ssh/authorized_keys/charlie
manage_dir: false
- name: Set up multiple authorized keys
ansible.posix.authorized_key:
user: deploy
state: present
key: '{{ item }}'
with_file:
- public_keys/doe-jane
- public_keys/doe-john
- name: Set authorized key defining key options
ansible.posix.authorized_key:
user: charlie
state: present
key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
key_options: 'no-port-forwarding,from="10.0.1.1"'
- name: Set authorized key without validating the TLS/SSL certificates
ansible.posix.authorized_key:
user: charlie
state: present
key: https://github.com/user.keys
validate_certs: false
- name: Set authorized key, removing all the authorized keys already set
ansible.posix.authorized_key:
user: root
key: "{{ lookup('file', 'public_keys/doe-jane') }}"
state: present
exclusive: true
- name: Set authorized key for user ubuntu copying it from current user
ansible.posix.authorized_key:
user: ubuntu
state: present
key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
If the key has been forced to be exclusive or not. Returned: success Sample: |
|
The key that the module was running against. Returned: success Sample: |
|
Key options related to the key. Returned: success |
|
Path for authorized key file. Returned: success Sample: |
|
Whether this module managed the directory of the authorized key file. Returned: success Sample: |
|
Alternate path to the authorized_keys file Returned: success |
|
Whether the given key (with the given key_options) should or should not be in the file Returned: success Sample: |
|
Whether the key is unique Returned: success Sample: |
|
The username on the remote host whose authorized_keys file will be modified Returned: success Sample: |
|
This only applies if using a https url as the source of the keys. If set to Returned: success Sample: |
Authors
Ansible Core Team