Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ini_file module does nothing if section is 'null' #30599

Closed
Alyust opened this issue Sep 20, 2017 · 21 comments
Closed

ini_file module does nothing if section is 'null' #30599

Alyust opened this issue Sep 20, 2017 · 21 comments
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bot_closed bug This issue/PR relates to a bug. collection:community.general collection Related to Ansible Collections work files Files category has_pr This issue has an associated PR. module This issue/PR relates to a module. needs_collection_redirect https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md support:community This issue/PR relates to code supported by the Ansible community.

Comments

@Alyust
Copy link

Alyust commented Sep 20, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

ini_file

ANSIBLE VERSION
$ ansible --version
ansible 2.4.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/a.yustus/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible-2.4.0-py2.7.egg/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
CONFIGURATION

$ ansible-config dump --only-changed
Usage: ansible-config dump [options] [-c ansible.cfg]

ansible-config: error: no such option: --only-changed

OS / ENVIRONMENT

Ubuntu 16.04.3 LTS

SUMMARY

ansible does not create destination file nor changes the option value if section parameter value is 'null'

STEPS TO REPRODUCE
    - name: set test configuration
      ini_file:
        path: /etc/conf
        section: null
        option: myoption
        value: no
        mode: 0644
        backup: yes
EXPECTED RESULTS

destination file is created or option value changed assuming that destionation file is a config file without sections as described in docs:
http://docs.ansible.com/ansible/latest/ini_file_module.html

ACTUAL RESULTS

nothing happens, no errors reported

@ansibot
Copy link
Contributor

ansibot commented Sep 20, 2017

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. labels Sep 20, 2017
@nrwahl2
Copy link
Contributor

nrwahl2 commented Sep 20, 2017

If the file doesn't exist or is empty, line 205 fails the conditional test because lines 171-172 insert a newline character.

171-172:

    if not ini_lines:
        ini_lines.append('\n')

205:

                        if not re.match(r'^[ \t]*([#;].*)?$', ini_lines[i - 1]):

I think this module would benefit from a redesign using the Python iniparse module rather than implementing its parsing from scratch.

@jpmens
Copy link
Contributor

jpmens commented Sep 20, 2017

needs_contributor

@ansibot ansibot added waiting_on_contributor This would be accepted but there are no plans to actively work on it. and removed needs_triage Needs a first human triage before being processed. labels Sep 20, 2017
@bobobox
Copy link
Contributor

bobobox commented Oct 3, 2017

Also running into this. Basically, the functionality described here doesn't seem to actually be implemented.

If this worked it would be hugely useful for dealing with (the very common) properties files which are essentially ini files without sections.

@jdani
Copy link

jdani commented Oct 31, 2017

I'm trying to solve this. Comments are welcome!

@gundalow
Copy link
Contributor

Hi, there is a proposed fix in #32576 does this fix the issue for you?

Please add review comments on the Pull Request

@noseka1
Copy link
Contributor

noseka1 commented Dec 2, 2017

Hi, the ini_file module already allows managing ini files without sections. You have to leave the "section" parameter empty like this:

- name: set test configuration
  ini_file:
    path: /etc/conf
    section:
    option: myoption
    value: no
    mode: 0644
    backup: yes

Setting the "section" parameter to "null" won't work as documented. However, I think that using "null" to denote that there are no sections in the ini file is not a good idea, anyway. What are we going to do with an ini file that would include a regular section named "null"?
In summary, ini_file allows to manage section-less ini files and to do that the user has to leave the "section" parameter empty. I would suggest to update the documentation to remove the section=null approach, as that's not implemented and I don't think we would want to implement it.

@jdani
Copy link

jdani commented Dec 2, 2017 via email

@noseka1
Copy link
Contributor

noseka1 commented Dec 2, 2017

Hi,

Thanks for poiting this out. You're right, the ini_file is buggy. When the file doesn't exist or exists but is empty, ini_file module won't add the new option. Only when the file already contains some options, then it worked for me:

$ ansible-playbook --version
ansible-playbook 2.2.1.0

$ echo someoption=value > /tmp/test.ini

$ ansible localhost -c local -m ini_file -a "dest='/tmp/test.ini' section='' option='aaa' value='bbbb' state='present'"

$ cat /tmp/test.ini
someoption=value
aaa = bbbb

So, yeah, this is a bug. Ansible should be able to add the option to a non-existing or empty file as well.

@jdani
Copy link

