ansible-bootstrap-rocky9/roles/rocky9/tasks/set_nic_names_eth.yml
Alexander Kazantsev 3914590287 lint
2026-05-15 21:20:01 +03:00

95 lines
3.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
- name: Ensure jmespath is installed on the control node
delegate_to: localhost
become: false
ansible.builtin.pip:
name: jmespath
- name: Формируем отсортированный список физических интерфейсов
ansible.builtin.set_fact:
# Сортировка гарантирует порядок: первый в списке станет eth0
physical_interfaces: "{{ ansible_interfaces | json_query('[? !contains(`' + (ignored_interfaces | join(' ')) + '`, @)]') | sort }}"
- name: Сохраняем сетевые настройки первого интерфейса (будущего eth0)
ansible.builtin.set_fact:
first_iface: "{{ physical_interfaces[0] }}"
current_ip: "{{ hostvars[inventory_hostname]['ansible_' + physical_interfaces[0].replace('-', '_')]['ipv4']['address'] }}"
current_mask: "{{ hostvars[inventory_hostname]['ansible_' + physical_interfaces[0].replace('-', '_')]['ipv4']['prefix'] }}"
current_gw: "{{ ansible_default_ipv4.gateway | default(omit) }}"
- name: Debug interface facts
ansible.builtin.debug:
var: hostvars[inventory_hostname]['ansible_' + physical_interfaces[0].replace('-', '_')]['ipv4']
- name: Настройка eth0 с сохранением текущего IP
community.general.nmcli:
conn_name: eth0
ifname: eth0
type: ethernet
mac: "{{ ansible_facts[first_iface]['macaddress'] }}"
ip4: "{{ current_ip }}/{{ current_mask }}"
gw4: "{{ current_gw }}"
state: present
autoconnect: true
#- name: Настройка остальных интерфейсов (eth1, eth2...)
# community.general.nmcli:
# conn_name: eth{{ item.0 + 1 }}
# ifname: eth{{ item.0 + 1 }}
# type: ethernet
# mac: "{{ ansible_facts[item.1]['macaddress'] }}"
# state: present
# autoconnect: true
# Пропускаем первый (индекс 0), так как настроили его выше
# loop: "{{ physical_interfaces[1:] | enumerate }}"
- name: Настройка остальных интерфейсов (eth1, eth2...)
community.general.nmcli:
conn_name: "eth{{ item.0 + 1 }}"
ifname: "eth{{ item.0 + 1 }}"
type: ethernet
mac: "{{ ansible_facts[item.1]['macaddress'] }}"
state: present
autoconnect: true
# Используем срез [1:] и специальный луп для индексации
with_indexed_items: "{{ physical_interfaces[1:] }}"
- name: Применение настроек (поднятие eth0)
ansible.builtin.shell: nmcli device connect eth0 # noqa: command-instead-of-shell ignore-errors
async: 20
poll: 0
ignore_errors: true # noqa: ignore-errors
changed_when: true
- name: Применение настроек для остальных интерфейсов
ansible.builtin.shell: "nmcli device connect eth{{ item.0 + 1 }}"
# item.0 — это индекс (0, 1, 2...)
# item.1 — это имя интерфейса из списка
loop: "{{ physical_interfaces[1:] }}"
# with_indexed_items: "{{ physical_interfaces[1:] }}"
ignore_errors: true
changed_when: false
- name: Перезапуск NetworkManager для чистоты
ansible.builtin.systemd:
name: NetworkManager
state: restarted
async: 30
poll: 0
- name: Create /etc/systemd/network dir
ansible.builtin.file:
path: /etc/systemd/network
state: directory
owner: root
group: root
mode: "0755"
- name: Rename NIC to eth0
ansible.builtin.template:
src: 70-eth0.link.j2
dest: /etc/systemd/network/70-eth{{ item }}.link
owner: root
group: root
mode: "0644"
loop: "{{ physical_interfaces[0:] }}"