在另一个主机和另一个角色上定义的ansible重用变量

时间:2018-02-23 09:31:16

标签: variables ansible

我想在主机上注册一个变量,然后在另一个主机中重用另一个角色。

我的main.yml

- hosts: icinga
  user: test

  tasks:
    - name: generetaing tickets for monitoring hosts
      command: /usr/sbin/icinga2 pki ticket --cn '{{ db01_hostname }}'
      register: pkidb01

    - set_fact:
        test: "{{ pkidb01 }}"

    - debug: msg="{{ pkidb01.stdout }}"
    - debug: msg="{{ test.stdout }}"

- hosts: dbserver02
  user: test

  tasks:
    - debug: msg="{{ hostvars['icinga']['pkidb01']['stdout'] }}"        
    - debug: msg="{{ pkidb01 }}"
    - debug: msg="{{ hostvars['icinga'] }}"

var pkidb01已正确注册,我可以使用set_fact访问它,也可以使用同一主机上的普通变量访问它。 但是在下一个主机上,我无法访问它们。

输出1.host:

TASK [generetaing tickets for monitoring hosts] *********************************************************************************************************************************************************************************************
Friday 23 February 2018  10:23:22 +0100 (0:00:09.593)       0:00:09.620 *******
changed: [10.10.10.100]

TASK [set_fact] *****************************************************************************************************************************************************************************************************************************
Friday 23 February 2018  10:23:23 +0100 (0:00:01.521)       0:00:11.142 *******
ok: [10.10.10.100]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
Friday 23 February 2018  10:23:23 +0100 (0:00:00.040)       0:00:11.182 *******
ok: [10.10.10.100] => {
    "msg": "08fcba18866e563dcded00e43637fec6dbb025e8"
}

TASK [debug] ********************************************************************************************************************************************************************************************************************************
Friday 23 February 2018  10:23:24 +0100 (0:00:00.039)       0:00:11.222 *******
ok: [10.10.10.100] => {
    "msg": "08fcba18866e563dcded00e43637fec6dbb025e8"
}

输出2.host:

TASK [debug] ********************************************************************************************************************************************************************************************************************************
Friday 23 February 2018  10:23:33 +0100 (0:00:09.293)       0:00:20.558 *******
fatal: [10.10.10.30]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['icinga']\" is undefined\n\nThe error appears to have been in '/main.yml': line 45, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - debug: msg=\"{{ hostvars['icinga']['pkidb01']['stdout'] }}\"\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: \"hostvars['icinga']\" is undefined"}

HOSTFILE:

[dbserver01]
10.10.10.30

[icinga]
10.10.10.100

这里的解决方案没有用: Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook

1 个答案:

答案 0 :(得分:2)

抱歉,我误解了你的问题,你的问题是你没有定义你的hostvar。您必须使用add_host修改ansible-playbook in-memory inventory

- hosts: icinga
  user: test

  tasks:
    - name: generetaing tickets for monitoring hosts
      command: /usr/sbin/icinga2 pki ticket --cn '{{ db01_hostname }}'
      register: pkidb01

    - set_fact:
         test: "{{ pkidb01 }}"

    - debug: msg="{{ pkidb01.stdout }}"
    - debug: msg="{{ test.stdout }}"

    - name: "dummy HostVar"
      add_host:
        name: "myVar"
        value: "{{ pkidb01.stdout }}"


- hosts: dbserver02
  user: test

  tasks:
    - debug: msg="{{ hostvars['myVar']['value'] }}"