ansible / ansible-runner

A tool and python library that helps when interfacing with Ansible directly or as part of another system whether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible.
Other
968 stars 356 forks source link

Using variable for Line with Lineinfile module throws exception #1333

Closed powej2 closed 10 months ago

powej2 commented 11 months ago

I'm trying to use python variables for the line and regexp elements of Lineinfile in Python using ansible-runner.

When I pass the string to ansible-runner for Line and Regexp in Lineinfile it works fine

When I put the exact same strings into variables and use them the same Lineinfile code I get an exception about wrong number of arguments

Any suggestions as to what I need to do in order to resolve this problem?

jrp999 commented 11 months ago

Variables:

line_to_add = "This is the line to add"
path = "/home/test/test.ini"
regexp = "^This is the line to add$"
state ='present'

This works:

run = ansible_runner.run(
    module='lineinfile',
    module_args='path={} line="This is the line to add" regexp="^This is the line to add\\$" state={}'.format(path, state),
    inventory='/usr/local/bin/gitlab/inv',
    host_pattern='all'
)

this doesn't work

run = ansible_runner.run(
    module='lineinfile',
    module_args='path={} line={} regexp="^{}\\$" state={}'.format(path, line_to_add, line_to_add, state),
    inventory='/usr/local/bin/gitlab/inv',
    host_pattern='all'
)

and produces this error:

ERROR! this task 'lineinfile' has extra params, which is only allowed in the following modules:

Thanks for investigating this

Shrews commented 10 months ago

I'm unable to recreate the error you are seeing. Please provide the output of the following commands:

powej2 commented 10 months ago

here you go, hope this helps:

ansible-runner --version 2.3.4

$ ansible --version ansible [core 2.16.2] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/ksfq/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/bin/python/venv/lib/python3.11/site-packages/ansible ansible collection location = /home/ksfq/.ansible/collections:/usr/share/ansible/collections executable location = /usr/local/bin/python/venv/bin/ansible python version = 3.11.2 (main, Jun 6 2023, 07:39:01) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] (/usr/local/bin/python/venv/bin/python) jinja version = 3.1.2 libyaml = True

Shrews commented 10 months ago

Oh, apologies, I tested the working code, not the non-working.

Your problem is a quoting issue. The non-working code is not using quotes around your strings with embedded spaces, thus causing ansible to see those as additional parameters.

Add these 3 new debugging parameters to your run() call and you'll see the ansible command that is being executed:

run = ansible_runner.run(
    verbosity=2, debug=True, ignore_logging=False,
    module='lineinfile',
    module_args='path={} line={} regexp="^{}\\$" state={}'.format(path, line_to_add, line_to_add, state),
    inventory='/usr/local/bin/gitlab/inv',
    host_pattern='all'
)
powej2 commented 10 months ago

Thanks David - That was a rookie move on my part - works as designed now

You're awesome - have a great day my friend!