使用Ansible将服务器添加到/etc/exports

使用Ansible将服务器添加到/etc/exports,ansible,ansible-2.x,Ansible,Ansible 2.x,我试图让ansible为{{ips}}变量中的每个值向/etc/exports添加行,而不是让它们都在同一行上 我尝试了几种不同的方法,包括lineinfle、blockinfle和replace,它们似乎比其他方法更接近我,但我仍然无法得到我想要的结果 对于某些服务器,我使用这种格式添加它们 /foo/bar server1.com (rw,sync,no_root_squash) /foo/bar server2.com (rw,sync,no_root_squash) /foo/bar s

我试图让ansible为{{ips}}变量中的每个值向/etc/exports添加行,而不是让它们都在同一行上

我尝试了几种不同的方法,包括lineinfle、blockinfle和replace,它们似乎比其他方法更接近我,但我仍然无法得到我想要的结果

对于某些服务器,我使用这种格式添加它们

/foo/bar server1.com (rw,sync,no_root_squash)
/foo/bar server2.com (rw,sync,no_root_squash)
/foo/bar server3.com (rw,sync,no_root_squash)
/foo/bar server1(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)
使用此代码

- name: Add New server to /etc/exports
lineinfile:
  path: /etc/exports
  insertafter: "# This Line"
  line: "/foo/bar {{ item }}(rw,sync,no_root_squash)"
loop: "{{ ips.split(',') }}"
对于其他服务器,我需要使用这种格式,但我似乎无法使其正常工作

/foo/bar server1(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)

我想出了一个办法来得到我想要的东西。如果有人知道更好的方法,我愿意接受建议,但到目前为止,这是可行的

  - name: Add Servers to /etc/exports
    lineinfile:
      path: /etc/exports
      insertafter: "# This Line"
      line: "/foo/bar {{ ips.split(',') | join('(rw,sync,no_root_squash) ') }}(rw,sync,no_root_squash)"
这将以这种格式在ips变量中添加服务器

/foo/bar server1.com (rw,sync,no_root_squash)
/foo/bar server2.com (rw,sync,no_root_squash)
/foo/bar server3.com (rw,sync,no_root_squash)
/foo/bar server1(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)

我想
/etc/exports
的模板是不可能的?