需求:由于网段改变,批量修改网段。
思路演进
1、修改地址最开始我们使用 shell 模块,采用 sed 命令进行替换,然而在运行 ansible playbook 的时候有一个警告提示可以选择 lineinfile 模块,但是 lineinfile 模块有一个问题,即
lineinfile 的 regexp 参数使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,而当删除文本时,如果有多行文本都能被匹配,这些行都会被删除。
由于需要进行多行匹配进行替换,所以我们使用 replace 模块
2、网卡可能存在多块,并且名称有可能是不固定的,所以采用 find /etc/sysconfig/network-scripts -name "ifcfg*"
,然后注册成变量进行循环执行修改
3、Centos8 采用nmcli
进行网卡管理,重启网卡需要执行nmcli con reload
和 nmcli con up con-name
两步,由于不知道修改了那块网卡,所以我们需要把网卡名称定义(注册)成变量,这样使用 nmcli con up con-name
的时候才不会有所遗漏
4、在修改完网卡重启的时候,由于重启网卡导致 ssh 连接断开,ansible 会产生阻塞,所以采用了异步处理机制,增加了async和poll实现
5、注意,要将Centos8
的网卡重启放到Ubuntu
和Centos7
之前,否则,即是做了异步处理机制,也会出现阻塞
[root@centos8 ansible]#cat change_network.yml
---
- hosts: all
remote_user: root
vars:
old_ip: "172.17"
new_ip: "172.18"
tasks:
- name: find centos8 network name
shell: find /etc/sysconfig/network-scripts -name "ifcfg*"
register: centos8_network_name
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "8"
- name: replace centos8 network
replace:
path: "{{ item }}"
regexp: "{{ old_ip }}"
replace: "{{ new_ip }}"
with_items: "{{ centos8_network_name.stdout_lines }}"
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "8"
- name: centos8 interface
setup: filter=ansible_interfaces
register: centos8_interface
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "8"
- name: find centos7 network name
shell: find /etc/sysconfig/network-scripts -name "ifcfg*"
register: centos7_network_name
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "7"
- name: replace centos7 netowork
replace:
path: "{{ item }}"
regexp: "{{ old_ip }}"
replace: "{{ new_ip }}"
with_items: "{{ centos7_network_name.stdout_lines }}"
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "7"
- name: find ubuntu network name
shell: find /etc/netplan/ -name "*.yaml"
register: ubuntu_network_name
when: ansible_facts['distribution'] == "Ubuntu"
#- name: show variable
# debug: msg="{{ network_name }}"
- name: replace ubuntu network
replace:
path: "{{ item }}"
regexp: "{{ old_ip }}"
replace: "{{ new_ip }}"
with_items: "{{ ubuntu_network_name.stdout_lines }}"
when: ansible_facts['distribution'] == "Ubuntu"
- name: restart centos8 network
shell:
nmcli connection reload && nmcli connection up "{{ item }}"
with_items: "{{ centos8_interface.ansible_facts.ansible_interfaces }}"
ignore_errors: True
async: 1
poll: 0
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "8"
- name: restart ubuntu netowrk
shell: netplan apply
ignore_errors: True
async: 1
poll: 0
when: ansible_facts['distribution'] == "Ubuntu"
- name: restart centos7 network
shell: systemctl restart network
ignore_errors: True
async: 1
poll: 0
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "7"
- name: wait for connection
wait_for_connection:
timeout: 0
ignore_errors: True
[root@centos8 ansible]#
执行playbook
[root@centos8 ansible]#ansible-playbook change_network.yml