ansible / ansible.posix / 1.5.4 / module / authorized_key Adds or removes an SSH authorized key | "added in version" 1.0.0 of ansible.posix" Authors: Ansible Core Teamansible.posix.authorized_key (1.5.4) — module
Install with ansible-galaxy collection install ansible.posix:==1.5.4
collections: - name: ansible.posix version: 1.5.4
Adds or removes SSH authorized keys for particular user accounts.
- 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') }}"
key: description: - The SSH public key(s), as a string or (since Ansible 1.9) url (https://github.com/username.keys). required: true type: str path: description: - Alternate path to the authorized_keys file. - When unset, this value defaults to I(~/.ssh/authorized_keys). type: path user: description: - The username on the remote host whose authorized_keys file will be modified. required: true type: str state: choices: - absent - present default: present description: - Whether the given key (with the given key_options) should or should not be in the file. type: str follow: default: false description: - Follow path symlink instead of replacing it. type: bool comment: description: - 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. type: str exclusive: default: false description: - Whether to remove all other non-specified keys from the authorized_keys file. - Multiple keys can be specified in a single C(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 C(key) in a single batch as mentioned above. type: bool manage_dir: default: true description: - Whether this module should manage the directory of the authorized key file. - If set to C(true), the module will create the directory, as well as set the owner and permissions of an existing directory. - Be sure to set C(manage_dir=false) if you are using an alternate directory for authorized_keys, as set with C(path), since you could lock yourself out of SSH access. - See the example below. type: bool key_options: description: - A string of ssh key options to be prepended to the key in the authorized_keys file. type: str validate_certs: default: true description: - 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. - This should only set to C(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 C(true). type: bool
exclusive: description: If the key has been forced to be exclusive or not. returned: success sample: false type: bool key: description: The key that the module was running against. returned: success sample: https://github.com/user.keys type: str key_option: description: Key options related to the key. returned: success sample: null type: str keyfile: description: Path for authorized key file. returned: success sample: /home/user/.ssh/authorized_keys type: str manage_dir: description: Whether this module managed the directory of the authorized key file. returned: success sample: true type: bool path: description: Alternate path to the authorized_keys file returned: success sample: null type: str state: description: Whether the given key (with the given key_options) should or should not be in the file returned: success sample: present type: str unique: description: Whether the key is unique returned: success sample: false type: bool user: description: The username on the remote host whose authorized_keys file will be modified returned: success sample: user type: str validate_certs: description: 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. returned: success sample: true type: bool