Ansible:如何为角色实现条件?(获取错误)
我的剧本如下:Ansible:如何为角色实现条件?(获取错误),ansible,Ansible,我的剧本如下: --- - hosts: influxdbmeta_lab, influxdbdata_lab gather_facts: true become: true tasks: - shell: "docker ps --format '{{.Image}}' | grep influx" register: command_result - name: setting fact set_fact: string_to_echo
---
- hosts: influxdbmeta_lab, influxdbdata_lab
gather_facts: true
become: true
tasks:
- shell: "docker ps --format '{{.Image}}' | grep influx"
register: command_result
- name: setting fact
set_fact: string_to_echo = "{{ command_result.stdout }}"
roles:
- role: influxdb-upgrade
when: (string_to_echo == "--meta")
当我运行此剧本时,出现以下错误:
TASK [influxdb-upgrade : Creating directories] ************************************************************************************************************
fatal: [influxmetalab-1]: FAILED! => {"msg": "The conditional check '(string_to_echo == \"--meta\")' failed. The error was: error while evaluating conditional ((string_to_echo == \"--meta\")): 'string_to_echo' is undefined\n\nThe error appears to be in '/Users/zafaab1/git-repos/deploy-vcp-performance/ansible_home/roles/influxdb-upgrade/tasks/main.yml': line 3, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# Ensures that the directories are there. For upgrade, Ansible will simply mark this as OK\n- name: Creating directories\n ^ here\n"}
我只是不知道为什么字符串_to_echo显示为未定义。
如有任何建议,将不胜感激
我试过各种组合。我尝试将shell命令放置在角色xdb升级的实际任务中。但是,这也会产生同样的错误。在一个剧本中,角色是在执行任务之前执行的。执行roles:语句时,尚未声明变量字符串_to_echo 例如,让我们使用一个只有一个任务的简单角色
$ cat roles/role1/tasks/main.yml
- debug:
var: test_var
下面的剧本
- hosts: localhost
tasks:
- set_fact:
test_var: test
- debug:
var: test_var
roles:
- role1
给予
用于根据您的需要更改工作流
- hosts: localhost
gather_facts: false
tasks:
- set_fact:
test_var: test
- debug:
var: test_var
- include_role:
name: role1
when: test_var == 'test'
这个问题似乎是“金黄”的翻版:我想不是。这里的问题是工作流程。“ansible set\u fact not accessible”的链接显示了错误的语法。@Abdul:如果您发布了完整的输出,问题会立即显现出来。谢谢!!这很有效!!!这里也有类似的解释:
- hosts: localhost
gather_facts: false
tasks:
- set_fact:
test_var: test
- debug:
var: test_var
- include_role:
name: role1
when: test_var == 'test'