user3796292 user3796292 - 3 months ago 27
Bash Question

Print out contents of a file using ansible

I want to print out the results of a text file in ansible using cat. This is my code.

tasks:
- name: This option
script: "./getIt.py -s {{ myhostname }} -u {{ myuser }} -p {{ mypass }} >> ./It.txt"
shell: cat ./It.txt
when: user_option == 'L'


This however doesn't work. What am I doing wrong?

mwp mwp
Answer

You are trying to call two different modules from a single task: script and shell. You need to break them up... one module per task! However, there's a better way to do it by capturing the output of the script with register, and using the debug module in a subsequent task to display it:

tasks:
  - name: This option
    script: "./getIt.py -s {{ myhostname }} -u {{ myuser }} -p {{ mypass }}"
    register: script
    when: user_option == 'L'
  - name: stdout of getIt
    debug: msg={{ script.stdout }}
    when: script is defined and script|succeeded