我这周有 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"
任何帮助表示赞赏。谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句