1

In a Bash script we need to write $1 instead the variable, remove the read command, define the survey in the AAP to execute the Bash file, and add the arguments in the shell module. It works. Now, how is it for a Python script?

Trying to make a TXT file with the value that the user inputs works in local witout any problem:

print("Enter your value")
name = input()
with open("/us/gr_comun/prueba_py_ansible.txt", "w") as file:
    file.write(f"{{name}}")

Following how it is for Bash, the Python script should look like this:

print("Enter your value")
with open("/us/gr_comun/prueba_py_ansible.txt", "w") as file:
    file.write(f"{{$1}}")

It does not work.

The YML:

    - name:  RUN PYTHON ON TARGET
      changed_when: false
      shell: python3 /.../try_python.py {{side_a}}
      become: true
      become_user: xxxx
      register: py_output

The script (adapted to AAP and tested locally):

# name = input()
with open("/.../try_txt.txt", "w") as file:
    file.write(f"{{$1}}")

The survey contains only the side_a variable, and it is working already for Bash cases.

It runs the python script in a Remote Host.

Does anybody know how it would be?

I tried several variations, and look for an answer over the Internet. I expect to pass user input for python through AAP.

1
  • Can you provide more details and your playbook you are trying to execute? Sepcifically how and where you run your Python script? As well show how your survey is? Commented Sep 25 at 16:24

1 Answer 1

0

First, take note that according documentation Job Templation - Extra Variables

When you pass survey variables, they are passed as extra variables (extra_vars) ...

Then, the question becomes "How to use Ansible extra variables with Python scripts?" and a minimmal example could look like Run Python script with arguments in Ansible

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    survey_input: test

  tasks:

  - name: Create example Python script
    copy:
      dest: survey.py
      content: |
        #!/usr/bin/python

        import sys

        print("arguments:", len(sys.argv))
        print("first argument:", str(sys.argv[1]))

  - name: Run example Python script
    script: survey.py {{ survey_input }}
    register: results

  - name: Show result
    debug:
      msg: "{{ results.stdout }}"

and which result into an output of

TASK [Show result] *****
ok: [localhost] =>
  msg: |-
    arguments: 2
    first argument: test

Finally it will depend on if you like to provide the content of the extra variables as arguments for the script or within the script by templating.

Further Q&A

Which might be interesting or help in this Use Case

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I had to switch topic for the moment, will answer soon

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.