-
Notifications
You must be signed in to change notification settings - Fork 23.7k
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
Comments
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:
205:
I think this module would benefit from a redesign using the Python iniparse module rather than implementing its parsing from scratch. |
needs_contributor |
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. |
I'm trying to solve this. Comments are welcome! |
Hi, there is a proposed fix in #32576 does this fix the issue for you? Please add review comments on the Pull Request |
Hi, the ini_file module already allows managing ini files without sections. You have to leave the "section" parameter empty like this:
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"? |
Hi,
I'm trying and cannot get this working:
➜ ~ cat test.yml
- name: set test configuration
hosts: local
connection: local
tasks:
- name: Create sample file
ini_file:
path: /tmp/test.ini
section:
option: myoption
value: no
mode: 0644
backup: yes
state: present
➜ ~ cat /tmp/test.ini
cat: /tmp/test.ini: No existe el archivo o el directorio *(Spanish, file
does not exists)*
➜ ~ ansible-playbook test.yml
PLAY [set test configuration]
*************************************************************************************************************************************************************************************
TASK [Gathering Facts]
********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Create sample file]
*****************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP
********************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
➜ ~ cat /tmp/test.ini
cat: /tmp/test.ini: No existe el archivo o el directorio *(Spanish, file
does not exists)*
➜ ~ ansible local -c local -m ini_file -a "path='/tmp/test.ini' section=''
option='aaa' value='bbbb' state='present'"
localhost | SUCCESS => {
"changed": false,
"msg": "OK",
"path": "/tmp/test.ini",
"state": "absent"
}
➜ ~ cat /tmp/test.ini
cat: /tmp/test.ini: No existe el archivo o el directorio *(Spanish, file
does not exists)*
*➜ ~ ansible local -c local -m ini_file -a "path='/tmp/test.ini'
section='sectionA' option='aaa' value='bbbb' state='present'"*
*localhost | SUCCESS => {*
* "changed": true,*
* "gid": 1000,*
* "group": "jdani",*
* "mode": "0644",*
* "msg": "section and option added",*
* "owner": "jdani",*
* "path": "/tmp/test.ini",*
* "size": 23,*
* "state": "file",*
* "uid": 1000*
*}*
*➜ ~ cat /tmp/test.ini*
*[sectionA]*
*aaa = bbbb*
➜ ~ ansible --version
ansible 2.4.2.0
config file = None
configured module search path = ['/home/jdani/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location =
/usr/local/lib/python3.5/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.5.3 (default, Nov 23 2017, 11:34:05) [GCC 6.3.0
20170406]
Which version are you using?
BTW, I agree using "null" in a task as keyword is not a good idea and does
not behave as documented. So I'll remove this behaviour...
…------
JDaniel Jimenez Paniagua
On 2 December 2017 at 19:22, Ales Nosek ***@***.***> wrote:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#30599 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AC0u6tQXtY1DgSwDCGgY5tSLvcABADB8ks5s8ZVfgaJpZM4PdS7n>
.
|
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:
So, yeah, this is a bug. Ansible should be able to add the option to a non-existing or empty file as well. |
Great to get it clear!
Can I ask why configparser isn't used in this module? The only caveat I see
are comments existing in the file, that are ignored and would be removed.
But, if a comment parameter is added to the ini_file module, comments could
be defined in the playbook...
El 3 dic. 2017 0:51, "Ales Nosek" <notifications@github.com> escribió:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#30599 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AC0u6qTgZhwBRms9wt427C_sF_gCmqJ4ks5s8eKSgaJpZM4PdS7n>
.
|
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. |
Great to know! In that case I'll try to fix it up sticking to the existing
code.
About comments, if the file is only modified with ansible, I think it's not
such a bad idea having comments in the playbook. The problem is that this
is not always the caseand, most important, you guys have already made a
decision about it. So that's all!
Many thanks! Really helpful!
…------
JDaniel Jimenez Paniagua
On 3 December 2017 at 17:46, Ales Nosek ***@***.***> wrote:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#30599 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AC0u6oev18Lnmn4nfzp16l6w_ysCv3erks5s8tBWgaJpZM4PdS7n>
.
|
…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)
…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)
…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)
…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)
…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)
…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)
…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)
…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)
…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)
…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)
…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)
This is still an issue in Ansible 2.5.3. |
I am also having the issue with Ansible 2.5.2. |
Still an open issue in Ansible 2.6.1. I've just hit it. |
I've pushed a PR that solves the problem a few months ago and still waiting
for someone to review it. Here it is:
#38971
------
JDaniel Jimenez Paniagua
…On Thu, 12 Jul 2018 at 13:47, Stephan ***@***.***> wrote:
Still an open issue in Ansible 2.6.1. I've just hit it.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#30599 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AC0u6hO3MsN5p6jr7GUknTIq7n5GBxpkks5uFzdYgaJpZM4PdS7n>
.
|
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. :) |
…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
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.: ``
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'"} |
you should open a separate bug for the 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.) |
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: |
ISSUE TYPE
COMPONENT NAME
ini_file
ANSIBLE VERSION
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
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
The text was updated successfully, but these errors were encountered: