如何使用 Ansible 和用户模块将现有的 public 键添加到 authorized_keys 文件?
How to add an existing public key to authorized_keys file using Ansible and user module?
我正在使用 Ansible 编写一个简单的任务来创建用户并添加现有的 RSA public 密钥。这是我写的代码:
- 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 public 密钥添加到用户创建块中,以便创建和填充 authorized_keys
文件。
我读过 Ansible user module 但 ssh_key_file 方法不包括将现有 pub 密钥的值回显到 authorized_keys
文件的可能性(最终目的是能够使用用户和私钥通过 ssh 进行远程连接。
ssh_key_file = Optionally specify the SSH key filename. If this is a
relative filename then it will be relative to the user's home
directory.
是否可以使用 Ansible 在用户模块中管理此进程?
你的问题的答案是:
- 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 密钥对有关,这不是您想要的。
所以我一直潜伏在这个线程中,试图把它缠绕在我的头上。我最终能够绕过这个问题。
首先,我倾向于将所有内容都塞进字典中,然后使用 |每当我需要在 jinja2 中循环时使用 dict2items。
我的主要问题是,一旦用户模块生成 ssh_keys,就没有干净的方法可以将 authorized_key 模块与您刚刚创建的模块一起使用(或者我认为如此?我可能是不是这里最聪明的人)而不以不可能的方式弯曲 Ansible(啜饮?不可能将另一个变量放在一个变量中(根据我的尝试)”{{ 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 堡垒主机,最终将 rsync SFTP 数据回购到私有网络中适当的 Web 前端。
我正在使用 Ansible 编写一个简单的任务来创建用户并添加现有的 RSA public 密钥。这是我写的代码:
- 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 public 密钥添加到用户创建块中,以便创建和填充 authorized_keys
文件。
我读过 Ansible user module 但 ssh_key_file 方法不包括将现有 pub 密钥的值回显到 authorized_keys
文件的可能性(最终目的是能够使用用户和私钥通过 ssh 进行远程连接。
ssh_key_file = Optionally specify the SSH key filename. If this is a relative filename then it will be relative to the user's home directory.
是否可以使用 Ansible 在用户模块中管理此进程?
你的问题的答案是:
- 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 密钥对有关,这不是您想要的。
所以我一直潜伏在这个线程中,试图把它缠绕在我的头上。我最终能够绕过这个问题。
首先,我倾向于将所有内容都塞进字典中,然后使用 |每当我需要在 jinja2 中循环时使用 dict2items。
我的主要问题是,一旦用户模块生成 ssh_keys,就没有干净的方法可以将 authorized_key 模块与您刚刚创建的模块一起使用(或者我认为如此?我可能是不是这里最聪明的人)而不以不可能的方式弯曲 Ansible(啜饮?不可能将另一个变量放在一个变量中(根据我的尝试)”{{ 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 堡垒主机,最终将 rsync SFTP 数据回购到私有网络中适当的 Web 前端。