user245011
user245011

Reputation: 75

How to format a long Ansible command module task over multiple lines?

I am trying to format following command tasks over multiple lines.

tasks:
    ...
    - name: Run the python file
      command: "{{ lookup('env','HOME') }}/bin/pythonfile.py \"{{ cmd_status.stdout }}\" {{ test_number }}"

Works without formatting. pythonfile is executed properly. I tried formatting with >:

tasks:
    ...
    - name: Run the python file
      command: >
        "{{ lookup('env','HOME') }}/bin/pythonfile.py \"{{ cmd_status.stdout }}\" {{ test_number }}"

and it gives:

"msg": "[Errno 2] No such file or directory",

Debug:
"invocation": {
    "module_args": {
        "_raw_params": "\"/home/bin/pythonfile.py

Any suggestions on formatting the command line over multiple lines.

Upvotes: 2

Views: 6666

Answers (1)

techraf
techraf

Reputation: 68469

Simply drop the surrounding quotes:

command: >
  {{ lookup('env','HOME') }}/bin/pythonfile.py "{{ cmd_status.stdout }}" {{ test_number }}

Otherwise the whole string (including spaces and arguments) is considered to be the name of the executable to run (notice \" surrounding the whole line in the debug invocation string).

When you write it in a single line, the gets first interpreted and stripped by YAML parser.

Upvotes: 5

Related Questions