我正在写一本小剧本,想与一些人分享。 此剧本需要一个或多个 ssh 公钥才能安装在 ansible 节点上。
为了更容易定制,我认为在环境变量中设置 SSH 公钥将是最好的解决方案,但我可以研究任何更好的建议。
因此,用户只需设置:
export ANSIBLE_SSH_PUBKEY=${HOME}/.ssh/my_ssh_key.pub
在剧本中,这是这样处理的:
- name: Install ssh public key
ansible.posix.authorized_key:
user: ansible
state: present
key: '{{ item }}'
with_file: '{{ lookup("env", "ANSIBLE_SSH_PUBKEY") }}'
目前,我只管理一个 ssh 公钥安装。
所以,我的问题是:如何处理任意数量的公钥?
最佳答案
要么允许他们导入所有的公钥,with_fileglob
改为循环:
export ANSIBLE_SSH_FOLDER=~/.ssh
- name: Install ssh public key
ansible.posix.authorized_key:
user: ansible
state: present
key: '{{ item }}'
with_fileglob: '{{ lookup("env", "ANSIBLE_SSH_FOLDER") }}/*'
或者允许它们使用冒号分隔值,然后在该分隔符上拆分环境变量:
export ANSIBLE_SSH_PUBKEY=~/.ssh/my_ssh_key.pub:~/.ssh/my_other_ssh_key.pub
- name: Install ssh public key
ansible.posix.authorized_key:
user: ansible
state: present
key: '{{ item }}'
with_file: '{{ (lookup("env", "ANSIBLE_SSH_PUBKEY")).split(":") }}'
关于ansible - 如何从环境变量安装多个 ssh 公钥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72301231/