ansible-collections / community.general

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

ini_file module to allow multiple distinct key-value pairs #1204

Open ptMcGit opened 4 years ago

ptMcGit commented 4 years ago

Please raise issues via the new interface

Per ansible/ansible:

Thank you very much for your interest in Ansible. Ansible has migrated much of the content into separate repositories to allow for more rapid, independent development. We are closing this issue/PR because this content has been moved to one or more collection repositories.

lib/ansible/modules/files/ini_file.py -> https://galaxy.ansible.com/community/general

For further information, please see: https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md

SUMMARY

This request regards the community.general ini_file module (https://github.com/ansible-collections/community.general/blob/main/plugins/modules/files/ini_file.py)

Currently, a user can modify one value in an ini file:

- name: Ensure "fav=lemonade is in section "[drinks]" in specified file
  ini_file:
    path: /etc/conf
    section: drinks
    option: fav
    value: lemonade
    mode: '0600'
    backup: yes

I am interested in modifying the module such that multiple key-value pairs in one or more sections can be added, for example:

- name: Ensure drinks and foods in specified file.
  ini_file:
    path: /etc/conf
    data:
      - name: drinks
        - name: fav
          value: lemonade
      - name: foods
        - name: fav
          value: cheeseburger
    mode: '0600'
    backup: yes

Does this seem feasible? I have a prototype that seems to be working so far. I would like to continue working on it if no one sees any issues. Thanks, and apologies if I'm abusing the module style, as I'm not a regular Ansible developer.

@Andersson007 @felixfontein

ISSUE TYPE
COMPONENT NAME

ini_file

ADDITIONAL INFORMATION

A user may wish to edit multiple key-value pairs in an ini file, e.g. a systemd file, in a single task.

There are at least two alternatives:

See Summary section
ansibullbot commented 4 years ago

Files identified in the description:

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

click here for bot help

ansibullbot commented 4 years ago

cc @jpmens @noseka1 click here for bot help

felixfontein commented 4 years ago

I have no idea why you're asking me, but since what you need can easily be done with the existing ini_file module and a loop, I'm not sure whether this should be added. This would increase the complexity of the module and doesn't really add something that wasn't possible before.

Also it doesn't solve the most common "problem" with ini_file, namely being able to specify a key multiple times in the same section. That's in particular needed for systemd units.

Also, when talking about systemd units, why do you actually want to edit existing ones, instead of using drop-in files (in unitname.d/) to adjust behavior of existing units without modifying them?

ptMcGit commented 4 years ago

Thanks for the reply--

I reached out to you because you were listed as a contributor for this module, and I figured you would be in the best position to comment here.

My mistake--I see now that the loop mechanism is available for this module.

You're right that my current implementation doesn't solve ansible-collections/community.general#273, but I thought that it might help to point to a solution.

Also, I was not aware of the drop-in file mechanism. I think this will actually work for what I'm trying to do. This has saved me a lot of time. Thanks for the info

felixfontein commented 4 years ago

@ptMcGit ah the only reason why we're listed is that we did generic PRs which touched a lot of modules. Neither @Andersson007 nor me specifically worked on this module :) See https://github.com/ansible/ansible/blob/pre-ansible-base/lib/ansible/modules/files/ini_file.py for a better list.

In any case, the drop-in file mechanism is a great thing, it also solved a lot of trouble for me when I discovered it some time ago :)

russoz commented 3 years ago

@ptMcGit hi, just a friendly check: does is still makes sense to keep this issue open? It kinda looks like the problem has already been solved.

quidame commented 3 years ago

I am interested in modifying the module such that multiple key-value pairs in one or more sections can be added

@ptMcGit what you describe is the intent to manage a complete datastructure by calling the module only once.

Does this seem feasible?

I think it doesn't. More precisely: even if it is technically doable, it would bloat the module and make it harder to maintain. Also, there is no real benefit to break the existing interface, that means the current parameters of the module should still work.

A recent PR (#3033) has improved the module a lot. What you described is now doable in one task, by calling the module as much as needed, in a loop. Could you please try with the current (mainline) version of the module ?

- name: Ensure drinks and foods in specified file.
  ini_file:
    path: /etc/conf
    section: "{{ item.section }}"
    option: "{{ item.option }}"
    values: "{{ item.values | d(omit) }}"
    value: "{{ item.value | d(omit) }}"
    mode: '0600'
    backup: true
    exclusive: "{{ item.exclusive | d(omit) }}"    # omit => True
    state: "{{ item.state | d(omit) }}"            # omit => present
  loop:
    # Add 'fav' option in [drinks], with N values (remove other 'fav' options, if any)
    - section: drinks
      option: fav
      values:
        - water
        - lemonade
        - apple juice
    # Add 'fav=cheeseburger' to [foods], keeping other 'fav' options, if any
    - section: foods
      option: fav
      value: cheeseburger
      exclusive: false
    # Remove 'fav=chocolate' from [foods], keeping other 'fav' options
    - section: foods
      option: fav
      value: chocolate
      exclusive: false
      state: absent

Does it cover your use case ?

braindevices commented 3 years ago

@quidame what you proposed is not atomic which can cause serious consequences. I described here https://github.com/ansible-collections/community.general/issues/3397

ansibullbot commented 2 years 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