user3796292
user3796292

Reputation: 141

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?

Upvotes: 5

Views: 15066

Answers (1)

mwp
mwp

Reputation: 8467

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

Upvotes: 5

Related Questions