I am attempting to use ansible to toggle the encryption state of a specified file (encrypt the file if decrypted, decrypt it if encrypted) using the ansible filter “ternary”.
This is my playbook:
---
# site.yml
- name: site playbook (dummy site)
hosts: localhost
gather_facts: no
vars:
file: test_key_file
cmd_encrypt: ansible-vault encrypt {{ file }}
cmd_decrypt: ansible-vault decrypt {{ file }}
tasks:
- local_action: shell
head -1 {{ file }} | grep -v -q $ANSIBLE_VAULT | ternary( "{{ cmd_encrypt }}" , "{{ cmd_decrypt }}" )
The first part of the shell command was taken from here. I added the ternary part. I have tried various modifications regarding quotes and curly-braces, and substituting values for variables to no avail.
The configuration above results in the error:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
could not find expected ':'
The error appears to be in '/home/sjf/toggle_encryption_test/site.yml': line 16, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- local_action: shell
head -1 {{ file }} | grep -v -q $ANSIBLE_VAULT | ternary( "{{ cmd_encrypt }}" , "{{ cmd_decrypt }}" )
^ 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 }}"
Any help would be appreciated.