authorized_key¶
Collection Note
This module is part of the ansible.posix collection. To install the collection, use:
Added in version1.0.0.
Synopsis¶
- Adds or removes SSH authorized keys for particular user accounts.
Parameters¶
| Parameter | Defaults / Choices | Comments |
|---|---|---|
| comment str |
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. |
|
| exclusive bool |
Whether to remove all other non-specified keys from the authorized_keys file. Multiple keys can be specified in a single O(key) string value by separating them by newlines. This option is not loop aware, so if you use C(with_) , it will be exclusive per iteration of the loop. If you want multiple keys in the file you need to pass them all to O(key) in a single batch as mentioned above. |
|
| follow bool |
Follow path symlink instead of replacing it. | |
| key str required |
The SSH public key(s), as a string or (since Ansible 1.9) url (https://github.com/username.keys). You can also use V(file://) prefix to search remote for a file with SSH key(s). |
|
| key_options str |
A string of ssh key options to be prepended to the key in the authorized_keys file. | |
| manage_dir bool |
Default: True |
Whether this module should manage the directory of the authorized key file. If set to V(true), the module will create the directory, as well as set the owner and permissions of an existing directory. Be sure to set O(manage_dir=false) if you are using an alternate directory for authorized_keys, as set with O(path), since you could lock yourself out of SSH access. See the example below. |
| path path |
Alternative path to the authorized_keys file. The default value is the V(.ssh/authorized_keys) of the home of the user specified in the O(user) parameter. 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. |
|
| state str |
Default: present Choices: absent, present |
Whether the given key (with the given key_options) should or should not be in the file. |
| user str required |
The username on the remote host whose authorized_keys file will be modified. | |
| validate_certs bool |
Default: True |
This only applies if using a https url as the source of the keys. If set to V(false), the SSL certificates will not be validated. This should only set to V(false) used on personally controlled sites using self-signed certificates as it avoids verifying the source site. Prior to 2.1 the code worked as if this was set to V(true). |
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 path on controller node
ansible.posix.authorized_key:
user: charlie
state: present
key: file:///home/charlie/.ssh/id_rsa.pub
- 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¶
| Key | Data Type | Description | Returned |
|---|---|---|---|
| exclusive | bool | If the key has been forced to be exclusive or not. | success |
| key | str | The key that the module was running against. | success |
| key_option | str | Key options related to the key. | success |
| keyfile | str | Path for authorized key file. | success |
| manage_dir | bool | Whether this module managed the directory of the authorized key file. | success |
| path | str | Alternate path to the authorized_keys file | success |
| state | str | Whether the given key (with the given key_options) should or should not be in the file | success |
| unique | bool | Whether the key is unique | success |
| user | str | The username on the remote host whose authorized_keys file will be modified | success |
| validate_certs | bool | This only applies if using a https url as the source of the keys. If set to C(false), the SSL certificates will not be validated. | success |
Authors¶
- Ansible Core Team