0

I'm trying to execute a python script with args parser via ansible i want to put all my argument in a single extra_var but i im missing something

lets say my python script can get a username -u and a password -p and my ansible script has a single var my_args

script: /tmp/args.py "{{ my_args }}"

when i run my playbook like this :

ansible-playbook my_ansible_playbook.yml -e "my_args='-u my_username -p my_password'"

the result im getting is:

username = my_username -p my_password

password = default_password

what am i missing?

how do i send each value to the correct value with a single extra_var?

2
  • 1
    Try this way and see if it changes something: script: "/tmp/args.py {{ my_args }}" Commented Dec 10, 2019 at 15:20
  • thanks @Zeitounator, i tried so many options and i cant believe i missed this one Commented Dec 10, 2019 at 15:39

1 Answer 1

1

If you dont quote the entire script value, yaml sees the it as a string containing double quotes with a value inside (later interpreted by jinja2). In the end you script is called with a single parameter being your full templated string.

To pass all params as you are trying, you need to quote the entire string.

See the following example as an illustration:

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Show the extra var itself
      debug:
        var: myvar

    - name: Quotes in command (wrong)
      debug:
        msg: myscript.py "{{ myvar }}"

    - name: No quotes (good) - simple command with params
      debug:
        msg: "myscript.py {{ myvar }}"

And the result:

$ ansible-playbook tmp.yml -e "myvar='-u toto -p bingo'"

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Show the extra var itself] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "myvar": "-u toto -p bingo"
}

TASK [Quotes in command (wrong)] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myscript.py \"-u toto -p bingo\""
}

TASK [No quotes (good) - simple command with params] **************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myscript.py -u toto -p bingo"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.