【问题标题】:How to change the name of the network interface with ansible?如何用ansible更改网络接口的名称?
【发布时间】:2019-06-19 09:50:22
【问题描述】:

我需要使用 ansible 更改网络接口的名称。

例如。 mv /etc/sysconfig/network-scripts/ifcfg-eno1675454 /etc/sysconfig/network-scripts/ifcfg-eth0

nmtui 并更改个人资料名称

我目前的做法 -

 - name: Copy interface file ifcfg-{{ ansible_default_ipv4.interface }} to ifcfg-etho
  copy: 
    remote_src: True 
    src: '/etc/sysconfig/network-scripts/ifcfg-{{ ansible_default_ipv4.interface }}'
    dest: /etc/sysconfig/network-scripts/ifcfg-eth0

- name: Replace device name to eth0 inside the file
  replace:
    path: '/etc/sysconfig/network-scripts/ifcfg-eth0'
    regexp:  "{{ ansible_default_ipv4.interface }}"
    replace: "eth0"

修改 grub:

- name: update grub 
  lineinfile:
    dest: "{{ grub_input_config }}"
    regexp: "GRUB_CMDLINE_LINUX"
    line: 'GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet net.ifnames=0 biosdevname=0" '
  notify: "make grub config"

grub 处理程序:

---

- name: make grub config
  command: "{{ grub_mkconfig_cmd }} -o {{ grub_output_config }}"

默认值


grub_mkconfig_cmd: grub2-mkconfig
grub_output_config: /boot/grub2/grub.cfg
grub_input_config: /etc/default/grub

编辑:添加代码

【问题讨论】:

  • 嗨@willing_to_learn,欢迎来到SO(顺便说一句,爱你的昵称:))。到目前为止,您尝试了什么,什么没有按预期工作?请花一些时间浏览帮助部分,更具体地说是How do I ask a good question 和 How to create a minimal reproducible example
  • 嗨@Zeitounator。我做了以下事情:创建了一个任务,将当前接口文件复制到 eth0 并最终替换该文件中的接口名称。
  • 我不确定这是否是正确的做法?

标签: ansible centos7 network-interface


【解决方案1】:

当您运行这个 ansible 任务并重新启动系统时,更改会保留吗? 在 centos7 中,我必须执行此额外步骤以使其永久化:

  • 编辑文件 /etc/default/grub 并将 net.ifnames=0 biosdevname=0 添加到 GRUB_CMDLINE_LINUX 行:
GRUB_CMDLINE_LINUX="... net.ifnames=0 biosdevname=0 ..."
  • 重新生成一个 GRUB 配置文件并覆盖现有的:
grub2-mkconfig -o /boot/grub2/grub.cfg

【讨论】:

  • HI Diogo,我也对 GRUB 文件进行了更改。我忘了添加那部分代码。我已经添加了我使用的代码。
猜你喜欢
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
相关资源
最近更新 更多