Skip to content

Commit ec2b7cf

Browse files
committed
feat: Add dynamic config
1 parent cfc23e8 commit ec2b7cf

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

defaults/main/agent.yml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
# Requires access to either the NGINX stub_status or the NGINX Plus REST API.
44
nginx_agent_enable: false
55

6+
7+
########################################################################################################################
8+
# The following parameters let you configure the static configuration file of NGINX Agent. #
9+
# By default, the config produced is as close a match to the default config provided by NGINX Agent upon installation. #
10+
########################################################################################################################
11+
612
# Configure the NGINX Agent.
713
nginx_agent_configure: false
814

9-
#######################################################################################################################
10-
# The following parameters let you configure NGINX Agent. #
11-
# By default, the config produced is as close a match to the default config provided by NGINX Agent upon installation.#
12-
#######################################################################################################################
13-
1415
# Specify the NGINX Agent API host and port. Optionally, specify the path to the cert and key.
1516
# Default is not enabled.
1617
# nginx_agent_api:
@@ -84,3 +85,23 @@ nginx_agent_metrics:
8485
# nginx_agent_app_protect:
8586
# report_interval: 15s
8687
# precompiled_publication: true
88+
89+
90+
#############################################################################################
91+
# The following parameters let you configure the dynamic configuration file of NGINX Agent. #
92+
# By default, nothing is configured. #
93+
#############################################################################################
94+
95+
# Configure the NGINX Agent dynamic configuration file.
96+
# NOTE: This will only run if the NGINX Agent dynamic configuration file has not yet been modified externally.
97+
# If you want to force push a dynamic configuration file, use the 'nginx_agent_configure_dynamic_force' parameter below.
98+
# Default is false.
99+
nginx_agent_configure_dynamic: false
100+
# Force pushing a new dynamic configuration file to NGINX Agent.
101+
# NOTE: This will overwrite any changes made to the dynamic configuration file by a control plane, and might lead to unexpected behavior.
102+
# Default is false.
103+
nginx_agent_configure_dynamic_force: false
104+
105+
# Specify the NGINX Agent instance group and tags.
106+
# nginx_agent_instance_group: my_instance_group
107+
# nginx_agent_tags: [ansible, dev, qa]

molecule/agent/converge.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@
2525
nginx_agent_api:
2626
host: 127.0.0.1
2727
port: 8081
28+
nginx_agent_configure_dynamic: true
29+
nginx_agent_instance_group: ansible_instance_group
30+
nginx_agent_tags: ['ansible', 'dev']

tasks/agent/install-agent.yml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
---
2+
- name: (Alpine Linux/Debian/SLES OSs) Install NGINX Agent nstat prerequisite
3+
ansible.builtin.package:
4+
name: iproute2
5+
state: present
6+
when: ansible_facts['os_family'] in ['Alpine', 'Debian', 'Suse']
7+
8+
- name: (Red Hat OSs) Install NGINX Agent nstat prerequisite
9+
ansible.builtin.package:
10+
name: iproute
11+
state: present
12+
when: ansible_facts['os_family'] == "RedHat"
13+
214
- name: Manage NGINX Agent repository
315
ansible.builtin.include_tasks: "{{ role_path }}/tasks/agent/setup-{{ ansible_facts['os_family'] | lower }}.yml"
416
when: ansible_facts['os_family'] in ['Alpine', 'Debian', 'RedHat', 'Suse']
@@ -8,11 +20,34 @@
820
name: nginx-agent
921
state: present
1022

11-
- name: Configure NGINX Agent
23+
- name: Dynamically generate NGINX Agent static configuration file
1224
ansible.builtin.template:
1325
src: nginx-agent/nginx-agent.conf.j2
1426
dest: /etc/nginx-agent/nginx-agent.conf
1527
mode: "0644"
1628
backup: true
1729
when: nginx_agent_configure | bool
1830
notify: (Handler) Start NGINX Agent
31+
32+
- name: Check if the NGINX Agent dynamic configuration file has been modified
33+
ansible.builtin.lineinfile:
34+
path: /var/lib/nginx-agent/agent-dynamic.conf
35+
line: '# agent-dynamic.conf'
36+
state: present
37+
check_mode: true
38+
changed_when: false
39+
when:
40+
- nginx_agent_configure_dynamic | bool
41+
- not nginx_agent_configure_dynamic_force | bool
42+
register: default_conf
43+
44+
- name: Dynamically generate NGINX Agent dynamic configuration file if it has not been externally modified
45+
ansible.builtin.template:
46+
src: nginx-agent/agent-dynamic.conf.j2
47+
dest: "{{ (ansible_facts['system'] | lower is not search('bsd')) | ternary('/var/lib/nginx-agent/agent-dynamic.conf', '/var/db/nginx-agent/agent-dynamic.conf') }}"
48+
mode: "0644"
49+
backup: true
50+
when:
51+
- nginx_agent_configure_dynamic | bool
52+
- (default_conf['msg'] is defined and default_conf['msg'] != 'line added') or nginx_agent_configure_dynamic_force | bool
53+
notify: (Handler) Start NGINX Agent
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{ ansible_managed | comment }}
2+
3+
{% if nginx_agent_instance_group is defined %}
4+
instance_group: {{ nginx_agent_instance_group }}
5+
{% endif %}
6+
{% if nginx_agent_tags is defined %}
7+
tags:
8+
{% for tag in nginx_agent_tags %}
9+
- {{ tag }}
10+
{% endfor %}
11+
{% endif %}

0 commit comments

Comments
 (0)