简体   繁体   中英

How to evaluate nested bash expressions in ansible commands?

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

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

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"
        - ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM