Skip to content

Commit 0e6ef7e

Browse files
authored
Merge pull request #247 from stackhpc/feat/proxy-nameservers
Support configuring nameservers and proxies
2 parents 8cf8ab0 + 56dff7a commit 0e6ef7e

File tree

14 files changed

+170
-7
lines changed

14 files changed

+170
-7
lines changed

ansible/.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ roles/*
3333
!roles/mysql/
3434
!roles/mysql/**
3535
!roles/systemd/
36-
!roles/systemd/**
36+
!roles/systemd/**
37+
!roles/freeipa/
38+
!roles/freeipa/**
39+
!roles/proxy/
40+
!roles/proxy/**
41+
!roles/resolv_conf/
42+
!roles/resolv_conf/**

ansible/bootstrap.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
to update these variable names. ** NB: The actual secrets will not be changed.**
1414
when: "'secrets_openhpc_' in (hostvars[inventory_hostname] | join)"
1515

16+
- hosts: resolv_conf
17+
become: yes
18+
gather_facts: false
19+
tags: resolv_conf
20+
tasks:
21+
- import_role:
22+
name: resolv_conf
23+
1624
- hosts: etc_hosts
1725
gather_facts: false
1826
tags: etc_hosts
@@ -21,6 +29,14 @@
2129
- import_role:
2230
name: etc_hosts
2331

32+
- hosts: proxy
33+
gather_facts: false
34+
tags: proxy
35+
become: yes
36+
tasks:
37+
- import_role:
38+
name: proxy
39+
2440
- hosts: cluster
2541
gather_facts: false
2642
tasks:

ansible/cleanup.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
file:
1414
path: /etc/resolv.conf
1515
state: absent
16+
when: "'resolv_conf' not in group_names" # if its been overriden, deleting it is the wrong thing to do
1617

1718
- name: Reenable NetworkManager control of resolv.conf
19+
# NB: This *doesn't* delete the 90-dns-none.conf file created by the resolv_conf role
20+
# as if nameservers are explicitly being set by that role we don't want to allow NM
21+
# to override it again.
1822
file:
1923
path: /etc/NetworkManager/conf.d/99-cloud-init.conf
2024
state: absent

ansible/roles/proxy/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# proxy
2+
3+
Define http/s proxy configuration.
4+
5+
## Role variables
6+
7+
- `proxy_http_proxy`: Required. Address of http proxy. E.g. "http://10.1.0.28:3128" for a Squid proxy on default port.
8+
- `proxy_https_proxy`: Optional. Address of https proxy. Default is `{{ proxy_http_proxy }}`.
9+
- `proxy_no_proxy`: Optional. Comma-separated list of addresses not to proxy. Default is to concatenate `inventory_hostname` (for hostnames) and `ansible_host` (for host IPs) for all Ansible hosts.
10+
- `proxy_dnf`: Optional bool. Whether to configure yum/dnf proxying through `proxy_http_proxy`. Default `true`.
11+
- `proxy_systemd`: Optional bool. Whether to give processes started by systemd the above http, https and no_proxy configuration. **NB** Running services will need restarting if this is changed. Default `true`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# proxy_http_proxy:
2+
proxy_https_proxy: "{{ proxy_http_proxy }}"
3+
proxy_no_proxy: "{{ (groups['all'] + hostvars.values() | map(attribute='ansible_host')) | sort | join(',') }}"
4+
proxy_dnf: true
5+
proxy_systemd: true

ansible/roles/proxy/tasks/main.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
- name: Define configuration in /etc/environment
2+
tags: proxy
3+
lineinfile:
4+
path: "/etc/environment"
5+
create: yes
6+
owner: root
7+
group: root
8+
mode: o=rw,go=r
9+
state: present
10+
regexp: "{{ item.key }}=.*"
11+
line: "{{ item.key }}={{ item.value }}"
12+
loop:
13+
- key: http_proxy
14+
value: "{{ proxy_http_proxy }}"
15+
- key: https_proxy
16+
value: "{{ proxy_https_proxy }}"
17+
- key: no_proxy
18+
value: "{{ proxy_no_proxy }}"
19+
20+
- name: Define dnf proxy
21+
ini_file:
22+
path: /etc/dnf/dnf.conf
23+
section: main
24+
option: "proxy"
25+
value: "{{ proxy_http_proxy }}"
26+
no_extra_spaces: true
27+
owner: root
28+
group: root
29+
mode: o=rw,go=r
30+
when: proxy_dnf | bool
31+
32+
- name: Create systemd configuration directory
33+
file:
34+
path: /etc/systemd/system.conf.d/
35+
state: directory
36+
owner: root
37+
group: root
38+
mode: ug=rw,o=rX
39+
when: proxy_systemd | bool
40+
41+
- name: Define proxy configuration for systemd units
42+
community.general.ini_file:
43+
path: /etc/systemd/system.conf.d/90-proxy.conf
44+
section: Manager
45+
option: DefaultEnvironment
46+
value: >
47+
"http_proxy={{ proxy_http_proxy }}" "https_proxy={{ proxy_http_proxy }}" "no_proxy={{ proxy_no_proxy }}"
48+
no_extra_spaces: true
49+
owner: root
50+
group: root
51+
mode: ug=rw,o=r
52+
register: _copy_systemd_proxy
53+
when: proxy_systemd | bool
54+
55+
- name: Restart systemd
56+
command: systemctl daemon-reexec
57+
when:
58+
- proxy_systemd | bool
59+
- _copy_systemd_proxy.changed | default(false)
60+
61+
- name: Reset connection to get new /etc/environment
62+
meta: reset_connection
63+
# NB: conditionals not supported
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# resolv_conf
2+
3+
Template out `/etc/resolv.conf`.
4+
5+
## Role variables
6+
- `resolv_conf_nameservers`: List of up to 3 nameserver addresses.
7+
8+
Notes:
9+
- `NetworkManager` (if used) will be prevented from rewriting this file on boot.
10+
- If `/etc/resolv.conf` includes `127.0.0.1` (e.g. due to a FreeIPA server installation), then `resolv_conf_nameservers` is ignored and this role does not change `/etc/resolv.conf`
11+
- For hosts in the `resolv_conf` group, the `/etc/resolv.conf` created with `resolv_conf_nameservers` will
12+
NOT be deleted at the end of Packer image builds.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resolv_conf_nameservers: []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[main]
2+
dns=none
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
- name: Read nameservers from /etc/resolv.conf
2+
ansible.builtin.slurp:
3+
src: /etc/resolv.conf
4+
register: _slurp_resolv_conf
5+
6+
- name: Set nameservers in /etc/resolv.conf
7+
# Might need to set this for freeipa_server host, but freeipa server install
8+
# will then change it to point to 127.0.0.1.
9+
ansible.builtin.template:
10+
src: resolv.conf.j2
11+
dest: /etc/resolv.conf
12+
owner: root
13+
group: root
14+
mode: u=rw,og=r
15+
when: "'127.0.0.1' not in (_slurp_resolv_conf.content | b64decode)"
16+
17+
- name: Disable NetworkManager control of resolv.conf
18+
ansible.builtin.copy:
19+
src: NetworkManager-dns-none.conf
20+
dest: /etc/NetworkManager/conf.d/90-dns-none.conf
21+
owner: root
22+
group: root
23+
mode: u=rw,og=r
24+
register: _copy_nm_config
25+
26+
- name: Reload NetworkManager
27+
ansible.builtin.systemd:
28+
name: NetworkManager
29+
state: reloaded
30+
when: _copy_nm_config.changed | default(false)

0 commit comments

Comments
 (0)