Ansible: multi-line shell script inside playbook

Sometimes ansible is not enough, and you want to unleash the raw shell power. But long one-line scripts look totally unreadable. Here is how you can do it with YAML multi-line representation:

- name: long shell script
  shell: |
    cat /proc/cmdline | tr ' ' '\n' | while read param; do
        if [[ "$param" == root=* ]]; then
            echo ${param#root=}
    executable: /bin/bash
  register: boot_param_root


UPD androidul on reddit suggested to use file or template lookups for embedding long scripts. IMHO that’s a good idea for really large scripts, but for 5-7 lines YAML multi-line is more readable.

- name: large shell script
  shell: "{{ lookup('template', 'large_script.j2') }}"
    executable: /bin/bash
  register: large_script_result

License: This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License Share: share on reddit share on hacker news share on facebook share on twitter share on linkedin share on slashdot