Ansible - 在 SQL 查询中应用变量
Ansible - apply variable in SQL query
- hosts: localhost
vars_prompt:
- name: " username"
prompt: "What is merchant name?"
private: no
register: reg_name
- name: "password"
prompt: "Enter password"
private: yes
encrypt: "sha256_crypt"
confirm: yes
salt_size: 16
register: s
tasks:
- debug:
var: password
register: s
- debug: var=s
- name: update db
shell : echo -n "insert into database (id, createdDate, updatedDate, username, sharedSecretHash) values (7, strftime('%s', 'now'), strftime('%s', 'now'), "{{ reg_name.stdout }}", "{{ s.stdout }}");" | sqlite3 /tmp/test.db
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'reg_name' is undefined\n\nThe error appears to have been in '/home/ansible/prompt.yml': line 33, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: update db\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'merchantname' is undefined"}
fatal: [10.1.1.1: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'reg_ name' is undefined\n\The error appears to have been in '/home/ansible/prompt.yml': line 33, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: update db\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'merchantname' is undefined"}
我用 reg_name.stdout 作为用户名和 s.stdout 作为密码注册了变量,并试图在 sql 查询中应用它,它抛出错误 - var undefined。请提出建议。
在 vars_prompt 中,内容存储在同名变量中(在本例中为用户名和密码)。无需使用属性寄存器。实际上,它被ansible忽略了。
这本剧本对我有用:
- hosts: localhost
vars_prompt:
- name: " username"
prompt: "What is merchant name?"
private: no
- name: "password"
prompt: "Enter password"
private: yes
encrypt: "sha256_crypt"
confirm: yes
salt_size: 16
tasks:
- debug: var=s
- name: update db
shell : echo -n "insert into database (id, createdDate, updatedDate, username, sharedSecretHash) values (7, strftime('%s', 'now'), strftime('%s', 'now'), "{{ username }}", "{{ password }}");" | sqlite3 /tmp/test.db
- hosts: localhost
vars_prompt:
- name: " username"
prompt: "What is merchant name?"
private: no
register: reg_name
- name: "password"
prompt: "Enter password"
private: yes
encrypt: "sha256_crypt"
confirm: yes
salt_size: 16
register: s
tasks:
- debug:
var: password
register: s
- debug: var=s
- name: update db
shell : echo -n "insert into database (id, createdDate, updatedDate, username, sharedSecretHash) values (7, strftime('%s', 'now'), strftime('%s', 'now'), "{{ reg_name.stdout }}", "{{ s.stdout }}");" | sqlite3 /tmp/test.db
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'reg_name' is undefined\n\nThe error appears to have been in '/home/ansible/prompt.yml': line 33, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: update db\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'merchantname' is undefined"}
fatal: [10.1.1.1: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'reg_ name' is undefined\n\The error appears to have been in '/home/ansible/prompt.yml': line 33, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: update db\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'merchantname' is undefined"}
我用 reg_name.stdout 作为用户名和 s.stdout 作为密码注册了变量,并试图在 sql 查询中应用它,它抛出错误 - var undefined。请提出建议。
在 vars_prompt 中,内容存储在同名变量中(在本例中为用户名和密码)。无需使用属性寄存器。实际上,它被ansible忽略了。
这本剧本对我有用:
- hosts: localhost
vars_prompt:
- name: " username"
prompt: "What is merchant name?"
private: no
- name: "password"
prompt: "Enter password"
private: yes
encrypt: "sha256_crypt"
confirm: yes
salt_size: 16
tasks:
- debug: var=s
- name: update db
shell : echo -n "insert into database (id, createdDate, updatedDate, username, sharedSecretHash) values (7, strftime('%s', 'now'), strftime('%s', 'now'), "{{ username }}", "{{ password }}");" | sqlite3 /tmp/test.db