Skip to content
Snippets Groups Projects
Verified Commit fad39693 authored by Chris Croome's avatar Chris Croome
Browse files

add conf_absent option for file edited

parent 11d5caa7
No related branches found
No related tags found
No related merge requests found
Pipeline #33279 failed
......@@ -124,6 +124,11 @@ php_config:
version: "8.2"
state: present
files:
- name: PHP 8.2 JIT configuration
path: /etc/php/8.1/mods-available/opcache.ini
state: edited
conf_absent:
"opcache.jit": "off"
- name: PHP 8.2 CLI configuration
path: /etc/php/8.2/cli/php.ini
state: present
......@@ -228,6 +233,11 @@ php_config:
version: "8.1"
state: present
files:
- name: PHP 8.1 JIT configuration
path: /etc/php/8.1/mods-available/opcache.ini
state: edited
conf_absent:
"opcache.jit": "off"
- name: PHP 8.1 CLI configuration
path: /etc/php/8.1/cli/php.ini
state: present
......@@ -332,6 +342,11 @@ php_config:
version: "8.0"
state: present
files:
- name: PHP 8.0 JIT configuration
path: /etc/php/8.0/mods-available/opcache.ini
state: edited
conf_absent:
"opcache.jit": "off"
- name: PHP 8.0 CLI configuration
path: /etc/php/8.0/cli/php.ini
state: present
......
......@@ -96,7 +96,11 @@ argument_specs:
conf:
type: dict
required: false
description: A optional YAML dictionary representing the sections, variables and values of a PHP configuration file.
description: A optional YAML dictionary representing the sections, variables and values of a PHP configuration file that sould be present.
conf_absent:
type: dict
required: false
description: A optional YAML dictionary representing the sections, variables and values of a PHP configuration file that should be absent.
name:
type: str
required: false
......
......@@ -19,16 +19,37 @@
- name: Set a fact for the existing PHP configuration file variables
ansible.builtin.set_fact:
php_conf_file_existing_vars: "{{ php_conf_file_b64encoded['content'] | ansible.builtin.b64decode | community.general.jc('ini') }}"
php_conf_file_no_extra_spaces: "{%- if (php_conf_file | ansible.builtin.dirname | ansible.builtin.split('/') | last) == 'mods-available' -%}true{%- else -%}false{%- endif -%}"
- name: Debug the existing PHP configuration file variables
ansible.builtin.debug:
var: php_conf_file_existing_vars
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the PHP configuration no extra spaces variable
ansible.builtin.debug:
var: php_conf_file_no_extra_spaces
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Set a fact to indicate that the existing PHP configuration file has sections
ansible.builtin.set_fact:
php_conf_file_existing_sections: >-
{%- if (php_conf_file_existing_vars |
ansible.builtin.json_query('*[]') |
map('ansible.builtin.type_debug') |
unique)[0] == "dict"
-%}true{%- else -%}false{%- endif -%}
- name: Debug the existing PHP configuration file sections variables
ansible.builtin.debug:
var: php_conf_file_existing_sections
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the existing PHP configuration file sections
ansible.builtin.debug:
var: php_conf_file_existing_vars.keys()
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
when: php_conf_file_existing_sections | bool
- name: Set a fact for the proposed PHP configuration file variables
ansible.builtin.set_fact:
......@@ -41,10 +62,42 @@
var: php_conf_file_proposed_vars
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Set a fact for the absent PHP configuration file variables
ansible.builtin.set_fact:
php_conf_file_absent_vars: "{{ php_config | community.general.json_query(php_conf_file_absent_vars_json_query) }}"
vars:
php_conf_file_absent_vars_json_query: "[?state=='present'].files[]|[?path=='{{ php_conf_file }}'].conf_absent|[0]"
- name: Debug the absent PHP configuration file variables
ansible.builtin.debug:
var: php_conf_file_absent_vars
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Set a fact to indicate that the proposed PHP configuration file has sections
ansible.builtin.set_fact:
php_conf_file_proposed_sections: >-
{%- if (php_conf_file_proposed_vars |
ansible.builtin.json_query('*[]') |
map('ansible.builtin.type_debug') |
unique)[0] == "dict"
-%}true{%- else -%}false{%- endif -%}
- name: Debug the proposed PHP configuration file sections variables
ansible.builtin.debug:
var: php_conf_file_proposed_sections
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the proposed PHP configuration file sections
ansible.builtin.debug:
var: php_conf_file_proposed_vars.keys()
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
when: php_conf_file_proposed_sections | bool
- name: Ensure that when the existing files has sections and proposed config also has sections
ansible.builtin.assert:
that:
- php_conf_file_existing_sections == php_conf_file_proposed_sections
quiet: "{% if ansible_verbosity == 0 %}true{% else %}false{% endif %}"
- name: Set facts for the PHP configuration file
ansible.builtin.set_fact:
......@@ -68,11 +121,6 @@
var: php_conf_file_version
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the proposed PHP configuration file sections
ansible.builtin.debug:
var: php_conf_file_proposed_vars.keys()
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: File backup present
ansible.builtin.copy:
src: "{{ php_conf_file }}"
......@@ -83,13 +131,68 @@
group: root
changed_when: false
- name: Edit the section-less PHP configuration file
community.general.ini_file:
path: "{{ php_conf_file }}"
option: "{{ php_conf_pair.key }}"
value: "{{ php_conf_pair.value }}"
state: present
modify_inactive_option: true
no_extra_spaces: "{{ php_conf_file_no_extra_spaces }}"
mode: "0644"
owner: root
group: root
loop: "{{ php_conf_file_proposed_vars | ansible.builtin.dict2items }}"
loop_control:
loop_var: php_conf_pair
label: "{{ php_conf_pair.key }}"
when:
- php_conf_file_proposed_vars is defined
- php_conf_file_proposed_vars | ansible.builtin.dict2items | length > 0
- not php_conf_file_existing_sections | bool
- not php_conf_file_proposed_sections | bool
- name: Remove config from the section-less PHP configuration file
community.general.ini_file:
path: "{{ php_conf_file }}"
option: "{{ php_conf_pair.key }}"
state: absent
mode: "0644"
owner: root
group: root
loop: "{{ php_conf_file_absent_vars | ansible.builtin.dict2items }}"
loop_control:
loop_var: php_conf_pair
label: "{{ php_conf_pair.key }}"
when:
- php_conf_file_absent_vars is defined
- php_conf_file_absent_vars | ansible.builtin.dict2items | length > 0
- not php_conf_file_existing_sections | bool
- not php_conf_file_proposed_sections | bool
- name: Include the PHP configuration file section edited tasks
ansible.builtin.include_tasks: file_section_edited.yml
loop: "{{ php_conf_file_proposed_vars | dict2items }}"
loop: "{{ php_conf_file_proposed_vars | ansible.builtin.dict2items }}"
loop_control:
loop_var: php_conf_section
label: "{{ php_conf_section.key }}"
when: php_conf_file_proposed_vars.keys() | length != 0
when:
- php_conf_file_proposed_vars is defined
- php_conf_file_proposed_vars | ansible.builtin.dict2items | length > 0
- php_conf_file_existing_sections | bool
- php_conf_file_proposed_sections | bool
- name: Include the PHP configuration file config absent tasks
ansible.builtin.include_tasks: file_section_absent.yml
loop: "{{ php_conf_file_absent_vars | ansible.builtin.dict2items }}"
loop_control:
loop_var: php_conf_section
label: "{{ php_conf_section.key }}"
when:
- php_conf_file_absent_vars is defined
- php_conf_file_absent_vars | ansible.builtin.dict2items | length > 0
- php_conf_file_existing_sections | bool
- php_conf_file_proposed_sections | bool
- name: Ansible managed comment present at the top of the file
ansible.builtin.lineinfile:
......
# Copyright 2019-2024 Chris Croome
#
# This file is part of the Webarchitects PHP Ansible role.
#
# The Webarchitects PHP Ansible role is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# The Webarchitects PHP Ansible role is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with the Webarchitects PHP Ansible role. If not, see <https://www.gnu.org/licenses/>.
---
- name: PHP configuration file section config absent
block:
- name: Debug the absent PHP configuration file variables
ansible.builtin.debug:
msg:
- "File: {{ php_conf_file }}"
- "Section: {{ php_conf_section.key }}"
- "Absent {{ php_conf_variable_pair.key }}"
verbosity: "{% if ansible_check_mode | bool or ansible_diff_mode | bool %}0{% else %}1{% endif %}"
when: php_conf_variable_pair.value != php_conf_file_existing_vars | community.general.json_query(php_conf_variable_json_query)
vars:
php_conf_variable_json_query: '"{{ php_conf_section.key }}"."{{ php_conf_variable_pair.key }}"'
loop: "{{ php_conf_section.value | ansible.builtin.dict2items }}"
loop_control:
loop_var: php_conf_variable_pair
label: "{{ php_conf_variable_pair.key }}"
- name: PHP configuration variables absent
community.general.ini_file:
path: "{{ php_conf_file }}"
section: "{{ php_conf_section.key }}"
option: "{{ php_conf_variable_pair.key }}"
state: absent
mode: "0644"
owner: root
group: root
when: php_conf_variable_pair.value != php_conf_file_existing_vars | community.general.json_query(php_conf_variable_json_query)
vars:
php_conf_variable_json_query: '"{{ php_conf_section.key }}"."{{ php_conf_variable_pair.key }}"'
loop: "{{ php_conf_section.value | ansible.builtin.dict2items }}"
loop_control:
loop_var: php_conf_variable_pair
label: "{{ php_conf_variable_pair.key }}"
register: php_conf_file_edited
- name: Set a fact to indicate that the file has changed # noqa: no-handler
ansible.builtin.set_fact:
php_conf_file_changed: true
# when: (php_conf_file_comments_edited.changed ) or ( php_conf_file_edited.changed | bool )
when: php_conf_file_edited.changed | bool
tags:
- php
- php_cfg
- php_conf
...
......@@ -54,8 +54,9 @@
section: "{{ php_conf_section.key }}"
option: "{{ php_conf_variable_pair.key }}"
value: "{{ php_conf_variable_pair.value }}"
state: present
modify_inactive_option: true
no_extra_spaces: false
no_extra_spaces: "{{ php_conf_file_no_extra_spaces }}"
mode: "0644"
owner: root
group: root
......
......@@ -14,6 +14,7 @@
- name: Set facts for the PHP configuration file
ansible.builtin.set_fact:
php_conf_file_backup: "{{ php_conf_file | ansible.builtin.dirname }}/.{{ php_conf_file | ansible.builtin.basename }}.{{ ansible_date_time.iso8601_basic_short }}.bak"
php_conf_file_no_extra_spaces: "{%- if (php_conf_file | ansible.builtin.dirname | ansible.builtin.split('/') | last) == 'mods-available' -%}true{%- else -%}false{%- endif -%}"
php_conf_file_proposed_vars: "{{ php_config | community.general.json_query(php_conf_file_proposed_vars_json_query) }}"
php_conf_file_sapi: "{{ php_conf_file | ansible.builtin.split(php_file_path_separator) | community.general.json_query('[4]') }}"
php_conf_file_version: "{{ php_conf_file | ansible.builtin.split(php_file_path_separator) | community.general.json_query('[3]') }}"
......@@ -25,6 +26,11 @@
var: php_conf_file_backup
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the PHP configuration no extra spaces variable
ansible.builtin.debug:
var: php_conf_file_no_extra_spaces
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the PHP configuration file PHP SAPI
ansible.builtin.debug:
var: php_conf_file_sapi
......@@ -40,10 +46,25 @@
var: php_conf_file_proposed_vars
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Set a fact to indicate that the proposed PHP configuration file has sections
ansible.builtin.set_fact:
php_conf_file_proposed_sections: >-
{%- if (php_conf_file_proposed_vars |
ansible.builtin.json_query('*[]') |
map('ansible.builtin.type_debug') |
unique)[0] == "dict"
-%}true{%- else -%}false{%- endif -%}
- name: Debug the proposed PHP configuration file sections variables
ansible.builtin.debug:
var: php_conf_file_proposed_sections
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
- name: Debug the proposed PHP configuration file sections
ansible.builtin.debug:
var: php_conf_file_proposed_vars.keys()
verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
when: php_conf_file_proposed_sections | bool
- name: File backup present
ansible.builtin.copy:
......
; {{ ansible_managed }}
; {# j2lint: disable=jinja-statements-indentation #}
{% for php_section in php_conf_file_proposed_vars | dict2items %}
{% if php_conf_file_proposed_sections | bool %}
{% for php_section in php_conf_file_proposed_vars | dict2items %}
[{{ php_section.key }}]
{% for php_variable_pair in php_section.value | dict2items %}
{% for php_variable_pair in php_section.value | dict2items %}
{% if php_conf_file_no_extra_spaces | bool %}
{{ php_variable_pair.key }}={{ php_variable_pair.value }}
{% else %}
{{ php_variable_pair.key }} = {{ php_variable_pair.value }}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
{% else %}
{% for php_conf_pair in php_conf_file_proposed_vars | ansible.builtin.dict2items %}
{% if php_conf_file_no_extra_spaces | bool %}
{{ php_conf_pair.key }}={{ php_conf_pair.value }}
{% else %}
{{ php_conf_pair.key }} = {{ php_conf_pair.value }}
{% endif %}
{% endfor %}
{% endif %}
; vim: syntax=dosini
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment