Home > front end >  How to evaluate nested bash expressions in ansible commands?
How to evaluate nested bash expressions in ansible commands?

Time:08-29

I am trying to write an ansible script, and want to run a command with nested commands, e.g.:

echo "$(uname -s)-$(uname -r)" | grep "Linux"

NOTE: (The actual command is a curl request with multiple pipes and substitutions, but the above is just for simplification)

In my ansible playbook:


    - name: Run commands
      command: "{{ item }}"
      loop:
        - ...
        - echo "$(uname -s)-$(uname -r)" | grep "Linux"
        - ....

The problem is, ansible does not evaluate internal expressions and runs it literally: changed: [<IP>] => (item=echo "$(uname -s)-$(uname -r)" | grep "Linux")

I've seen examples of using pipe and lookup, but can't quite replicate it for multiple nested expressions.

Thanks

CodePudding user response:

As highlighted by β.εηοιτ.βε, by using the shell module I was able to execute the commands.

Changes code:


    - name: Run commands
      shell: "{{ item }}"
      loop:
        - ...
        - echo "$(uname -s)-$(uname -r)" | grep "Linux"
        - ....
  • Related