我的比赛如下:
---
- 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"}
我只是不确定为什么string_to_echo显示为未定义。 任何建议将不胜感激
我尝试了各种组合。我尝试将shell命令放置在该角色的实际任务中:influxdb-upgrade。但是,这会产生相同的错误。
答案 0 :(得分:1)
在戏剧中,角色在任务执行之前执行。当执行 roles:语句时,尚未声明变量 string_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
给予
PLAY [localhost] ****************************************************************************
TASK [role1 : debug] ************************************************************************
ok: [localhost] => {
"test_var": "VARIABLE IS NOT DEFINED!"
}
TASK [set_fact] *****************************************************************************
ok: [localhost]
TASK [debug] ********************************************************************************
ok: [localhost] => {
"test_var": "test"
}
PLAY RECAP **********************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
使用include_role来更改工作流程以满足您的需求。
- hosts: localhost
gather_facts: false
tasks:
- set_fact:
test_var: test
- debug:
var: test_var
- include_role:
name: role1
when: test_var == 'test'