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

module xml failing when running with jinja2_native=True and value is a number #26

Open
cmasopust opened this issue Oct 15, 2024 · 2 comments
Assignees

Comments

@cmasopust
Copy link

SUMMARY

Setting jinja2_native=True in ansible.cfg and run the following playbook:

- name: all
  hosts: localhost
  gather_facts: no
  become: no

  vars:
    myval: 255

  tasks:
    - redhat.runtimes_common.xml:
        path: test.xml
        xpath: "/business/rating"
        value: "{{ myval }}"
        pretty_print: true

results in:

TASK [redhat.runtimes_common.xml] *******************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: Argument must be bytes or unicode, got 'int'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/home/cmaso/.ansible/tmp/ansible-tmp-1728972733.6213477-1779995-35690117152941/AnsiballZ_xml.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/home/cmaso/.ansible/tmp/ansible-tmp-1728972733.6213477-1779995-35690117152941/AnsiballZ_xml.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/cmaso/.ansible/tmp/ansible-tmp-1728972733.6213477-1779995-35690117152941/AnsiballZ_xml.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.redhat.runtimes_common.plugins.modules.xml', init_globals=dict(_module_fqn='ansible_collections.redhat.runtimes_common.plugins.modules.xml', _modlib_path=modlib_path),\n  File \"/usr/lib64/python3.9/runpy.py\", line 225, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.9/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib64/python3.9/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_redhat.runtimes_common.xml_payload_i_tmacpd/ansible_redhat.runtimes_common.xml_payload.zip/ansible_collections/redhat/runtimes_common/plugins/modules/xml.py\", line 1018, in <module>\n  File \"/tmp/ansible_redhat.runtimes_common.xml_payload_i_tmacpd/ansible_redhat.runtimes_common.xml_payload.zip/ansible_collections/redhat/runtimes_common/plugins/modules/xml.py\", line 1004, in main\n  File \"/tmp/ansible_redhat.runtimes_common.xml_payload_i_tmacpd/ansible_redhat.runtimes_common.xml_payload.zip/ansible_collections/redhat/runtimes_common/plugins/modules/xml.py\", line 708, in set_target\n  File \"/tmp/ansible_redhat.runtimes_common.xml_payload_i_tmacpd/ansible_redhat.runtimes_common.xml_payload.zip/ansible_collections/redhat/runtimes_common/plugins/modules/xml.py\", line 694, in set_target_inner\n  File \"src/lxml/etree.pyx\", line 1024, in lxml.etree._Element.text.__set__\n  File \"src/lxml/apihelpers.pxi\", line 748, in lxml.etree._setNodeText\n  File \"src/lxml/apihelpers.pxi\", line 736, in lxml.etree._createTextNode\n  File \"src/lxml/apihelpers.pxi\", line 1539, in lxml.etree._utf8\nTypeError: Argument must be bytes or unicode, got 'int'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

Not totally positive if that can/should be treated as a bug (module docu says that value must be a string), maybe it should be reported as a feature request?

ISSUE TYPE
  • Bug Report
ANSIBLE VERSION
ansible [core 2.14.14]
  config file = /home/cmaso/test/xml/ansible.cfg
  configured module search path = ['/home/cmaso/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/cmaso/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.18 (main, Jul  3 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
redhat.amq_broker                        2.2.5
redhat.runtimes_common             1.2.2
EXPECTED RESULTS

Module xml may be extended to allow more basic types (e.g. int, boolean, etc) for parameter value.

ACTUAL RESULTS

@RanabirChakraborty RanabirChakraborty self-assigned this Nov 6, 2024
@RanabirChakraborty
Copy link
Member

@cmasopust you need to explicitly convert myval to a string before passing it to the value parameter.
The value: "{{ myval | string }}" ensures that the integer 255 is converted to the string "255".

@cmasopust
Copy link
Author

I know that will be a workaround, but to make it more user friendly... this conversion could also happen in the module code.

But of course, I'm not sure if that is best practice or if some Ansible module guidelines are against this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants