带有子元素的Ansible Use dict2items

时间:2018-10-08 20:28:14

标签: ansible

我是Ansible的新手,在弄清楚如何正确执行此操作时遇到了麻烦...

我有一个用户列表,在他们下面是我希望Ansible为我设置的git config设置列表。例如,我有这样的东西:

---
- hosts: all
  gather_facts: no
  vars:
    user_list:
      - name: john.doe
        git_config:
          - user.name: John Doe
          - user.email: john.doe@example.com
          - user.signingkey: 0A46826A
      - name: jane.doe
        git_config:
          - user.name: Jane Doe
          - diff.tool: meld
  tasks:
    - debug:
        msg: "User: {{ item.0.name }}, Key: {{ item.1 }}"
      loop: "{{ user_list | subelements('git_config') }}"

# Eventually, I would like to be able to do something like this using Ansible's git_config module:  https://docs.ansible.com/ansible/2.4/git_config_module.html
#   - git_config:
#       name: "{{ item.1.key }}"
#       value: "{{ item.1.value }}"
#       scope: global
#     loop: "{{ user_list | <<Some Ansible Magic>> }}"
#     become: yes
#     become_user: "{{ item.0.name }}"

我对此进行了测试

ansible-playbook git_config.yml

哪个给我这个:

PLAY [all] *****************************************************************************************************************************************************************************************************************************************************************************

TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=[{u'name': u'john.doe', u'git_config': [{u'user.name': u'John Doe'}, {u'user.email': u'john.doe@example.com'}, {u'user.signingkey': u'0A46826A'}]}, {u'user.name': u'John Doe'}]) => {
    "msg": "User: john.doe, Key: {u'user.name': u'John Doe'}"
}
ok: [localhost] => (item=[{u'name': u'john.doe', u'git_config': [{u'user.name': u'John Doe'}, {u'user.email': u'john.doe@example.com'}, {u'user.signingkey': u'0A46826A'}]}, {u'user.email': u'john.doe@example.com'}]) => {
    "msg": "User: john.doe, Key: {u'user.email': u'john.doe@example.com'}"
}
ok: [localhost] => (item=[{u'name': u'john.doe', u'git_config': [{u'user.name': u'John Doe'}, {u'user.email': u'john.doe@example.com'}, {u'user.signingkey': u'0A46826A'}]}, {u'user.signingkey': u'0A46826A'}]) => {
    "msg": "User: john.doe, Key: {u'user.signingkey': u'0A46826A'}"
}
ok: [localhost] => (item=[{u'name': u'jane.doe', u'git_config': [{u'user.name': u'Jane Doe'}, {u'diff.tool': u'meld'}]}, {u'user.name': u'Jane Doe'}]) => {
    "msg": "User: jane.doe, Key: {u'user.name': u'Jane Doe'}"
}
ok: [localhost] => (item=[{u'name': u'jane.doe', u'git_config': [{u'user.name': u'Jane Doe'}, {u'diff.tool': u'meld'}]}, {u'diff.tool': u'meld'}]) => {
    "msg": "User: jane.doe, Key: {u'diff.tool': u'meld'}"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

我想要的是能够以某种方式提取键和值(我需要知道用户名,以便可以成为用户,然后需要遍历所有git config键/值,以便我可以使用Ansible的git配置模块:https://docs.ansible.com/ansible/2.4/git_config_module.html):

"msg": "User: jane.doe, Key: diff.tool, Value: meld"

带有这样的内容

- name: just testing
  debug:
    msg: "User: {{ item.0.name }}, Key: {{ item.1.key }}, Value: {{ item.1.value }}"

请参阅git_config.yml,以更好地了解我要做什么。

0 个答案:

没有答案