forked from DavesCodeMusings/CloudPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure-static-network-params.yml
63 lines (55 loc) · 1.85 KB
/
configure-static-network-params.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
- name: Configure Static IP
hosts: localhost
connection: local
become: true
# To customize variable values, use --extra-vars when running to override.
# For example:
# ansible-playbook --extra-vars "ip=192.168.1.100 hostname=mypi"
vars:
ip: "{{ ansible_default_ipv4.address }}"
netmask: "{{ ansible_default_ipv4.netmask }}"
gateway: "{{ ansible_default_ipv4.gateway }}"
primary_dns: "{{ ansible_dns.nameservers[0] }}"
secondary_dns: 1.1.1.1
hostname: "{{ ansible_hostname }}"
domain: home
tasks:
- name: Checking IP address
debug:
msg: "The IP address hasn't changed. (Still using: {{ ip }}) Are you sure you don't want to customize using --extra-vars?"
when: ip == ansible_default_ipv4.address
- name: Checking Hostname
debug:
msg: "The hostname hasn't changed. (Still using: {{ hostname }}) Are you sure you don't want to customize using --extra-vars?"
when: hostname == ansible_hostname
- name: Configuring network interface parameters
ansible.builtin.copy:
dest: /etc/network/interfaces.d/eth0
content: |
auto eth0
allow-hotplug eth0
iface eth0 inet static
address {{ ip }}
netmask {{ netmask }}
gateway {{ gateway }}
dns-nameservers {{ primary_dns }} {{ secondary_dns }}
dns-search {{ domain }}
- name: Disabling DHCP
ansible.builtin.service:
name: dhcpcd
state: stopped
enabled: no
- name: Setting the hostname
ansible.builtin.hostname:
name: "{{ hostname }}.{{ domain }}"
- name: Creating /etc/hosts
ansible.builtin.copy:
dest: /etc/hosts
backup: yes
content: |
127.0.0.1 localhost
{{ ip }} {{ hostname }}.{{ domain }} {{ hostname }}
- name: Reboot to active changes
debug:
msg: You must reboot for changes to take effect.