0

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?

carbolymer
  • 1,439
  • 1
  • 15
  • 30
  • 1
    That YAML is fine, so there is nothing you have to do to make it work. The problem is in the interpretation of your scalars, outside of YAML: balance your double quotes (hint: look at the last line of your block style literal scalar) – Anthon Dec 06 '18 at 11:29

1 Answers1

3

It is a very misleading error message, but your problem is actually that double-quote at the end of /dev/null"

mdaniel
  • 31,240
  • 5
  • 55
  • 58