added static ip playbook
This commit is contained in:
15
static-ip.yml
Normal file
15
static-ip.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
---
|
||||
- hosts: all
|
||||
become: true
|
||||
tasks:
|
||||
- name: Import static IP variables
|
||||
ansible.builtin.include_vars:
|
||||
file: static-ip.yml
|
||||
- import_tasks: tasks/debian-general/static-ip.yml
|
||||
when: ansible_facts["distribution"] == "Debian"
|
||||
- import_tasks: tasks/debian-general/restart-networking.yml
|
||||
when: ansible_facts["distribution"] == "Debian"
|
||||
- import_tasks: tasks/rhel-general/static-ip.yml
|
||||
when: ansible_facts["distribution"] == "RedHat"
|
||||
- import_tasks: tasks/rhel-general/restart-networking.yml
|
||||
when: ansible_facts["distribution"] == "RedHat"
|
||||
8
tasks/debian-general/restart-networking.yml
Normal file
8
tasks/debian-general/restart-networking.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
- name: (debian) Restart networking to apply new configuration
|
||||
ansible.builtin.systemd:
|
||||
name: networking
|
||||
state: restarted
|
||||
async: 5
|
||||
poll: 0
|
||||
ignore_errors: true
|
||||
33
tasks/debian-general/static-ip.yml
Normal file
33
tasks/debian-general/static-ip.yml
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
- name: (debian) Check if /etc/network/interfaces exists
|
||||
ansible.builtin.stat:
|
||||
path: /etc/network/interfaces
|
||||
register: _debian_interfaces_file
|
||||
|
||||
- name: (debian) Check if interface is already managed by ansible
|
||||
ansible.builtin.command:
|
||||
cmd: "grep -qF '# BEGIN ANSIBLE MANAGED {{ INTERFACE_NAME }}' /etc/network/interfaces"
|
||||
register: _debian_ansible_managed
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
when: _debian_interfaces_file.stat.exists
|
||||
|
||||
- name: (debian) Remove old unmanaged interface stanza
|
||||
ansible.builtin.replace:
|
||||
path: /etc/network/interfaces
|
||||
regexp: '(?m)^auto {{ INTERFACE_NAME }}\s*\niface {{ INTERFACE_NAME }} inet (dhcp|static)\s*\n(\s+.*\n)*'
|
||||
replace: ''
|
||||
when:
|
||||
- _debian_interfaces_file.stat.exists
|
||||
- _debian_ansible_managed.rc | default(1) != 0
|
||||
|
||||
- name: (debian) Configure static IP block for interface
|
||||
ansible.builtin.blockinfile:
|
||||
path: /etc/network/interfaces
|
||||
marker: "# {mark} ANSIBLE MANAGED {{ INTERFACE_NAME }}"
|
||||
block: |
|
||||
auto {{ INTERFACE_NAME }}
|
||||
iface {{ INTERFACE_NAME }} inet static
|
||||
address {{ STATIC_IP }}
|
||||
gateway {{ STATIC_GATEWAY }}
|
||||
state: present
|
||||
8
tasks/rhel-general/restart-networking.yml
Normal file
8
tasks/rhel-general/restart-networking.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
- name: (rhel) Restart NetworkManager to apply new configuration
|
||||
ansible.builtin.systemd:
|
||||
name: NetworkManager
|
||||
state: restarted
|
||||
async: 5
|
||||
poll: 0
|
||||
ignore_errors: true
|
||||
71
tasks/rhel-general/static-ip.yml
Normal file
71
tasks/rhel-general/static-ip.yml
Normal file
@@ -0,0 +1,71 @@
|
||||
---
|
||||
- name: (rhel) Check if ifcfg file exists for interface
|
||||
ansible.builtin.stat:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
register: _rhel_ifcfg_file
|
||||
|
||||
- name: (rhel) Create ifcfg file with static configuration
|
||||
ansible.builtin.copy:
|
||||
dest: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
content: |
|
||||
TYPE=Ethernet
|
||||
BOOTPROTO=static
|
||||
NAME={{ INTERFACE_NAME }}
|
||||
DEVICE={{ INTERFACE_NAME }}
|
||||
ONBOOT=yes
|
||||
IPADDR={{ STATIC_IP | regex_replace('/.*', '') }}
|
||||
PREFIX={{ STATIC_IP | regex_replace('.*/', '') }}
|
||||
GATEWAY={{ STATIC_GATEWAY }}
|
||||
DNS1={{ STATIC_DNS1 }}
|
||||
DNS2={{ STATIC_DNS2 }}
|
||||
mode: "0644"
|
||||
when: not _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Ensure BOOTPROTO is set to static
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^BOOTPROTO="
|
||||
line: "BOOTPROTO=static"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Ensure ONBOOT is set to yes
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^ONBOOT="
|
||||
line: "ONBOOT=yes"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Set IPADDR
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^IPADDR="
|
||||
line: "IPADDR={{ STATIC_IP | regex_replace('/.*', '') }}"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Set PREFIX
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^PREFIX="
|
||||
line: "PREFIX={{ STATIC_IP | regex_replace('.*/', '') }}"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Set GATEWAY
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^GATEWAY="
|
||||
line: "GATEWAY={{ STATIC_GATEWAY }}"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Set DNS1
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^DNS1="
|
||||
line: "DNS1={{ STATIC_DNS1 }}"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
|
||||
- name: (rhel) Set DNS2
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysconfig/network-scripts/ifcfg-{{ INTERFACE_NAME }}"
|
||||
regexp: "^DNS2="
|
||||
line: "DNS2={{ STATIC_DNS2 }}"
|
||||
when: _rhel_ifcfg_file.stat.exists
|
||||
6
vars/static-ip.yml
Normal file
6
vars/static-ip.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
INTERFACE_NAME: eth0
|
||||
STATIC_IP: 192.168.1.10/24
|
||||
STATIC_GATEWAY: 192.168.1.1
|
||||
STATIC_DNS1: 9.9.9.9
|
||||
STATIC_DNS2: 149.112.112.112
|
||||
Reference in New Issue
Block a user