W3cubDocs

/Ansible

authorized_key - Adds or removes an SSH authorized key

Synopsis

Adds or removes SSH authorized keys for particular user accounts

Options

parameter required default choices comments
exclusive
(added in 1.9)
no no
  • yes
  • no
Whether to remove all other non-specified keys from the authorized_keys file. Multiple keys can be specified in a single key string value by separating them by newlines.
This option is not loop aware, so if you use 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 key in a single batch as mentioned above.
key
yes
The SSH public key(s), as a string or (since 1.9) url (https://github.com/username.keys)
key_options
(added in 1.4)
no
A string of ssh key options to be prepended to the key in the authorized_keys file
manage_dir
no yes
  • yes
  • no
Whether this module should manage the directory of the authorized key file. If set, the module will create the directory, as well as set the owner and permissions of an existing directory. Be sure to set manage_dir=no if you are using an alternate directory for authorized_keys, as set with path, since you could lock yourself out of SSH access. See the example below.
path
no (homedir)+/.ssh/authorized_keys
Alternate path to the authorized_keys file
state
no present
  • present
  • absent
Whether the given key (with the given key_options) should or should not be in the file
user
yes
The username on the remote host whose authorized_keys file will be modified
validate_certs
(added in 2.1)
no yes
  • yes
  • no
This only applies if using a https url as the source of the keys. If set to no, the SSL certificates will not be validated.
This should only set to no 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 yes.

Examples

# Example using key data from a local file on the management machine
- authorized_key: user=charlie key="{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"

# Using github url as key source
- authorized_key: user=charlie key=https://github.com/charlie.keys

# Using alternate directory locations:
- authorized_key:
    user: charlie
    key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
    path: '/etc/ssh/authorized_keys/charlie'
    manage_dir: no

# Using with_file
- name: Set up authorized_keys for the deploy user
  authorized_key: user=deploy key="{{ item }}"
  with_file:
    - public_keys/doe-jane
    - public_keys/doe-john

# Using key_options:
- authorized_key:
    user: charlie
    key:  "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
    key_options: 'no-port-forwarding,from="10.0.1.1"'

# Using validate_certs:
- authorized_key: user=charlie key=https://github.com/user.keys validate_certs=no

# Set up authorized_keys exclusively with one key
- authorized_key: user=root key="{{ item }}" state=present exclusive=yes
  with_file:
    - public_keys/doe-jane

# Copies the key from the user who is running ansible to the remote machine user ubuntu
- authorized_key: user=ubuntu key="{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
  become: yes

This is a Core Module

For more information on what this means please read Core Modules

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, developing_test_pr and Developing Modules.

© 2012–2016 Michael DeHaan
© 2016 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/authorized_key_module.html