Ansible 使用列表项通过字典进行解析

Ansible 使用列表项通过字典进行解析,ansible,jinja2,Ansible,Jinja2,我的字典是这样的: seed_job_additional_git_scms: TestUrl: credentials: "TestCredentials" branch: "testBranch" directory: "testDirectory" TestUrl2: credentials: "TestCredentials2" branch: "testBranch2" directory: "testDirectory2" 现

我的字典是这样的:

seed_job_additional_git_scms:
  TestUrl:
    credentials: "TestCredentials"
    branch: "testBranch"
    directory: "testDirectory"
  TestUrl2:
    credentials: "TestCredentials2"
    branch: "testBranch2"
    directory: "testDirectory2"
现在,使用ansible
debug
模块正常迭代,我得到了我想要的:

- name: Print 
  debug:
    msg: "Repo {{ item.key }} has credentials  {{ item.value.credentials }}. Its used branch {{ item.value.branch }} and gets saved to directory {{ item.value.branch }}"
  loop: "{{ lookup('dict', seed_job_additional_git_scms) }}"



TASK [copy : Print ] ************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': u'TestUrl', 'value': {u'directory': u'testDirectory', u'credentials': u'TestCredentials', u'branch': u'testBranch'}}) => {
    "msg": "Repo TestUrl has credentials  TestCredentials. Its used branch testBranch and gets saved to directory testBranch"
}
ok: [localhost] => (item={'key': u'TestUrl2', 'value': {u'directory': u'testDirectory2', u'credentials': u'TestCredentials2', u'branch': u'testBranch2'}}) => {
    "msg": "Repo TestUrl2 has credentials  TestCredentials2. Its used branch testBranch2 and gets saved to directory testBranch2"
现在,我正在尝试使用Jinja在模板文件中购买同样的东西

我尝试的是:

{% for dict_item in seed_job_additional_git_scms %}
   {% for key, value in dict_item.items() %}
      <h1>URL: {{key}}</h1>
      <h2>Credentials: {{ value.credentials }}</h2>
      <h2>Branch: {{ value.branch }}</h2>
      <h2>Direcotry: {{ value.directory }}</h2>
   {% endfor %}
{% endfor %}

我做错了什么

变量
seed\u job\u additional\u git\u scms
是一个字典。当你写这样的循环时

{% for dict_item in seed_job_additional_git_scms %}
…然后您将迭代字典的键。这意味着对于每个循环迭代,
dict\u item
是一个字符串。这就是为什么会出现此错误:

AnsibleUnicode object' has no attribute 'items'
因为字符串对象没有
方法。此外,您正在使用嵌套循环来尝试并解决一些不必要的问题:即使我们要用外部循环来解决问题,它仍然不能满足您的需要。我想你想要这个:

{% for key, value in seed_job_additional_git_scms.items() %}
      <h1>URL: {{key}}</h1>
      <h2>Credentials: {{ value.credentials }}</h2>
      <h2>Branch: {{ value.branch }}</h2>
      <h2>Direcotry: {{ value.directory }}</h2>
{% endfor %}
{%表示键,seed\u job\u additional\u git\u scms.items()中的值%}
URL:{{key}}
凭据:{value.Credentials}
分支:{{value.Branch}
目录尝试:{{value.directory}
{%endfor%}
其结果是:

      <h1>URL: TestUrl</h1>
      <h2>Credentials: TestCredentials</h2>
      <h2>Branch: testBranch</h2>
      <h2>Direcotry: testDirectory</h2>
      <h1>URL: TestUrl2</h1>
      <h2>Credentials: TestCredentials2</h2>
      <h2>Branch: testBranch2</h2>
      <h2>Direcotry: testDirectory2</h2>
URL:TestUrl
凭证:TestCredentials
分支:测试分支
目录:testDirectory
URL:TestUrl2
凭证:TestCredentials2
分支:testBranch2
目录:testDirectory2
      <h1>URL: TestUrl</h1>
      <h2>Credentials: TestCredentials</h2>
      <h2>Branch: testBranch</h2>
      <h2>Direcotry: testDirectory</h2>
      <h1>URL: TestUrl2</h1>
      <h2>Credentials: TestCredentials2</h2>
      <h2>Branch: testBranch2</h2>
      <h2>Direcotry: testDirectory2</h2>