我有一个list,它实际上是一个dict 的键列表。我想得到一个连接字符串,其中dict 在这个list 的键上过滤,并在模块选项中使用它。
我的用例是拥有公钥名称列表以生成授权密钥文件的用户。
1 ---
2 - hosts: localhost
3 become: false
4 vars:
5 pub_keys:
6 key01: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ[…]5/ someuser@somehost
7 key02: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ[…]ea otheruser@somewher
8 key03: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ[…]dN anyser@anyhost
9 users:
10 root:
11 home: /root
12 shell: /bin/bash
13 authorized_keys:
14 - key01
15 mgmtusr:
16 home: /home/mgmtusr
17 shell: /bin/bash
18 authorized_keys:
19 - key01
20 - key02
21 - key03
22
23 tasks:
24 - name: Debug Authorized Keys
25 debug:
26 msg: "USER:{{ item.key }} AUTHKEYSLIST:{{ pub_keys|selectattr(item.authorized_keys) }}"
27 with_dict: "{{ users }}"
28
29 - name: Manage users Authorized Keys
30 authorized_key:
31 user: "{{ item.key }}"
32 key: "{{ pub_keys|selectattr(item.authorized_keys) }}"
33 exclusive: yes
34 with_dict: "{{ users }}"
35
正如您在此处看到的,我尝试使用dict|selectattr(list),但失败了。
在调试模块中获取<generator object select_or_reject at 0x…>,当然在authorized_key 模块中获取invalid key specified。
任务 [调试授权密钥] ******************************************** ****************************************************** ****************************************************** ****************************************************** ************************************************
ok: [localhost] => (item={'key': u'mgmtusr', 'value': {u'home': u'/home/mgmtusr', u'shell': u'/bin/bash' , u'authorized_keys': [u'key01', u'key02', u'key03']}}) => {
“物品”: {
“关键”:“mgmtusr”,
“价值”: {
“授权密钥”:[
"key01",
"key02",
“key03”
],
"家": "/home/mgmtusr",
“外壳”:“/bin/bash”
}
},
“味精”:“用户:mgmtusr AUTHKEYSLIST:”
}
ok: [localhost] => (item={'key': u'root', 'value': {u'home': u'/root', u'shell': u'/bin/bash', u 'authorized_keys': [u'key01']}}) => {
“物品”: {
“键”:“根”,
“价值”: {
“授权密钥”:[
“key01”
],
“家”:“/根”,
“外壳”:“/bin/bash”
}
},
“味精”:“用户:根 AUTHKEYSLIST:”
}
任务 [管理用户授权密钥] ******************************************* ****************************************************** ****************************************************** ****************************************************** ******************************************
失败:[localhost] (item={'key': u'mgmtusr', 'value': {u'home': u'/home/mgmtusr', u'shell': u'/bin/bash', u 'authorized_keys': [u'key01', u'key02', u'key03']}}) => {"changed": false, "failed": true, "item": {"key": "mgmtusr" , "value": {"authorized_keys": ["key01", "key02", "key03"], "home": "/home/mgmtusr", "shell": "/bin/bash"}}, "msg ": "查找用户 mgmtusr 失败: 'getpwnam(): name not found: mgmtusr'"}
失败:[localhost] (item={'key': u'root', 'value': {u'home': u'/root', u'shell': u'/bin/bash', u'authorized_keys ': [u'key01']}}) => {"changed": false, "failed": true, "item": {"key": "root", "value": {"authorized_keys": [" key01"], "home": "/root", "shell": "/bin/bash"}}, "msg": "指定的键无效:"}
像其他尝试一样 (with_subelements, lookup('template', ...) selectattr 似乎不是解决方案。
有什么建议吗?