Ansible 填充变量以供全球使用

新2Python

我这周有 Ansible 变量地狱。我需要在运行时动态填充一个变量,然后它需要可用于多个任务。

myplaybook.yml

---
- hosts: Healthcheck_Host 
  gather_facts: no
  become: "{{ True if deploy_user != '{{ deploy_user }}' else False }}"
  become_user: "{{ deploy_user }}"
  tasks:

    - name: "Get latest installed CL on groups['Healthcheck_Host'][0]"
      shell: |
        grep -oP '(?<=\:)(.*?)(?=\-)' {{ latest_deployed_build_dir.stdout }}/thebuildinfo.txt
      register: latest_stable_cl

    - debug:
        var: latest_stable_cl.stdout

    - name: Assign CL value from HC host
      set_fact:
        stable_cl_to_deploy: "{{ latest_stable_cl.stdout }}"
        cacheable: yes

- hosts: Appserver
  gather_facts: no
  become: "{{ True if deploy_user != '{{ deploy_user }}' else False }}"
  become_user: "{{ deploy_user }}"
  tasks:

    - debug:
        var: stable_cl_to_deploy

在 /etc/ansible/ansible.cfg 我确实有这个设置:fact_caching = memory

调试输出返回第一个块的预期值,但是一旦执行第二个块,我就会收到此错误:

"msg": "The task includes an option with an undefined variable. The error was: 'stable_cl_to_deploy' is undefined\n\nThe error appears to have been in '/some/path/here/to/my/myplaybook.yml': line 30, 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: Set variables for tools package if deploying stable CL\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'stable_cl_to_deploy' is undefined"

任何帮助表示赞赏。谢谢

月亮

set_fact设置特定于主机的事实。在示例中,stable_cl_to_deploy被添加到Healthcheck_Host和 中引用的事实Appserver,因此出现错误。

使用 ansible 的特殊变量 hostvars来访问其他主机的变量,例如:

- debug:
    var: hostvars['Healthcheck_Host'].stable_cl_to_deploy

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章