使用 Ansible 将服务器添加到 /etc/exports
Add servers to /etc/exports with Ansible
我试图让 ansible 为 {{ips}} 变量中的每个值添加行到 /etc/exports,而不是让它们都在同一行。
我尝试了几种不同的方法,包括 lineinfile、blockinfile 和 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)
使用此代码。
- 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(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)
我试图让 ansible 为 {{ips}} 变量中的每个值添加行到 /etc/exports,而不是让它们都在同一行。
我尝试了几种不同的方法,包括 lineinfile、blockinfile 和 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)
使用此代码。
- 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(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)