ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
830 stars 1.53k forks source link

ini_file: allow whitespace at start of commented option #8539

Open sibebleuze opened 4 months ago

sibebleuze commented 4 months ago

Summary

I am using the community.general.ini_file plugin to alter the configuration of InfluxDB (in /etc/influxdb/influxdb.conf). I have modify_inactive_option enabled to replace commented out options with the ones I write in the playbook, yet ansible still adds the options on a new line instead of replacing the ones that are there. At first I thought there was a problem with the # symbol used for commented out options instead of semicolons. Then I looked at the regex used and I realized the problem was not in the comment symbol, but in the fact that there are spaces in front of it.

My request is to change the regex matcher used in the match_opt function of plugins/modules/ini_file.py from the current value of re.match('([#;]?)( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line) to something like re.match('( |\t)*([#;]?)( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line) to allow for spaces in front of the comment symbol. I was also thinking it could be useful to use \s to match whitespace instead of the currently used group ( |\t), unless of course there is a reason to only matches spaces and tabs, but not other whitespace characters.

I can imagine my case with InfluxDB is just one out of many different types of ini formats, so I think it would be valuable to get feedback on the impact of this change for others before implementing it.

Issue Type

Feature Idea

Component Name

ini_file

Additional Information

Current behaviour (on fresh InfluxDB install where all four of these options have been commented out): options are added on a new line, the commented versions are not replaced

Requested behaviour: commented options are replaced even when whitespace occurs before # or ;

Example playbook:

- name: Alter Influx configuration
  ini_file:
    path: /etc/influxdb/influxdb.conf
    backup: true
    create: false
    state: present
    modify_inactive_option: true
    section: "{{ item.section }}"
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  loop:
    - section: data
      option: query-log-enabled
      value: false
    - section: http
      option: log-enabled
      value: false
    - section: http
      option: flux-enabled
      value: true
    - section: http
      option: auth-enabled
      value: true
  become: true

Extract of /etc/influxdb/influxdb.conf on fresh install:

[data]
  # Whether queries should be logged before execution. Very useful for troubleshooting, but will
  # log any sensitive data contained within a query.
  # query-log-enabled = true

[http]
  # Determines whether the Flux query endpoint is enabled.
  # flux-enabled = false

  # Determines whether user authentication is enabled over HTTP/HTTPS.
  # auth-enabled = false

  # Determines whether HTTP request logging is enabled.
  # log-enabled = true

Code of Conduct

ansibullbot commented 4 months ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 4 months ago

cc @jpmens @noseka1 click here for bot help