我对Ansible有点陌生,他试图编写一本涵盖几种不同用例的剧本,但由于我到目前为止所拥有的一切都无法正常工作,因此必定会缺少一些明显的方法来设置它。基本上,按照我写剧本的方式,我不能使用exclusive: no
,因为我正在使用with_items
,但发现它不是循环感知的。
分发ssh密钥的约束如下:
我在下面定义了一个vars_file
,它将远程用户和远程主机分配给给定的密钥。我不确定这是否是最好的方法,因为当用户需要以两个不同的用户身份访问服务器时,我最终为同一密钥定义了多个哈希。例如:开发人员(timmy)正在部署Rails应用程序并从passenger
内部使用capistrano
帐户,但有时他会以 timmy 的身份使用sudo
重新启动Web服务器。
另外,人们来来往往,因此在运行剧本时,它首先需要删除其中的任何~/.ssh/authorized_keys
,以便可以从已添加或删除键的更新后的vars_file
中重建它。 (只需追加到该文件即可将旧密钥保留在原处)。我没有在下面的代码段中包含该部分,因为..简洁起见。我无法工作的部分是authorized_keys
位。有人有建议吗?也许我完全需要使用host_vars
或loop
重新定义整个内容?
ssh_keys:
- name : eric_site_key
group : systems
pubkey : "{{ lookup('file', '../group_vars/pubkeys/eric_site.pub') }}"
remote_users:
- eric
remote_hosts:
- all
- name : eric_home_key
group : systems
pubkey : "{{ lookup('file', '../group_vars/pubkeys/eric_homekey.pub') }}"
remote_users:
- eric
remote_hosts:
- sshgateway1
- sshgateway2
- name : timmy_site_key
group : systems
pubkey : "{{ lookup('file', '../group_vars/pubkeys/timmy_site.pub') }}"
remote_users:
- timmy
remote_hosts:
- sshgateway1
- name : timmy_site_key
group : systems
pubkey : "{{ lookup('file', '../group_vars/pubkeys/timmy_site.pub') }}"
remote_users:
- timmy
- passenger
remote_hosts:
- webserver1
- webserver2
剧本片段:
---
- hosts: all
vars_files:
- ../group_vars/ssh_keys.yml
- name: setup ssh pub key
when: inventory_hostname in item.0.remote_hosts or 'all' in item.0.remote_hosts
authorized_key:
user : "{{ item.1.remote_users }}"
state : present
key : '{{ item.0.pubkey }}'
exclusive: no
with_subelements:
- "{{ ssh_keys }}"
- remote_users