Ansible-在SQL查询中应用变量

时间:2018-10-05 10:36:58

标签: ansible ansible-2.x

- 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。请提出建议。

1 个答案:

答案 0 :(得分:0)

在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