小编dut*_*irf的帖子

使用 Jinja2 模板迭代嵌套列表/字典

我正在尝试通过使用 Ansible 生成 /etc/exports 文件来动态配置系统中的多个 NFS 服务器。我希望能够使用 jinja2 模板来做到这一点。这是 jinja2 模板,我无法根据我的导出列表找出它。

我在 nfs 角色中定义了以下变量:

site_nfs_servers: ['ansibletarget1', 'ansibletarget2']

exports:
  - server: "ansibletarget1"
    shares:
      - path: "/my/first/share/path"
        client: "*"
        options: "rw,sync"
      - path: "/my/second/share/path"
        client: "*"
        options: "rw,sync,root_squash"
  - server: "ansibletarget2"
    shares:
      - path: "/another/shared/path/different/server"
        client: "*"
        options: "ro,sync"
Run Code Online (Sandbox Code Playgroud)

然后我有以下 ansible play 来生成模板:

- name: Generate the exports file.
  template:
    src: exports.j2
    dest: /etc/exports
    owner: root
    group: root
    mode: '0750'
Run Code Online (Sandbox Code Playgroud)

我的模板目前如下所示:

{% for export in exports %}
{% if ansible_hostname in export.server …
Run Code Online (Sandbox Code Playgroud)

ansible ansible-playbook jinja2

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

ansible ×1

ansible-playbook ×1

jinja2 ×1