Change request: community.general.ini_file

I just signed up and I’m not sure if this is the right place to post this so here it goes:

The in the community.general.ini_file module the default return for changed: false is fail if the file does not exist. To me, it would make more sense if it was marked as skipped. Thoughts?

Welcome to the forum!

I’m not sure I understand what you mean. Can you specify more precisely in which case the module fails when it shouldn’t?

If I do:

- name: Ini test 1

community.general.ini_file:

name: /some/test/inifile

create: true

section: main

option: purple

value: peopleeater

It will create the file /some/test/inifile

And put in it:

[main]

purple=peopleater

If I change this to:

- name: Ini test 1

community.general.ini_file:

name: /some/test/inifile

create: false

section: main

option: purple

value: peopleater

If /some/test/inifile does not exist. The play will fail and thus cause the whole playbook to fail. I’m suggesting that change: false behavior should be skipped vs fail. So that if the file exists, great modify it, otherwise move on to the next play and notify that the play was skipped. Yes, I understand I could run a pre-check play and see if /some/test/inifile exists, then run the “Ini test 1” play with a when statement. I’m just suggesting the idea, and I’m a relative noob to the Ansible world, so I was wondering what the appetite for a change like this might be.

Thanks for listening :blush:

If /some/test/inifile does not exist. The play will fail and thus cause the whole playbook to fail.

That’s the intended behavior for create: false. Changing it would break a LOT of existing playbooks and roles.

If you want to skip the task when the file does not exist, you need to use the ansible.builtin.stat to first figure out whether the file exists, register its return value, and then add a when: condition based on that return value to the community.general.ini_file task. It will be skipped if the condition evaluates to false.

3 Likes

People expect modules to fail when they can’t do what they’re asked to and it would be a bad idea to change the module’s behaviour.

You can instead use failed_when to change what is considered an error for your specific task, since you’re okay with this change not being made.

    - name: Add value to nonexistent file
      community.general.ini_file:
        path: /some/test/inifile
        create: false
        section: main
        option: purple
        value: peopleater
      register: result
      failed_when:
        - result is failed
        - result.msg != 'Destination /some/test/inifile does not exist!'
3 Likes