Skip to content

Commit 2b4dd13

Browse files
m-brandoMatus BrandysCI/CD pipeline
authored
Feat/ansible perf (#1917)
This PR addresses issue #1831. Improvements: - Removed unnecessary fact gathering. - Replaced loops with blockinfile where applicable. - Introduced `async` with `poll: 0` to run non-blocking tasks, combined with later state checks using `async_status`. - Reorganized tasks and play structure for more efficient execution. overall speedup of execution is ~20% The WireGuard playbook remains the slowest part of the workflow. It could be parallelized further, but doing so would require synchronizing host facts, particularly the public key values used when rendering the WireGuard template in this task: ``` - name: Template wireguard config file to a node ansible.builtin.template: src: "{{ 'wg-static.conf.j2' if 'static' in group_names else 'wg-dynamic.conf.j2' }}" dest: "{{ wg_conf_dir.path }}/wg0.conf" ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Performance Improvements** * More background package operations and reduced fact gathering to speed playbook runs. * **Stability & Reliability** * Consolidated kernel, sysctl and systemd limits with a single reload for consistent node limits. * Centralized proxy environment management via block updates and standardized drop-in dirs. * Safer WireGuard key handling and idempotent interface/IP management; unattended-upgrades now masked. * **Refactor** * Merged and simplified related tasks for cleaner, more consistent playbooks. * **Chores** * Updated ansibler image tag. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Matus Brandys <matus.brandys@berops.com> Co-authored-by: CI/CD pipeline <CI/CD-pipeline@users.noreply.github.com>
1 parent 1f2cb57 commit 2b4dd13

14 files changed

Lines changed: 123 additions & 194 deletions

File tree

manifests/claudie/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
5656
kind: Kustomization
5757
images:
5858
- name: ghcr.io/berops/claudie/ansibler
59-
newTag: 7430672-3800
59+
newTag: 24bd046-3775
6060
- name: ghcr.io/berops/claudie/autoscaler-adapter
6161
newTag: 193717f-3802
6262
- name: ghcr.io/berops/claudie/builder
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
- hosts: all
3-
gather_facts: no
3+
gather_facts: false
44
become: yes
55
vars:
66
# for node-limits tasks
@@ -12,33 +12,18 @@
1212
max_queued_events: 65535
1313
# Install longhorn dependencies
1414
tasks:
15-
- name: install open-iscsi
15+
- name: install open-iscsi and nfs-common
1616
ansible.builtin.apt:
17-
name: open-iscsi
17+
name:
18+
- open-iscsi
19+
- nfs-common
1820
state: present
1921
update_cache: true
2022
retries: 2
2123
delay: 10
22-
register: res
23-
until: res is not failed
24-
# open-iscsi packages are few hundred Kibs in size
25-
# having a timeout of 8 mins with 2 retries is generous.
24+
register: async_job
2625
async: 480
27-
poll: 5
28-
29-
- name: install nfs-common
30-
ansible.builtin.apt:
31-
name: nfs-common
32-
state: present
33-
update_cache: true
34-
retries: 2
35-
delay: 10
36-
register: res
37-
until: res is not failed
38-
# nfs-common packages are few hundred Kibs in size
39-
# having a timeout of 8 mins with 2 retries generous.
40-
async: 480
41-
poll: 5
26+
poll: 0
4227

4328
- name: Update /etc/multipath.conf with blacklist configuration
4429
blockinfile:
@@ -61,3 +46,11 @@
6146

6247
- name: update node limits
6348
include_tasks: ./node-limits.yml
49+
50+
- name: Check async task result
51+
async_status:
52+
jid: "{{ async_job.ansible_job_id }}"
53+
register: result
54+
until: result.finished
55+
retries: 60
56+
delay: 5

services/ansibler/server/ansible-playbooks/node-limits.yml

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,44 @@
1010
root hard nofile {{ nofile_hard }}
1111
state: present
1212

13-
- name: Ensure PAM limits are enabled
14-
lineinfile:
15-
path: /etc/pam.d/common-session
16-
line: "session required pam_limits.so"
17-
state: present
18-
19-
- name: Ensure PAM limits are enabled for non-interactive sessions
20-
lineinfile:
21-
path: /etc/pam.d/common-session-noninteractive
13+
- name: Enable PAM limits
14+
ansible.builtin.lineinfile:
15+
path: "{{ item }}"
2216
line: "session required pam_limits.so"
2317
state: present
18+
loop:
19+
- /etc/pam.d/common-session
20+
- /etc/pam.d/common-session-noninteractive
2421

2522
- name: Configure systemd system limits
2623
ansible.builtin.lineinfile:
27-
path: /etc/systemd/system.conf
28-
regexp: "^#?DefaultLimitNOFILE="
29-
line: "DefaultLimitNOFILE={{ nofile_hard }}"
30-
31-
- name: Configure systemd user limits
32-
ansible.builtin.lineinfile:
33-
path: /etc/systemd/user.conf
24+
path: "{{ item }}"
3425
regexp: "^#?DefaultLimitNOFILE="
3526
line: "DefaultLimitNOFILE={{ nofile_hard }}"
27+
loop:
28+
- /etc/systemd/system.conf
29+
- /etc/systemd/user.conf
3630

3731
- name: Reload systemd configuration
3832
ansible.builtin.systemd:
3933
daemon_reload: yes
4034

41-
- name: Set kernel file handle maximum
42-
ansible.builtin.sysctl:
43-
name: fs.file-max
44-
value: "{{ file_max }}"
45-
state: present
46-
47-
- name: Set kernel user queued events maximum
48-
ansible.builtin.sysctl:
49-
name: fs.inotify.max_queued_events
50-
value: "{{ max_queued_events }}"
51-
state: present
52-
53-
- name: Set kernel user watches maximum
54-
ansible.builtin.sysctl:
55-
name: fs.inotify.max_user_watches
56-
value: "{{ max_user_watches }}"
57-
state: present
58-
59-
- name: Set kernel user instances maximum
60-
ansible.builtin.sysctl:
61-
name: fs.inotify.max_user_instances
62-
value: "{{ max_user_instances }}"
63-
state: present
64-
reload: yes
65-
66-
- name: Set Ephemeral port range
67-
ansible.builtin.sysctl:
68-
name: net.ipv4.ip_local_port_range
69-
value: "2048 65535"
70-
state: present
71-
reload: yes
72-
73-
- name: Set somaxconn to 65535
74-
ansible.builtin.sysctl:
75-
name: net.core.somaxconn
76-
value: "65535"
77-
state: present
78-
reload: yes
79-
80-
- name: Set TCP fin timeout to 30s
81-
ansible.builtin.sysctl:
82-
name: net.ipv4.tcp_fin_timeout
83-
value: "30"
84-
state: present
85-
reload: yes
35+
- name: Configure kernel parameters
36+
ansible.builtin.blockinfile:
37+
path: /etc/sysctl.d/99-claudie-custom.conf
38+
create: yes
39+
owner: root
40+
group: root
41+
mode: '0644'
42+
block: |
43+
fs.file-max = {{ file_max }}
44+
fs.inotify.max_queued_events = {{ max_queued_events }}
45+
fs.inotify.max_user_watches = {{ max_user_watches }}
46+
fs.inotify.max_user_instances = {{ max_user_instances }}
47+
net.ipv4.ip_local_port_range = 2048 65535
48+
net.core.somaxconn = 65535
49+
net.ipv4.tcp_fin_timeout = 30
50+
51+
52+
- name: Reload sysctl at once with new settings
53+
ansible.builtin.command: sysctl --system

services/ansibler/server/ansible-playbooks/proxy/commit-proxy-envs-changes.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
crictl pods | grep kube-scheduler | awk '{print $1}' | xargs -I {} sh -c 'crictl stopp {} && crictl rmp {}'
4545
4646
- hosts: all
47-
gather_facts: true
47+
gather_facts: false
4848
become: true
4949
tasks:
5050
# This will reload the systemd unit files present under /etc/systemd/system/.
@@ -69,7 +69,7 @@
6969
loop: "{{ service_status.results }}"
7070

7171
- hosts: control
72-
gather_facts: true
72+
gather_facts: false
7373
become: true
7474
tasks:
7575
- name: Try 10 times to check kube-apiserver health
Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
- hosts: all
2+
strategy: free
23
gather_facts: false
34
become: true
45
tasks:
5-
- name: Verifying if /etc/apt/apt.conf.d/ exists
6+
- name: Ensure apt and systemd drop-in directories exist
67
ansible.builtin.file:
7-
path: /etc/apt/apt.conf.d/
8-
state: directory
9-
- name: Verifying if directories in /etc/systemd/system for containerd,kubelet,docker exists
10-
ansible.builtin.file:
11-
path: "/etc/systemd/system/{{ item }}.service.d/"
8+
path: "{{ item }}"
129
state: directory
10+
mode: "0755"
1311
loop:
14-
- docker
15-
- containerd
16-
- kubelet
12+
- /etc/systemd/system/docker.service.d
13+
- /etc/systemd/system/containerd.service.d
14+
- /etc/systemd/system/kubelet.service.d
15+
1716
- name: Setting HTTP,HTTPS proxy settings in /etc/apt/apt.conf.d/proxy.conf
1817
ansible.builtin.copy:
1918
content: |
2019
Acquire::https::Proxy "{{ http_proxy_url }}";
2120
Acquire::http::Proxy "{{ http_proxy_url }}";
2221
dest: "/etc/apt/apt.conf.d/proxy.conf"
22+
2323
- name: Setting HTTP,HTTPS proxy settings in /etc/systemd/system/
2424
ansible.builtin.copy:
2525
content: |
@@ -31,28 +31,15 @@
3131
- containerd
3232
- docker
3333

34-
- name: Delete HTTP,HTTPS,NO_PROXY from /etc/environmennt
35-
ansible.builtin.lineinfile:
34+
- name: Ensure proxy variables in /etc/environment
35+
ansible.builtin.blockinfile:
3636
path: /etc/environment
37-
regexp: "^{{ item.key }}=.*$"
38-
state: absent
39-
loop:
40-
- { key: "HTTP_PROXY" }
41-
- { key: "http_proxy" }
42-
- { key: "HTTPS_PROXY" }
43-
- { key: "https_proxy" }
44-
- { key: "NO_PROXY" }
45-
- { key: "no_proxy" }
46-
47-
- name: Add HTTP,HTTPS,NO_PROXY into /etc/environmennt
48-
ansible.builtin.lineinfile:
49-
path: /etc/environment
50-
regexp: "^{{ item.key }}=.*$"
51-
line: "{{ item.key }}={{ item.value }}"
52-
loop:
53-
- { key: "HTTP_PROXY", value: "{{ http_proxy_url }}" }
54-
- { key: "http_proxy", value: "{{ http_proxy_url }}" }
55-
- { key: "HTTPS_PROXY", value: "{{ http_proxy_url }}" }
56-
- { key: "https_proxy", value: "{{ http_proxy_url }}" }
57-
- { key: "NO_PROXY", value: "{{ no_proxy_list }}" }
58-
- { key: "no_proxy", value: "{{ no_proxy_list }}" }
37+
create: yes
38+
marker: "# {mark} ANSIBLE MANAGED PROXY SETTINGS"
39+
block: |
40+
HTTP_PROXY={{ http_proxy_url }}
41+
http_proxy={{ http_proxy_url }}
42+
HTTPS_PROXY={{ http_proxy_url }}
43+
https_proxy={{ http_proxy_url }}
44+
NO_PROXY={{ no_proxy_list }}
45+
no_proxy={{ no_proxy_list }}

services/ansibler/server/ansible-playbooks/proxy/remove-proxy-envs.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@
1414
- docker
1515
- containerd
1616
- kubelet
17-
- name: Delete HTTP,HTTPS,NO_PROXY from /etc/environmennt
18-
ansible.builtin.lineinfile:
17+
18+
- name: Remove proxy variables block from /etc/environment
19+
ansible.builtin.blockinfile:
1920
path: /etc/environment
20-
regexp: "^{{ item.key }}=.*$"
21+
marker: "# {mark} ANSIBLE MANAGED PROXY SETTINGS"
2122
state: absent
22-
loop:
23-
- { key: "HTTP_PROXY" }
24-
- { key: "http_proxy" }
25-
- { key: "HTTPS_PROXY" }
26-
- { key: "https_proxy" }
27-
- { key: "NO_PROXY" }
28-
- { key: "no_proxy" }

services/ansibler/server/ansible-playbooks/wireguard.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
wait_for_connection:
99
timeout: 100
1010

11-
# Gather facts manually after we made sure, VMs are accessible
12-
- name: Gather facts from nodes
13-
ansible.builtin.setup:
14-
1511
# abort playbook on any fatal error, the golang code will trigger a retry
1612
any_errors_fatal: true
1713
roles:

services/ansibler/server/ansible-playbooks/wireguard/tasks/configure.yml

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,42 @@
55
state: directory
66
register: wg_conf_dir
77

8-
- name: Generate wireguard keypair
9-
ansible.builtin.shell: umask 077 && wg genkey | tee {{ wg_conf_dir.path }}/privatekey | wg pubkey > {{ wg_conf_dir.path }}/publickey
8+
- name: Generate WireGuard keypair if not existing
9+
ansible.builtin.shell: |
10+
umask 077 && wg genkey | tee {{ wg_conf_dir.path }}/privatekey | wg pubkey > {{ wg_conf_dir.path }}/publickey
11+
args:
12+
creates: "{{ wg_conf_dir.path }}/privatekey"
1013

11-
- name: Save privatekey
12-
ansible.builtin.shell: cat {{ wg_conf_dir.path }}/privatekey
14+
- name: Read private key
15+
ansible.builtin.slurp:
16+
src: "{{ wg_conf_dir.path }}/privatekey"
1317
register: privatekey
1418

15-
- name: Save publickey
16-
ansible.builtin.shell: cat {{ wg_conf_dir.path }}/publickey
19+
- name: Read public key
20+
ansible.builtin.slurp:
21+
src: "{{ wg_conf_dir.path }}/publickey"
1722
register: publickey
1823

1924
- name: Check if wireguard interface exists
20-
ansible.builtin.shell: ip a
21-
register: ipa_contents
25+
ansible.builtin.stat:
26+
path: /sys/class/net/wg0
27+
register: wg0_stat
2228

2329
- name: Create wireguard interface
24-
ansible.builtin.shell: ip link add dev wg0 type wireguard
25-
when: ipa_contents.stdout.find('wg0') == -1
30+
ansible.builtin.command:
31+
cmd: ip link add dev wg0 type wireguard
32+
when: not wg0_stat.stat.exists
2633

27-
- name: Check if wireguard interface has IP address
28-
ansible.builtin.shell: ip a
29-
register: ipa_contents
34+
- name: Check if interface already has the IP
35+
ansible.builtin.command:
36+
cmd: ip -o addr show dev wg0
37+
register: wg0_ip_info
38+
changed_when: false
3039

31-
- name: Configure an IP address for an interface
32-
ansible.builtin.shell: "ip address add dev wg0 {{ private_ip }}/{{ netmask }}"
33-
when: "ipa_contents.stdout.find( private_ip ) == -1"
40+
- name: Assign IP address
41+
ansible.builtin.command:
42+
cmd: ip address add dev wg0 {{ private_ip }}/{{ netmask }}
43+
when: private_ip not in wg0_ip_info.stdout
3444

3545
- name: Template wireguard config file to a node
3646
ansible.builtin.template:
@@ -46,18 +56,13 @@
4656
ansible.builtin.shell: ip link set up dev wg0
4757

4858
- name: Set MTU size for wg0
49-
ansible.builtin.shell: ifconfig {{ wg_interface_name }} mtu {{ mtu_size }}
50-
51-
- name: Ensure file /etc/dhcp/dhclient.conf exists
52-
ansible.builtin.copy:
53-
content: ""
54-
dest: /etc/dhcp/dhclient.conf
55-
force: false
59+
ansible.builtin.shell: ip link set mtu {{ mtu_size }} dev {{ wg_interface_name }}
5660

57-
- name: Make MTU change persistent across reboots
61+
- name: Ensure file /etc/dhcp/dhclient.conf exists and make MTU change persistent across reboots
5862
ansible.builtin.blockinfile:
5963
path: /etc/dhcp/dhclient.conf
60-
insertbefore: "request"
64+
create: yes
65+
insertbefore: "^request"
6166
block: |
6267
interface "{{ wg_interface_name }}" {
6368
default interface-mtu {{ mtu_size }};

0 commit comments

Comments
 (0)