我有以下密钥和用户列表:
---
case1:
keys:
- sshrsa1
- sshrsa2
users:
- user1
- user2
- user4
case2:
keys:
- sshrsa3
- sshrsa4
- sshrsa5
users:
- user1
- user2
- user5
我如何将这些列表与authorized_key组合使用,以便将case 1下的所有密钥放置在所有用户的authorized_file中,如下面的示例所示?
user1's auth file contains:
sshrsa1
sshrsa2
user2's auth file contains:
sshrsa1
sshrsa2
user4's auth file contains:
sshrsa1
sshrsa2
与案例2相同:
user1's auth file contains:
sshrsa3
sshrsa4
sshrsa5
user2's auth file contains:
sshrsa3
sshrsa4
sshrsa5
user5's auth file contains:
sshrsa3
sshrsa4
sshrsa5
给定数据
auth_keys:
case1:
keys: [sshrsa1, sshrsa2]
users: [user1, user2, user4]
case2:
keys: [sshrsa3, sshrsa4, sshrsa5]
users: [user1, user2, user5]
Q:";将大小写*下的所有密钥放入用户的authorized_key文件中">
A: 将字典转换为列表,并使用_subelements迭代。例如,
- debug:
msg: "user: {{ item.1 }}, key: {{ item.0.value['keys'] }}"
with_subelements:
- "{{ auth_keys|dict2items }}"
- value.users
给出(节略(
msg: 'user: user1, key: [''sshrsa1'', ''sshrsa2'']'
msg: 'user: user2, key: [''sshrsa1'', ''sshrsa2'']'
msg: 'user: user4, key: [''sshrsa1'', ''sshrsa2'']'
msg: 'user: user1, key: [''sshrsa3'', ''sshrsa4'', ''sshrsa5'']'
msg: 'user: user2, key: [''sshrsa3'', ''sshrsa4'', ''sshrsa5'']'
msg: 'user: user5, key: [''sshrsa3'', ''sshrsa4'', ''sshrsa5'']'
钞票
参数键需要一个字符串。将列表中的键转换为字符串中的行。例如,
- ansible.posix.authorized_key:
user: "{{ item.1 }}"
key: "{{ _key }}"
with_subelements:
- "{{ auth_keys|dict2items }}"
- value.users
vars:
_key: |
{% for key in item.0.value['keys'] %}
{{ key }}
{% endfor %}