可能 - 包括变量

时间:2018-02-02 18:03:10

标签: ansible

我正在尝试从Ansible中的文件加载变量。我从脚本

运行ansible
./run.sh desupap1 idp1 1

该脚本创建一个包含变量

的文件~/ansible/roles/esup/vars/vars.yml
vars:
  idp_rga: false

取决于$ 3参数。 我正在尝试在任务文件/home/run/ansible/roles/esup/tasks/main.yml

中加载某个主机组的变量
- name: "Sourcing esup variables"
  include_vars:
    dir: '/home/run/ansible/roles/esup/vars'

- name: "DEBUG"
  debug: var=idp_rga

但结果是:

    TASK [esup : Sourcing common variables] ***************************************************************************
task path: /home/run/ansible/roles/esup/tasks/main.yml:1
ok: [desupap1] => {
    "ansible_facts": {
        "vars": {
            "idp_rga": true
        }
    }, 
    "ansible_included_var_files": [
        "/home/run/ansible/roles/esup/vars/vars.yml"
    ], 
    "changed": false
}

TASK [esup : DEBUG] ***********************************************************************************************
task path: /home/run/ansible/roles/esup/tasks/main.yml:5
ok: [desupap1] => {
    "idp_rga": "VARIABLE IS NOT DEFINED!"
}

TASK [esup : Sourcing .bashrc and running start_udp_proxy] ********************************************************
task path: /home/run/ansible/roles/esup/tasks/main.yml:8
fatal: [desupap1]: FAILED! => {
    "msg": "The conditional check 'idp_rga' failed. The error was: error while evaluating conditional (idp_rga): 'idp_rga' is undefined\n\nThe error appears to have been in '/home/run/ansible/roles/esup/tasks/main.yml': line 8, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \"Sourcing .bashrc and running start_udp_proxy\"\n  ^ here\n"
}
    to retry, use: --limit @/home/run/ansible/playbook.retry

1 个答案:

答案 0 :(得分:2)

vars:移除~/ansible/roles/esup/vars/vars.yml。它应该包含的全部是:

idp_rga: false

否则,您尝试定义名为vars的字典,而不是变量idp_rga

由于vars已经是保留字,因此您只能通过vars.vars.idp_rga

访问相关变量