Skip to content

Commit 27ddc2c

Browse files
authored
Avoid re-copying the NGINX Amplify config file every role re-run (#679)
And add Molecule tests for NGINX Amplify. Fixes #667.
1 parent 612809c commit 27ddc2c

File tree

10 files changed

+181
-7
lines changed

10 files changed

+181
-7
lines changed

.github/workflows/molecule.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ jobs:
3737
molecule:
3838
name: Molecule
3939
runs-on: ubuntu-22.04
40+
needs: ansible-lint
4041
env:
42+
AMPLIFY_API_KEY: ${{ secrets.AMPLIFY_API_KEY }}
43+
AMPLIFY_EMAIL: ${{ secrets.AMPLIFY_EMAIL }}
44+
AMPLIFY_PASSWORD: ${{ secrets.AMPLIFY_PASSWORD }}
4145
NGINX_CRT: ${{ secrets.NGINX_CRT }}
4246
NGINX_KEY: ${{ secrets.NGINX_KEY }}
4347
strategy:
4448
fail-fast: false
4549
matrix:
4650
scenario:
51+
- amplify
4752
- default
4853
- distribution
4954
- downgrade
@@ -57,7 +62,6 @@ jobs:
5762
- upgrade
5863
- upgrade-plus
5964
- version
60-
needs: ansible-lint
6165
steps:
6266
- name: Check out the codebase
6367
if: ${{ !(contains(matrix.scenario, 'plus')) || (env.NGINX_CRT != 0 && env.NGINX_KEY != 0) }}

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ ENHANCEMENTS:
66

77
- Allow strings in addition to a list when configuring `logrotate`.
88

9+
BUG FIXES:
10+
11+
- Avoid re-copying the NGINX Amplify config file every time the role is run.
12+
13+
CI/CD:
14+
15+
- Add Molecule tests for NGINX Amplify.
16+
917
## 0.24.2 (October 3rd, 2023)
1018

1119
FEATURES:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Working functional playbook examples can be found in the **[`molecule/`](https:/
209209

210210
| Name | Description |
211211
| ---- | ----------- |
212+
| **[`amplify/converge.yml`](https://github.com/nginxinc/ansible-role-nginx/blob/main/molecule/amplify/converge.yml)** | Install and configure the NGINX Amplify agent |
212213
| **[`default/converge.yml`](https://github.com/nginxinc/ansible-role-nginx/blob/main/molecule/default/converge.yml)** | Install a specific version of NGINX, install various NGINX supported modules, tweak systemd and set up logrotate |
213214
| **[`distribution/converge.yml`](https://github.com/nginxinc/ansible-role-nginx/blob/main/molecule/distribution/converge.yml)** | Install NGINX from the distribution's package repository instead of NGINX's package repository |
214215
| **[`downgrade/converge.yml`](https://github.com/nginxinc/ansible-role-nginx/blob/main/molecule/downgrade/converge.yml)** | Downgrade to a specific version of NGINX |

molecule/amplify/cleanup.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
- name: Cleanup
3+
hosts: localhost
4+
gather_facts: false
5+
tasks:
6+
- name: Cleanup NGINX Amplify instances
7+
block:
8+
- name: Wait for containers to be up
9+
ansible.builtin.wait_for_connection:
10+
delay: 1
11+
timeout: 2
12+
ignore_errors: true
13+
register: container
14+
15+
- name: Containers are not up, quit from here
16+
ansible.builtin.fail:
17+
when: container['failed']
18+
19+
- name: Gather facts
20+
ansible.builtin.setup:
21+
gather_subset:
22+
- "!all"
23+
- "!any"
24+
- distribution
25+
26+
- name: Login to NGINX Amplify
27+
ansible.builtin.uri:
28+
url: "https://amplify.nginx.com/sapi/auth/login/"
29+
method: POST
30+
headers:
31+
Content-Type: application/json
32+
body_format: json
33+
body:
34+
email: "{{ lookup('env', 'AMPLIFY_EMAIL') }}"
35+
password: "{{ lookup('env', 'AMPLIFY_PASSWORD') }}"
36+
register: login
37+
38+
- name: Get list of IDs
39+
ansible.builtin.uri:
40+
url: "https://amplify.nginx.com/sapi/inventory/objects/?all=true&filter=false/"
41+
method: GET
42+
headers:
43+
Cookie: "{{ login.cookies_string }}"
44+
Connection: keep-alive
45+
follow_redirects: all
46+
register: get_ids
47+
48+
- name: Remove dangling instances from NGINX Amplify
49+
ansible.builtin.uri:
50+
url: "https://amplify.nginx.com/sapi/inventory/objects/{{ item }}/"
51+
method: DELETE
52+
status_code: 204
53+
headers:
54+
Cookie: "{{ login.cookies_string }}"
55+
loop: "{{ get_ids.json | rejectattr('parent_id', 'match', '^[0-9]+$') | map(attribute='id') | list }}"
56+
rescue:
57+
- name: It's ok we're at startup
58+
ansible.builtin.meta: noop

molecule/amplify/converge.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Converge
3+
hosts: all
4+
tasks:
5+
- name: Install NGINX Amplify
6+
ansible.builtin.include_role:
7+
name: ansible-role-nginx
8+
vars:
9+
nginx_amplify_enable: true
10+
nginx_amplify_api_key: "{{ lookup('env', 'AMPLIFY_API_KEY') }}"

molecule/amplify/molecule.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
driver:
3+
name: docker
4+
platforms:
5+
- name: amazonlinux-2
6+
image: amazonlinux:2
7+
platform: x86_64
8+
dockerfile: ../common/Dockerfile.j2
9+
privileged: true
10+
cgroupns_mode: host
11+
volumes:
12+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
13+
command: /usr/sbin/init
14+
- name: debian-bullseye
15+
image: debian:bullseye-slim
16+
dockerfile: ../common/Dockerfile.j2
17+
privileged: true
18+
cgroupns_mode: host
19+
volumes:
20+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
21+
command: /sbin/init
22+
- name: oraclelinux-8
23+
image: oraclelinux:8
24+
dockerfile: ../common/Dockerfile.j2
25+
privileged: true
26+
cgroupns_mode: host
27+
volumes:
28+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
29+
command: /usr/sbin/init
30+
- name: rhel-9
31+
image: redhat/ubi9:9.1.0
32+
# platform: aarch64
33+
dockerfile: ../common/Dockerfile.j2
34+
privileged: true
35+
cgroupns_mode: host
36+
volumes:
37+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
38+
command: /usr/sbin/init
39+
- name: ubuntu-jammy
40+
image: ubuntu:jammy
41+
# platform: aarch64
42+
dockerfile: ../common/Dockerfile.j2
43+
privileged: true
44+
cgroupns_mode: host
45+
volumes:
46+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
47+
command: /sbin/init
48+
provisioner:
49+
name: ansible
50+
log: true
51+
playbooks:
52+
cleanup: cleanup.yml
53+
converge: converge.yml
54+
verify: verify.yml

molecule/amplify/verify.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
- name: Verify
3+
hosts: all
4+
tasks:
5+
- name: Check if NGINX is installed
6+
ansible.builtin.package:
7+
name: nginx
8+
state: present
9+
check_mode: true
10+
register: install
11+
failed_when: (install is changed) or (install is failed)
12+
13+
- name: Check if NGINX service is running
14+
ansible.builtin.service:
15+
name: nginx
16+
state: started
17+
enabled: true
18+
check_mode: true
19+
register: service
20+
failed_when: (service is changed) or (service is failed)
21+
22+
- name: Verify NGINX is up and running
23+
ansible.builtin.uri:
24+
url: http://localhost
25+
status_code: 200
26+
27+
- name: Check if NGINX Amplify agent is installed
28+
ansible.builtin.package:
29+
name: nginx-amplify-agent
30+
state: present
31+
check_mode: true
32+
register: install
33+
failed_when: (install is changed) or (install is failed)

tasks/amplify/install-amplify.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@
88
name: nginx-amplify-agent
99
state: present
1010

11-
- name: Copy NGINX Amplify configurator agent configuration template
11+
- name: Check if NGINX Amplify agent configuration file is present
12+
ansible.builtin.stat:
13+
path: /etc/amplify-agent/agent.conf
14+
register: nginx_amplify_agent_config
15+
16+
- name: Copy NGINX Amplify agent configuration template
1217
ansible.builtin.copy:
1318
remote_src: true
1419
src: /etc/amplify-agent/agent.conf.default
1520
dest: /etc/amplify-agent/agent.conf
1621
mode: "0644"
22+
when: not nginx_amplify_agent_config['stat']['exists']
1723

1824
- name: Configure NGINX Amplify agent API key
1925
ansible.builtin.lineinfile:
2026
dest: /etc/amplify-agent/agent.conf
2127
regexp: api_key =.*
2228
line: api_key = {{ nginx_amplify_api_key }}
29+
when:
30+
- nginx_amplify_api_key is defined
31+
- nginx_amplify_api_key | length > 0
2332
notify: (Handler) Start NGINX Amplify agent

tasks/amplify/setup-debian.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
- name: (Debian/Ubuntu) Add NGINX Amplify agent repository
33
ansible.builtin.apt_repository:
44
filename: nginx-amplify
5-
repo: deb [arch=amd64 signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://packages.amplify.nginx.com/py3/{{ ansible_facts['distribution'] | lower }}/{{ ansible_facts['distribution_release'] | lower }} amplify-agent
5+
repo: deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://packages.amplify.nginx.com/py3/{{ ansible_facts['distribution'] | lower }} {{ ansible_facts['distribution_release'] | lower }} amplify-agent
66
update_cache: true
77
mode: "0644"

tasks/main.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,5 @@
7676

7777
- name: Install NGINX Amplify
7878
ansible.builtin.include_tasks: "{{ role_path }}/tasks/amplify/install-amplify.yml"
79-
when:
80-
- nginx_amplify_enable | bool
81-
- nginx_amplify_api_key is defined
82-
- nginx_amplify_api_key | length > 0
79+
when: nginx_amplify_enable | bool
8380
tags: nginx_install_amplify

0 commit comments

Comments
 (0)