ansible - 使用列表项解析字典

标签 ansible jinja2

我的字典看起来像这样:

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

但是我收到错误:

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'"}

我在这里做错了什么?

最佳答案

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

关于ansible - 使用列表项解析字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064270/

相关文章:

git - 使用私有(private) git 角色依赖的 Ansible 剧本

使用 if 语句的 Ansible 处理程序

python-3.x - Ansible 帮助 : "msg": "The task includes an option with an undefined variable

ansible - 如何制作字典元素顶级变量

python - 找不到 flask 模板

python - 适用于 Python 3.4.4 的 Jinja2 版本

Vagrant Ansible CentOS 7 如何安装 MySQL 5.7 并更改默认密码

python - 在 Tornado 模板中通过 StringIO/BytesIO 动态提供图像

python - Jinja2 中的访问变量包括

python - 如何在 Flask 中为动态生成的 jinja2 模板注册过滤器?