I was following this question How to do multiline shell script in Ansible however, this solution does not work in my a bit complicated example. I am trying to make this piece of YAML working:
- name: "set wifi country code to {{ wifi_country }}"
shell: |
list_wlan_interfaces() {
for dir in /sys/class/net/*/wireless; do
if [ -d "$dir" ]; then
basename "$(dirname "$dir")"
fi
done
}
IFACE="$(list_wlan_interfaces | head -n 1)"
wpa_cli -i "$IFACE" set country {{ wifi_country }}
wpa_cli -i "$IFACE" save_config > /dev/null"
However, I am getting error:
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: list_wlan_interfaces() {
for dir in /sys/class/net/*/wireless; do
if [ -d "$dir" ]; then
basename "$(dirname "$dir")"
fi
done
}
IFACE="$(list_wlan_interfaces | head -n 1)"
wpa_cli -i "$IFACE" set country {{ wifi_country }}
wpa_cli -i "$IFACE" save_config > /dev/null"
The error appears to have been in 'rpi.yml': line 7, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: "set wifi country code to {{ wifi_country }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
How can I place a shell script containing Jinja variables in Ansible playbook?