通过字典与列表项进行解析

时间:2019-05-09 16:59:51

标签: ansible jinja2

我的字典看起来像这样:

seed_job_additional_git_scms:
  TestUrl:
    credentials: "TestCredentials"
    branch: "testBranch"
    directory: "testDirectory"
  TestUrl2:
    credentials: "TestCredentials2"
    branch: "testBranch2"
    directory: "testDirectory2"

现在使用正常的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 %}

但是我收到了错误消息:

TASK [copy : Template required files.] *******************************************************************************************************************************************************************************************************
failed: [localhost] (item={u'dest': u'/tmp/config.xml', u'src': u'job.j2'}) => {"changed": false, "item": {"dest": "/tmp/config.xml", "src": "job.j2"}, "msg": "AnsibleUndefinedVariable: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'items'"}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

变量seed_job_additional_git_scms是一个字典。当您编写这样的循环时...

{% for dict_item in seed_job_additional_git_scms %}

...然后您要遍历字典的。这意味着对于每次循环迭代,dict_item是一个字符串。这就是为什么出现此错误的原因:

AnsibleUnicode object' has no attribute 'items'

因为字符串对象没有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 %}

这将导致:

      <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>