ansible.posix.authorized_key 模块 – 添加或删除 SSH 授权密钥

注意

此模块是 ansible.posix 集合(版本 1.6.2)的一部分。

如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install ansible.posix

要在 playbook 中使用它,请指定:ansible.posix.authorized_key

ansible.posix 1.0.0 中的新增功能

概要

  • 为特定用户帐户添加或删除 SSH 授权密钥。

参数

参数

注释

comment

字符串

更改公钥上的注释。

在诸如从 GitHub 或 GitLab 获取注释的情况下,重写注释很有用。

如果未指定注释,则将保留现有注释。

exclusive

布尔值

是否从 authorized_keys 文件中删除所有其他未指定的密钥。

可以通过换行符分隔在单个 key 字符串值中指定多个密钥。

此选项不感知循环,因此如果您使用 with_,它将对循环的每次迭代都是独占的。

如果您希望文件中包含多个密钥,则需要如上所述将它们全部以单个批次传递给 key

选项

  • false ←(默认)

  • true

follow

布尔值

跟随路径符号链接而不是替换它。

选项

  • false ←(默认)

  • true

key

字符串 / 必需

SSH 公钥,作为字符串或(自 Ansible 1.9 起)URL (https://github.com/username.keys)。

key_options

字符串

要在 authorized_keys 文件中预先添加到密钥的 ssh 密钥选项字符串。

manage_dir

布尔值

此模块是否应管理授权密钥文件的目录。

如果设置为 true,则模块将创建目录,并设置现有目录的所有者和权限。

如果要对 authorized_keys 使用备用目录(如使用 path 设置),请务必设置 manage_dir=false,因为您可能会把自己锁定在 SSH 访问之外。

请参阅下面的示例。

选项

  • false

  • true ←(默认)

path

path

授权密钥文件的备用路径。

默认值是 user 参数中指定的用户主目录的 .ssh/authorized_keys

大多数情况下,无需设置此键。

如果您需要明确指向目标 authorized_keys,请使用其路径。

state

字符串

给定的密钥(具有给定的 key_options)是否应在文件中。

选项

  • "absent"

  • "present" ←(默认)

user

字符串 / 必需

将在其 authorized_keys 文件中进行修改的远程主机上的用户名。

validate_certs

布尔值

这仅适用于使用 https URL 作为密钥源的情况。

如果设置为 false,则不会验证 SSL 证书。

这应该仅在个人控制的站点上使用自签名证书时设置为 false,因为它会避免验证源站点。

在 2.1 之前,代码的工作方式就好像将其设置为 true

选项

  • false

  • true ←(默认)

示例

- 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

描述

exclusive

布尔值

是否强制该密钥为独占。

返回: 成功

示例: false

key

字符串

模块正在操作的密钥。

返回: 成功

示例: "https://github.com/user.keys"

key_option

字符串

与密钥相关的选项。

返回: 成功

keyfile

字符串

授权密钥文件的路径。

返回: 成功

示例: "/home/user/.ssh/authorized_keys"

manage_dir

布尔值

此模块是否管理了授权密钥文件的目录。

返回: 成功

示例: true

path

字符串

授权密钥文件的备用路径

返回: 成功

state

字符串

给定的密钥(带有给定的密钥选项)是否应该存在于文件中。

返回: 成功

示例: "present"

unique

布尔值

该密钥是否是唯一的。

返回: 成功

示例: false

user

字符串

远程主机上要修改其 authorized_keys 文件的用户名。

返回: 成功

示例: "user"

validate_certs

布尔值

仅当使用 https URL 作为密钥源时才适用。如果设置为false,则不会验证 SSL 证书。

返回: 成功

示例: true

作者

  • Ansible 核心团队