【问题标题】:How to add an existing public key to authorized_keys file using Ansible and user module?如何使用 Ansible 和用户模块将现有公钥添加到 authorized_keys 文件?
【发布时间】:2018-04-25 05:31:38
【问题描述】:

我正在使用 Ansible 编写一个简单的任务来创建用户并添加现有的 RSA 公钥。这是我写的代码:

- name: SYSTEM - Create test user
  tags: system-user
  user: 
        name: "{{ test_user }}"
        state: present
        createhome: yes

- name: SYSTEM - Add existing pub key for test user
  tags: system-user
  copy: 
       content: "{{ test_user_pubkey }}"
       dest: "/tmp/test_user_id_rsa.pub"
       force: no
       owner: "{{ test_user }}"
       group: "{{ test_user }}"
       mode: 0600

- name: SYSTEM - Set authorized key for test_user took from file
  tags: system-user
  authorized_key:
        user: "{{ test_user }}"
        state: present
        key: "{{ lookup('file', '/tmp/test_user_id_rsa.pub') }}"

我编写的代码并不优雅,我认为最好的选择是将现有的 RSA 公钥与用户创建块一起添加,以便创建和填充 authorized_keys 文件。

我已经阅读了Ansible user module 但 ssh_key_file 方法不包括将现有 pub 密钥的值回显到authorized_keys 文件的可能性(最终目的是能够使用用户远程连接 ssh和私钥)。

ssh_key_file = 可选择指定 SSH 密钥文件名。如果这是一个 相对文件名,那么它将相对于用户的家 目录。

是否可以使用 Ansible 在用户模块中管理此过程?

【问题讨论】:

  • 所以你写了一些代码,甚至在发布到 StackOverflow 之前都懒得尝试?因为它会在执行时失败。它会说/tmp/test_user_id_rsa.pub 不存在(除非你的本地计算机上有乱七八糟的东西)。
  • 您说得对,错误消息是我在测试时得到的,但我忘了在帖子中提及。我将对其进行编辑以进行澄清,并在以后的帖子中更加小心。此外,我不太确定为什么要使用代码获取它,因为该文件在运行副本后存在。

标签: ssh ansible ssh-keys


【解决方案1】:

你的问题的答案是:

- name: SYSTEM - Create test user
  tags: system-user
  user: 
    name: "{{ test_user }}"
    state: present
    createhome: yes

- name: SYSTEM - Set authorized key for test_user took from file
  tags: system-user
  authorized_key:
    user: "{{ test_user }}"
    state: present
    key: "{{ test_user_pubkey }}"

这就是我们所需要的。


关于您阅读文档,ssh_key_file 与生成 SSH 密钥对有关,这不是您想要的。

【讨论】:

    【解决方案2】:

    所以我一直潜伏在这个线程中,试图把它缠在我的头上。我最终能够解决这个问题。

    首先,我倾向于将所有内容都塞进 dicts 中,然后使用 |每当我需要在 jinja2 中循环时使用 dict2items。

    我的主要问题是,一旦用户模块生成了 ssh_keys,就没有干净的方法可以在不弯曲 Ansible 的情况下使用您刚刚制作的 authorized_key 模块(或者我认为?我可能不是这里最聪明的人)以不可能的方式(啜食?不可能在变量中放置另一个变量(根据我的尝试)“{{ slurp_{{ item.key }} | b64decode }}” 似乎无法撤消)

    因此,如果您使用大量循环并且不愿意将所有密钥复制到您的本地主机(老实说这很耗时),我发现这个鬼鬼祟祟的技巧不会让阅读您的代码成为奥林匹克挑战:

    - name: Prepare the SFTP user
      user:
        name: "{{ item.key }}"
        groups: sftp_users
        home: /home/{{ item.key }}
        password: "{{ impossible_sftp_pass }}"
        generate_ssh_key: yes
        ssh_key_file: .ssh/id_rsa
        shell: /bin/nologin
      with_dict: "{{ instances }}"
    
    - name: sneaky way to get the keys right
      shell: cat /home/{{ item.key }}/.ssh/id_rsa.pub > /home/{{ item.key }}/.ssh/authorized_keys
      args:
        creates: /home/{{ item.key }}/.ssh/authorized_keys
      with_dict: "{{ instances }}"
    

    在此示例中,我们的目标是设置一个 STFP 堡垒主机,最终将 SFTP 数据存储库同步到专用网络中的相应 Web 前端。

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2016-04-26
      • 2018-12-07
      • 1970-01-01
      相关资源
      最近更新 更多