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

Using variable for Line with Lineinfile module throws exception #1333

Closed
powej2 opened this issue Dec 29, 2023 · 5 comments
Closed

Using variable for Line with Lineinfile module throws exception #1333

powej2 opened this issue Dec 29, 2023 · 5 comments

Comments

@powej2
Copy link

powej2 commented Dec 29, 2023

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?

@github-actions github-actions bot added the needs_triage New item that needs to be triaged label Dec 29, 2023
@jrp999
Copy link

jrp999 commented Dec 29, 2023

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
Copy link
Contributor

Shrews commented Jan 2, 2024

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

  • ansible-runner --version
  • ansible --version

@Shrews Shrews added needs_info and removed needs_triage New item that needs to be triaged labels Jan 2, 2024
@powej2
Copy link
Author

powej2 commented Jan 2, 2024

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
Copy link
Contributor

Shrews commented Jan 2, 2024

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'
)

@Shrews Shrews removed the needs_info label Jan 2, 2024
@powej2 powej2 closed this as completed Jan 2, 2024
@powej2
Copy link
Author

powej2 commented Jan 2, 2024

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

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

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

3 participants