Ansible regex替换Ubuntu网络计划文件中的DNS地址



我想在Ubuntu网络计划yaml文件中将DNS IP地址从192.168.86.14更改为192.168.86.16:

link: ens3
addresses: [192.168.86.12/24]
gateway4: 192.168.86.1
nameservers:
addresses: [192.168.86.14,8.8.8.8,8.8.4.4]

这是我可靠的战术手册:

- name: test
ansible.builtin.replace:
path: /etc/netplan/00-installer-config.yaml
regexp: '(addresses: [)+192.168.86.14,'
replace: '1192.168.86.16,'

我的剧本没有改变文件中的任何内容。尝试转义逗号,但与任何内容都不匹配。

出于某种原因,我需要确保IP地址在";地址["one_answers","所以我不能只使用这样的语法:

- name: test
ansible.builtin.replace:
path: /etc/netplan/00-installer-config.yaml
regexp: '192.168.86.14'
replace: '192.168.86.16'

我是安西布尔的新手,任何帮助都将不胜感激!

字典在YAML中是不可变的。但是,您可以在Jinja2中更新词典。让我们举一个完整的网络计划配置文件示例,例如

shell> cat 00-installer-config.yaml
network:
version: 2
renderer: networkd
ethernets:
ens3:
mtu: 9000
enp3s0:
link: ens3
addresses: [192.168.86.12/24]
gateway4: 192.168.86.1
nameservers:
addresses: [192.168.86.14,8.8.8.8,8.8.4.4]

将字典读入变量

- include_vars:
file: 00-installer-config.yaml
name: netplan_conf

给出

netplan_conf:
network:
ethernets:
enp3s0:
addresses:
- 192.168.86.12/24
gateway4: 192.168.86.1
link: ens3
nameservers:
addresses:
- 192.168.86.14
- 8.8.8.8
- 8.8.4.4
ens3:
mtu: 9000
renderer: networkd
version: 2

创建一个更新名称服务器的模板

shell> cat 00-installer-config.yaml.j2
{% set _dummy = netplan_conf.network.ethernets.enp3s0.nameservers.update({'addresses': _addresses}) %}
{{ netplan_conf|to_nice_yaml }}

下方的任务

- template:
src: 00-installer-config.yaml.j2
dest: 00-installer-config.yaml
vars:
_addresses: "{{ netplan_conf.network.ethernets.enp3s0.nameservers.addresses|
regex_replace('192.168.86.14', '192.168.86.16') }}"

将更新配置文件

shell> cat 00-installer-config.yaml
network:
ethernets:
enp3s0:
addresses:
- 192.168.86.12/24
gateway4: 192.168.86.1
link: ens3
nameservers:
addresses:
- 192.168.86.16
- 8.8.8.8
- 8.8.4.4
ens3:
mtu: 9000
renderer: networkd
version: 2

相关内容

  • 没有找到相关文章

最新更新