Essentially, I am trying to use the when conditional to decide whether to proceed running a command based on the presence of a string. There seem to be all sorts of problems to overcome here, including the presence of the colon which I can't escape with "{{:}}"
like I normally would because then the conditional complains.
If i truncate everything from the point of the colon and after, I get an integer evaluation error instead. I've looked through 200 posts on stackoverflow and none of the solutions seem to be able to do what I'm looking for.
Relvevant part of the sync_status.stderr_lines contents I am looking for if it comes up:
ERROR 1227 (42000) at line 1: Access denied
The code in the task which tries to run the command if the string is present with the colon:
- name: Can we proceed?
command: uptime
when: ( 'ERROR 1227 (42000) at line 1"{{:}}" Access denied' in sync_status.stderr_lines )
This task's error during runtime:
fatal: [server1]: FAILED! => {"msg": "The conditional check '( 'ERROR 1227 (42000) at line 1\"{{:}}\" Access denied' in sync_status.stderr_lines )' failed. The error was: template error while templating string: unexpected ':'. String: ( 'ERROR 1227 (42000) at line 1\"{{:}}\" Access denied' in sync_status.stderr_lines )\n\nThe error appears to be in '/home/ansible/ssn-project/playbooks/i_mux-sql-upgrade.yml': line 54, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Can we proceed?\n ^ here\n"}
Now the "same" thing truncated:
- name: Can we proceed?
command: uptime
when: ( 'ERROR 1227 (42000) at line 1' in sync_status.stderr_lines )
And it's runtime failure as well:
fatal: [mux-ds1]: FAILED! => {"msg": "The conditional check '( 'ERROR 1227 (42000) at line 1' in sync_status.stderr_lines )' failed. The error was: error while evaluating conditional (( 'ERROR 1227 (42000) at line 1' in sync_status.stderr_lines )): Unable to look up a name or access an attribute in template string ({% if ( 'ERROR 1227 (42000) at line 1' in sync_status.stderr_lines ) %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'AnsibleUndefined' is not iterable\n\nThe error appears to be in '/home/ansible/ssn-project/playbooks/i_mux-sql-upgrade.yml': line 54, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Can we proceed?\n ^ here\n"}
Is there a way to make this work as I expect? I was maybe thinking of some sort of regex filter, but maybe that's not the right way to go. Insights would be appreciated. Note, I am on ansible version 2.9