【问题标题】:Ansible - Filter a dict with a list of keysAnsible - 使用键列表过滤字典
【发布时间】:2018-05-01 04:12:09
【问题描述】:

我有一个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 似乎不是解决方案。 有什么建议吗?

【问题讨论】:

    标签: loops ansible


    【解决方案1】:

    给你:

    - name: Manage users Authorized Keys
      authorized_key:
        user: "{{ item.key }}"
        key: "{{ item.value.authorized_keys | map('extract',pub_keys) | list | join('\n') }}"
        exclusive: yes
      with_dict: "{{ users }}"
    

    请参阅extract 过滤器使用情况。

    此外,当您使用 map 时,您几乎应该始终将其类型转换为 list 以防止出现 generator object 值。

    【讨论】:

    • 感谢您的回答!我在搜索时在map 上阅读了一些内容,但还没有得到它。所以现在很明确,我必须更多地了解那个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2020-04-08
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多