For a specific case I need to echo
a random string returned by ansible's function password_hash
that contains multiple random $
signs (encryption method bcrypt
). The echoed string is then piped into the next command.
Ideally I would just escape the entire string. If that is not possible, I would like to escape all $
signs. How is that possible? If there is an ansible native command that automatically escapes a variable, that would be fine, too.
Full task if needed
- name: Update Admin password
shell: "echo \"update users set passwd={{ zabbix_conf.admin_password|password_hash('bcrypt') }} where username='Admin'\" | mysql --user={{ zabbix_conf.db_user}} --password={{ zabbix_conf.db_password }} zabbix"
Currently doesn't work as expected, because the $
signs affect the echo to be empty.
CodePudding user response:
Ansible already has an inbuilt command for executing mysql queries called mysql_query
. Therefore one can simply do:
- name: Update Admin password
mysql_query:
login_db: "{{ zabbix_conf.db }}"
login_user: "{{ zabbix_conf.db_user}}"
login_password: "{{ zabbix_conf.db_password }}"
query: "update users set passwd='{{ zabbix_conf.admin_password|password_hash('bcrypt') }}' where username='Admin'"
That way you don't need to escape anything and it looks much cleaner, too.
In case someone wants to reproduce this: Ansible requires the installation of passlib
to password_hash
bycrypt
.