Ansible 填充变量供全局使用
Ansible populate variable for global use
我这周遇到了 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
调试输出 returns 第一个块的预期值,但是一旦执行第二个块,我就会收到此错误:
"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 的 special variable hostvars
访问其他主机的变量,如:
- debug:
var: hostvars['Healthcheck_Host'].stable_cl_to_deploy
我这周遇到了 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
调试输出 returns 第一个块的预期值,但是一旦执行第二个块,我就会收到此错误:
"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 的 special variable hostvars
访问其他主机的变量,如:
- debug:
var: hostvars['Healthcheck_Host'].stable_cl_to_deploy