jdani commented Dec 3, 2017 via email

@noseka1
Copy link
Contributor

noseka1 commented Dec 3, 2017

Configparser was used in this module until Ansible < 2.0. In 2.0 we replaced the implementation based on Configparser with our own. The reason was as you mentioned that Configparser would remove comments. See also my post from that time: http://alesnosek.com/blog/2015/08/03/improving-ansibles-ini-file-module/

I'm not sure if storing ini comments in the playbook is a good idea. When a new version of the ini file with updated comments is released, it would get out of sync with the ini comments hard-coded in the playbooks.

@jdani
Copy link

jdani commented Dec 3, 2017 via email

adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
adamdyson pushed a commit to ontic/ansible-role-postgresql that referenced this issue Feb 19, 2018
…file or create it for the first time, based on a ninja template, thereafter settings will be defined via ini_file module. A reason for doing things this way is partly because of this bug: ansible/ansible#30599 (comment)
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
@rpural
Copy link

rpural commented May 29, 2018

This is still an issue in Ansible 2.5.3.

@alexandremenif
Copy link

I am also having the issue with Ansible 2.5.2.

@stephan2012
Copy link

Still an open issue in Ansible 2.6.1. I've just hit it.

@jdani
Copy link

jdani commented Jul 12, 2018 via email

@codylane
Copy link
Contributor

codylane commented Aug 17, 2018

I hope I'm not stepping on anyone's toes but I was able to checkout the PR and address the reviewers comments. #44324 . Hopefully this can be merged soon. :)

@ansibot ansibot added support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed support:community This issue/PR relates to code supported by the Ansible community. labels Sep 18, 2018
@ansibot ansibot added needs_maintainer Ansibot is unable to identify maintainers for this PR. (Check `author` in docs or BOTMETA.yml) support:community This issue/PR relates to code supported by the Ansible community. and removed support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 4, 2018
@gundalow
Copy link
Contributor

Hi,
Fix in #44324 has been merged into devel for release in Ansible 2.8
Backport for stable-2.7 created #47741

abadger pushed a commit that referenced this issue Oct 31, 2018
…ction managed (#47741)

* Addresses comments in #38971 (#44324)

* Controlled params within no section

* Added tests to control params within no section

* Cleaning output_file before creating no-section params and check the content

* addresses comment in PR "s/hate/beverage/g"

(cherry picked from commit d3fe6c0)

* 44324-ini_file
@ansibot ansibot removed the needs_maintainer Ansibot is unable to identify maintainers for this PR. (Check `author` in docs or BOTMETA.yml) label Nov 14, 2018
@ansibot ansibot added the files Files category label Mar 5, 2019
@xp499
Copy link

xp499 commented Jun 20, 2019

idk if you have already fix this issue with ini_file module (that i'm supposing to yes when it doesn't throw any error) but this still exists on the lookup plugin ini, f.e.:

``

  • debug:
    msg: "{{ lookup('ini', 'GRUB_CMDLINE_LINUX_DEFAULT section file=/etc/default/grub') }}"

fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'ini'. Error was a <class 'configparser.MissingSectionHeaderError'>, original message: File contains no section headers.\nfile: '<???>', line: 1\n'GRUB_DEFAULT=saved\n'"}
``

@jamescassell
Copy link
Contributor

idk if you have already fix this issue with ini_file module (that i'm supposing to yes when it doesn't throw any error) but this still exists on the lookup plugin ini, f.e.:

you should open a separate bug for the ini lookup

Did the 'add an option to an empty file' bug mentioned in the thread get fixed with #44324 ? (If so, we can probably close this bug; if not, clarify with a reproducer.)

@ansibot ansibot added the has_pr This issue has an associated PR. label Jul 28, 2019
@ansibot ansibot added collection Related to Ansible Collections work collection:community.general needs_collection_redirect https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md labels Apr 29, 2020
@ansibot
Copy link
Contributor

ansibot commented Aug 16, 2020

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.

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

@ansibot ansibot closed this as completed Aug 16, 2020
@ansible ansible locked and limited conversation to collaborators Sep 13, 2020
@sivel sivel removed the waiting_on_contributor This would be accepted but there are no plans to actively work on it. label Dec 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bot_closed bug This issue/PR relates to a bug. collection:community.general collection Related to Ansible Collections work files Files category has_pr This issue has an associated PR. module This issue/PR relates to a module. needs_collection_redirect https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet