我正在尝试执行 Ansible play 来执行一条 MySQL 语句,该语句插入一个 JSON 对象,其中所有键和值都是字符串。但是,使用我当前的代码,生成的 JSON 在键和值周围没有双引号。
当前代码:
- name: Execute SQL to update entries for client_id 'test')
shell: >
mysql {{ mysql_credentials_file_cmdline_parameter }} -s -N -e
"UPDATE test_db.test SET app_home_url = '{{id_dns}}',
app_callback_urls = '{"notification_callback_urls":{"app_novuid_registration":"{{id_dns}}/user_registration","forgot_password":"{{id_dns}}/password_reset"},"user_attribute_callback_url":"{{id_dns}}/user_updated","check_switch_callback_url":"{{id_dns}}/email_switched"}',
app_dns = '{{id_dns}}'
WHERE client_id = 'test';"
但是,当我检查 MySQL 中的结果时得到的结果不包含字符串周围的双引号。
{ notification_callback_urls:
{ app_novuid_registration : https://dns/user_registration,
forgot_password : https://dns/password_reset
},
user_attribute_callback_url : https://dns/user_updated,
check_switch_callback_url : https://dns/email_switched
}
相反,我想要这个:
{"notification_callback_urls":
{ "app_novuid_registration" : "https://dns/user_registration",
"forgot_password" : "https://dns/password_reset"
},
"user_attribute_callback_url" : "https://dns/user_updated",
"check_switch_callback_url" : "https://dns/email_switched"
}
我尝试转义双引号:
- name: Execute SQL to update entries for client_id 'test')
shell: >
mysql {{ mysql_credentials_file_cmdline_parameter }} -s -N -e
"UPDATE test_db.test SET app_home_url = '{{id_dns}}',
app_callback_urls = '{\"notification_callback_urls\":{\"app_novuid_registration\":\"{{id_dns}}/user_registration\",\"forgot_password\":\"{{id_dns}}/password_reset\"},\"user_attribute_callback_url\":\"{{id_dns}}/user_updated\",\check_switch_callback_url\":\"{{id_dns}}/email_switched\"}',
app_dns = '{{id_dns}}'
WHERE client_id = 'test';"
但是,在运行时我收到一个一般错误,看起来尝试用 \" 转义双引号会导致运行的命令中出现 3 个斜杠。
TASK [Execute SQL to update entry] *********************************************
fatal: [localhost]: FAILED! =>
{
"changed": true,
"cmd":
"mysql {{ mysql_credentials_file_cmdline_parameter }} -s -N -e
"UPDATE test_db.test SET app_home_url = 'dns',
app_callback_urls = '{\\\"notification_callback_urls\\\":{\\\"app_novuid_registration\\\":\\\"dns/user_registration\\\",\\\"forgot_password\\\":\\\"dns/password_reset\\\"},\\\"user_attribute_callback_url\\\":\\\"dns/user_updated\\\",\\\"check_switch_callback_url\\\":\\\"dns/email_switched\\\"}',
app_dns = 'dns'
WHERE client_id = 'test';"
又快又懒的你可以尝试从
>
改为|
(文字风格)
一个最小的示例手册
---
- hosts: localhost
become: false
gather_facts: false
vars:
id_dns: DNS
mysql_credentials_file_cmdline_parameter: TEST
tasks:
- debug:
msg: |
mysql {{ mysql_credentials_file_cmdline_parameter }} -s -N -e
"UPDATE test_db.test SET app_home_url = '{{ id_dns }}',
app_callback_urls = '{"notification_callback_urls":{"app_novuid_registration":"{{ id_dns }}/user_registration","forgot_password":"{{ id_dns }}/password_reset"},"user_attribute_callback_url":"{{ id_dns }}/user_updated","check_switch_callback_url":"{{ id_dns }}/email_switched"}',
app_dns = '{{ id_dns }}'
WHERE client_id = 'test';"
将产生
的输出TASK [debug] *************************************
ok: [localhost] =>
msg: |-
mysql TEST -s -N -e
"UPDATE test_db.test SET app_home_url = 'DNS',
app_callback_urls = '{"notification_callback_urls":{"app_novuid_registration":"DNS/user_registration","forgot_password":"DNS/password_reset"},"user_attribute_callback_url":"DNS/user_updated","check_switch_callback_url":"DNS/email_switched"}',
app_dns = 'DNS'
WHERE client_id = 'test';"
由于该任务还有很大的改进空间,因此您可以从更改格式开始以摆脱内联 JSON 字符串。
---
- hosts: localhost
become: false
gather_facts: false
vars:
DNS: DNS
CREDENTIALS: TEST
URLs: {
"notification_callback_urls": {
"app_novuid_registration": "https://{{ DNS }}/user_registration",
"forgot_password": "https://{{ DNS }}/password_reset"
},
"user_attribute_callback_url": "https://{{ DNS }}/user_updated",
"check_switch_callback_url": "https://{{ DNS }}/email_switched"
}
tasks:
- debug:
msg: |
mysql {{ CREDENTIALS }} -s -N -e
"UPDATE test_db.test SET app_home_url = '{{ DNS }}',
app_callback_urls = {{ URLs | to_json }},
app_dns = '{{ DNS }}
WHERE client_id = 'test';"
产生
的输出TASK [debug] *************************************
ok: [localhost] =>
msg: |-
mysql TEST -s -N -e
"UPDATE test_db.test SET app_home_url = 'DNS',
app_callback_urls = {"notification_callback_urls": {"app_novuid_registration": "https://DNS/user_registration", "forgot_password": "https://DNS/password_reset"}, "user_attribute_callback_url": "https://DNS/user_updated", "check_switch_callback_url": "https://DNS/email_switched"},
app_dns = 'DNS
WHERE client_id = 'test';"