如何使用Ansible正确处理网络接口配置文件?
我希望能够使用Ansible完全管理我的/etc/network/interfaces.d/配置文件 我已经将ansible用于很多功能,包括apache文件、数据库和日志文件,但我找不到正确添加/更新/删除网络接口配置文件的方法 在我的服务器上有几个不同的项目使用不同的接口,我希望我的ansible能够在我可以部署项目的任何服务器上工作 我已经找到了使用下一个免费界面创建新文件的方法,如下所示:如何使用Ansible正确处理网络接口配置文件?,ansible,Ansible,我希望能够使用Ansible完全管理我的/etc/network/interfaces.d/配置文件 我已经将ansible用于很多功能,包括apache文件、数据库和日志文件,但我找不到正确添加/更新/删除网络接口配置文件的方法 在我的服务器上有几个不同的项目使用不同的接口,我希望我的ansible能够在我可以部署项目的任何服务器上工作 我已经找到了使用下一个免费界面创建新文件的方法,如下所示: - name: calc next free interface set_fact:
- name: calc next free interface
set_fact:
nextFreeIf: "
{%- set ifacePrefix = vars.ansible_default_ipv4.alias -%}
{%- set ifaceNum = { 'cnt': 0 } -%}
{%- macro increment(dct, key, inc=1)-%}
{%- if dct.update({key: dct[key] + inc}) -%}
{%- endif -%}
{%- endmacro -%}
{%- for iface in ansible_interfaces|sort -%}
{%- if iface| regex_search('^' ~ vars.ansible_default_ipv4.alias) -%}
{{ increment(ifaceNum, 'cnt') }}
{%- endif -%}
{%- endfor -%}
{{ifacePrefix}}:{{ifaceNum.cnt}}"
tags: network
- name: "copy network interface configuration"
template:
src: "files/etc/network/interfaces.d/my-configuration.conf"
dest: "/etc/network/interfaces.d/my-configuration.conf"
owner: root
group: root
force: true
notify: 'restart failover interface'
tags: network
现在我需要找到一种方法来检查我的配置文件是否已经存在,这样我就不会每次运行ansible时都重新创建一个新的配置文件。
但如果存在,仍然存在一个问题:
网络配置文件如下所示
auto {{ interface }}
iface {{ interface }} inet static
address {{ ip }}
netmask 255.255.255.255
因为我不知道我的项目使用哪个接口,所以我需要检查每个可用接口是否与实际文件匹配,如果不匹配,则使用下一个免费接口进行更新
我找不到用Ansible做这件事的方法
希望您能帮助我。要检查您的conf文件是否存在,您可以使用stat()
我找到了一个很好的方法来做我想做的事: 如果是的话,我想不出使用了什么接口。这就是为什么我想检查每个接口是否都是好的。我试图通过比较每个接口得到的文件和现有文件来找出这一点 但我知道使用或将使用哪个ip地址。Ansible对每个接口都有一个事实,我可以在其中找到对应的地址。所以我不需要比较文件,我只需要比较地址 我只是更新了用于获取下一个自由接口的任务,以获取要使用的实际接口,该接口可以是下一个自由接口,也可以是已经在使用的接口
- name: find interface to use
set_fact:
interface: "
{%- set ifacePrefix = vars.ansible_default_ipv4.alias -%}
{%- set ifaceNum = { 'cnt': 1 } -%}
{%- macro increment(dct, key, inc=1)-%}
{%- if dct.update({key: dct[key] + inc}) -%}
{%- endif -%}
{%- endmacro -%}
{%- for iface in ansible_interfaces|sort -%}
{%- if ifacePrefix + '_' + ifaceNum.cnt|string in ansible_interfaces -%}
{{ increment(ifaceNum, 'cnt') }}
{%- endif -%}
{%- endfor -%}
{%- for iface in ansible_interfaces|sort -%}
{%- if iface.startswith(ifacePrefix) and ansible_facts[iface]['ipv4']['address'] == ip_failover -%}
{{ ifaceNum.update({'cnt': iface.split('_')[-1]}) }}
{%- endif -%}
{%- endfor -%}
{{ifacePrefix}}:{{ifaceNum.cnt}}"
tags: network
关于信息,第一个For循环是获得第一个自由接口,即使当有人关闭某些接口时,接口数量可能出现差距
- name: find interface to use
set_fact:
interface: "
{%- set ifacePrefix = vars.ansible_default_ipv4.alias -%}
{%- set ifaceNum = { 'cnt': 1 } -%}
{%- macro increment(dct, key, inc=1)-%}
{%- if dct.update({key: dct[key] + inc}) -%}
{%- endif -%}
{%- endmacro -%}
{%- for iface in ansible_interfaces|sort -%}
{%- if ifacePrefix + '_' + ifaceNum.cnt|string in ansible_interfaces -%}
{{ increment(ifaceNum, 'cnt') }}
{%- endif -%}
{%- endfor -%}
{%- for iface in ansible_interfaces|sort -%}
{%- if iface.startswith(ifacePrefix) and ansible_facts[iface]['ipv4']['address'] == ip_failover -%}
{{ ifaceNum.update({'cnt': iface.split('_')[-1]}) }}
{%- endif -%}
{%- endfor -%}
{{ifacePrefix}}:{{ifaceNum.cnt}}"
tags: network