Reputation: 55
I have a DB tool that I want to call in ansible. The DB tool can take SQL commands passed in between quotes. The problem is that I cannot get the escapes right for this work.
The command would like this in a normal interactive way (as linuxuser)
dbtool -u DBUSER -p DBPWD "backup data using file ('/backup/path/prefi_')"
I tried something like this in ansible
tasks:
- name: something
become: yes
become_method: sudo
shell: "runuser -l {{ linuxuser }} -c '/path/to/dbtool/dbtool -u DBUSER -p DBPWD \"backup data using file ('/backup/path/prefix_')\"'"
I tried escaping different ways, the dbtool just complains about the " or the ' one way or another.
Any hint would be appreciated.
PS: I cannot use become_user: {{ linuxuser }} because of other reasons.
PS2: dbtool only works as {{ linuxuser }} because of the various libraries in environment.
Upvotes: 1
Views: 27
Reputation: 33203
You'll likely be interested in the |quote
filter to get you out of shell quoting hell
tasks:
- name: something
become: yes
become_method: sudo
shell: runuser -l {{ linuxuser }} -c {{ my_command | quote }}
vars:
my_command: /path/to/dbtool/dbtool -u DBUSER -p DBPWD "backup data using file ('/backup/path/prefix_')"
Be aware that you don't need the outer "
in your shell:
scalar since it doesn't start with any yaml-special characters, and the same story for my vars:
declaration for the same reason
be aware: I don't have runuser
to actually test that, but I believe it is accurate
Upvotes: 1