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 授权密钥。
参数
参数 |
注释 |
---|---|
更改公钥上的注释。 在诸如从 GitHub 或 GitLab 获取注释的情况下,重写注释很有用。 如果未指定注释,则将保留现有注释。 |
|
跟随路径符号链接而不是替换它。 选项
|
|
SSH 公钥,作为字符串或(自 Ansible 1.9 起)URL (https://github.com/username.keys)。 |
|
要在 authorized_keys 文件中预先添加到密钥的 ssh 密钥选项字符串。 |
|
此模块是否应管理授权密钥文件的目录。 如果设置为 如果要对 authorized_keys 使用备用目录(如使用 请参阅下面的示例。 选项
|
|
授权密钥文件的备用路径。 默认值是 大多数情况下,无需设置此键。 如果您需要明确指向目标 authorized_keys,请使用其路径。 |
|
给定的密钥(具有给定的 key_options)是否应在文件中。 选项
|
|
将在其 authorized_keys 文件中进行修改的远程主机上的用户名。 |
|
这仅适用于使用 https URL 作为密钥源的情况。 如果设置为 这应该仅在个人控制的站点上使用自签名证书时设置为 在 2.1 之前,代码的工作方式就好像将其设置为 选项
|
示例
- 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 |
描述 |
---|---|
是否强制该密钥为独占。 返回: 成功 示例: |
|
模块正在操作的密钥。 返回: 成功 示例: |
|
与密钥相关的选项。 返回: 成功 |
|
授权密钥文件的路径。 返回: 成功 示例: |
|
此模块是否管理了授权密钥文件的目录。 返回: 成功 示例: |
|
授权密钥文件的备用路径 返回: 成功 |
|
给定的密钥(带有给定的密钥选项)是否应该存在于文件中。 返回: 成功 示例: |
|
该密钥是否是唯一的。 返回: 成功 示例: |
|
远程主机上要修改其 authorized_keys 文件的用户名。 返回: 成功 示例: |
|
仅当使用 https URL 作为密钥源时才适用。如果设置为 返回: 成功 示例: |
作者
Ansible 核心团队