Ansible 文件查找适用于调试,但不适用于authorized_key模块 - 如何实现从文件中获取密钥?

Ansible 文件查找适用于调试,但不适用于authorized_key模块 - 如何实现从文件中获取密钥?

我正在尝试构建一个包含分发授权 SSH 密钥的剧本。

每个用户的密钥都放入以用户名命名的自己的文件中。需要分发的用户设置在变量中,然后使用 lookup 循环读取文件。奇怪的是,debug模块可以工作,但authorized_key模块不能与完全相同的 lookup 一起工作。这是剧本:

- hosts: hosts
  vars_files:
  - users-config.yaml
  tasks:
  - debug:
      msg: "{{ lookup('file', 'ssh_keys/' + item.username) }}"
    when: item.state == "present"
    loop: "{{ users }}"
  - name: distirbute authorized_keys
    ansible.posix.authorized_key:
      user: "{{ item.key }}"
      key: "{{ lookup('file', 'ssh_keys/' + item.username) }}"
      manage_dir: true
    when: item.state == "present"
    loop: "{{ users }}"

users-config.yaml具有以下结构:

users:
- username: apushkin
  gecos: Alexander Pushkin
  state: present
  groups: wheel
- username: nkhrushchev
  gecos: Nikita Khrushchev
  state: present
  groups: wheel

ssh_keys/apushkin每个都nkhrushchev包含一个或多个 SSH 密钥,并且可读。ansible-playbook --check playbook.yaml结果输出如下:

TASK [debug] ********************************************************************************************************************************************************************************
ok: [host1] => (item={'username': 'apushkin', 'gecos': 'Alexander Pushkin', 'state': 'present', 'groups': 'wheel'}) => {
    "msg": "ssh-rsa AAAA..."
}
ok: [host1] => (item={'username': 'nkhrushchev', 'gecos': 'Nikita Khrushchev', 'state': 'present', 'groups': 'wheel'}) => {
    "msg": "ecdsa-sha2-nistp384 AAAA...\nssh-ed25519 AAAA..."
}
ok: [host2] => (item={'username': 'apushkin', 'gecos': 'Alexander Pushkin', 'state': 'present', 'groups': 'wheel'}) => {
    "msg": "ssh-rsa AAAA..."
}
ok: [host2] => (item={'username': 'nkhrushchev', 'gecos': 'Nikita Khrushchev', 'state': 'present', 'groups': 'wheel'}) => {
    "msg": "ecdsa-sha2-nistp384 AAAA...\nssh-ed25519 AAAA..."
}

TASK [distribute authorized_keys] ***********************************************************************************************************************************************************
fatal: [host1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'key'\n\nThe error appears to be in '/home/username/Ansible/playbook.yaml': line 9, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    loop: \"{{ users }}\"\n  - name: distribute authorized_keys\n    ^ here\n"}
fatal: [host2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'key'\n\nThe error appears to be in '/home/username/Ansible/playbook.yaml': line 9, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    loop: \"{{ users }}\"\n  - name: distribute authorized_keys\n    ^ here\n"}

为什么?还有其他方法吗?

答案1

这是关键(没有双关语的意思):

‘dict 对象’没有属性‘key’

您的用户定义没有属性key。您很可能想使用username

    ansible.posix.authorized_key:
      user: "{{ item.username }}"

相关内容