如何从ansible结果中获取变量

如何从ansible结果中获取变量,ansible,ansible-playbook,Ansible,Ansible Playbook,我有一个shell脚本,其输出是以下格式的回声 等 我想使用这些变量并运行mysql查询来更新数据库,就像这样 mysql-u-p-h-e'insert-into-test_表值(“variable_1”、“variable_2”、“variable_3”)” 我的ansible剧本是这样的 --- - hosts: infoServers sudo: yes gather_facts: no tasks: - name: gather info script: get_

我有一个shell脚本,其输出是以下格式的回声

我想使用这些变量并运行mysql查询来更新数据库,就像这样
mysql-u-p-h-e'insert-into-test_表值(“variable_1”、“variable_2”、“variable_3”)”

我的ansible剧本是这样的

---
- hosts: infoServers
  sudo: yes
  gather_facts: no
  tasks:
  - name: gather info
    script: get_hostdata.sh
    register: result
  - name: print result
    local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }});'
    with_items: [ result.stdout.split(';')[0], result.stdout.split(';')[1], result.stdout.split(';')[2] ]
错误:加载YAML脚本test_variables.yml时出现语法错误

基本上,我希望能够使用shell命令的输出,将其拆分为一些变量,并能够在进一步的ansible操作中使用这些变量。 你能指导我如何正确地访问变量吗


谢谢

当您遇到这样的错误时,您确实应该提供错误消息的完整详细信息。当我将你的剧本剪切粘贴到一个文件中并试图运行它时,我得到了以下信息:

ERROR: Syntax Error while loading YAML script, ff.yml
Note: The error may actually appear before this position: line 11, column 43

    local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }});'
    with_items: [ result.stdout.split(';')[0], result.stdout.split(';')[1], result.stdout.split(';')[2] ]
                                          ^
因此,如果这与您收到的完整错误相匹配,则表明with_items子句的语法是错误的

我不知道你为什么要用这些东西来做这件事。在这种情况下,您实际上所做的只是一些不必要的变量替换。以下内容也应完全符合您的要求:

- name: print result
    local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ result.stdout.split(';')[0] }}","{{ result.stdout.split(';')[1] }}","{{ result.stdout.split(';')[2] }}");'

您需要正确引用和使用
{}

- hosts: localhost
  tags: s16
  gather_facts: no
  tasks:
  - shell: echo 'variable_1;variable_2;variable_3'
    register: result
  - local_action: debug msg="values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }}");'"
    with_items: [ "{{result.stdout.split(';')[0]}}", "{{result.stdout.split(';')[1]}}", "{{result.stdout.split(';')[2]}}" ]
将打印如下内容:

TASK: [debug msg="values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }}");'"] *** 
ok: [localhost -> 127.0.0.1] => (item=variable_1) => {
    "item": "variable_1", 
    "msg": "values (\"v\",\"a\",\"i\");'"
}
ok: [localhost -> 127.0.0.1] => (item=variable_2) => {
    "item": "variable_2", 
    "msg": "values (\"v\",\"a\",\"i\");'"
}
ok: [localhost -> 127.0.0.1] => (item=variable_3) => {
    "item": "variable_3", 
    "msg": "values (\"v\",\"a\",\"i\");'"
}
正如您在
项[0]、…、项[2]
中所看到的,您将索引到字符串
“variable_1”
中,而不是数组
[“variable_1”、“variable_2”、“variable_3”]

实现这一点的最简单(而且性能更高)方法是:

- hosts: localhost
  tags: s17
  gather_facts: no
  tasks:
  - shell: echo 'variable_1;variable_2;variable_3'
    register: result
- debug: msg="insert into tt values ("{{result.stdout|replace(';', '","')}}");'"

谢谢你的解释。我还有一个疑问-如果我想将result.stdout.split(“;”)[1]的值赋给另一个变量(比如var_1),然后稍后使用var_1检查某些条件等,该怎么办。。完成此任务的正确方法是什么您可以通过task:set_fact:var1:result.stdout.split(“;”)[1]var2:result.stdout.split(“;”)[2]等来完成此任务。我在ansible的最新版本中尝试过此解决方案,并不断被告知“列表对象没有元素1”。在我使用调试打印出相同的split语句(没有数组引用)之前,它肯定会这样做,并将结果打印为3个单独的条目。。。我错过了什么?