【问题标题】:Ansible Undefined variable in vars_files item but the variable was defined using set_factvars_files 项中的 Ansible 未定义变量,但该变量是使用 set_fact 定义的
【发布时间】:2022-01-27 09:26:16
【问题描述】:

我正在尝试根据 Ansible 将部署这些文件的计算机上存在的主板版本来选择一个本地文件。

所以我的方法是使用格式为 {"" : ".yml"} 的字典。并使用此字典填充一个新变量(此处为pcu_config),该变量最终将存储 ansible 使用的文件名。到目前为止,我的实现中显示了我的未定义变量错误

vars_files:
    - "{{ playbook_dir }}/pcu_config/{{ pcu_config }}"

但正如在播放之前的输出图像中的 debug msg 中可以看到的,pcu_config 确实已定义(并且也是正确的文件名)。

- name: Find the pcu_config
  hosts: vehicle
  gather_facts : no
  vars:
    pcu_config_dict:
      VirtualBox: virtual.yml
      n1: n1.yml
      n2: n2.yml
      n3: n3.yml
      n4a: n4a.yml
      n4b: n4b.yml
      
  tasks:
        - name: Find the motherboard name
          shell: cat /sys/devices/virtual/dmi/id/board_name
          register: board_name

        - name: Find the motherboard version
          shell: cat /sys/devices/virtual/dmi/id/chassis_version
          register: board_version

        - debug:
            msg: "board_name : {{ board_name.stdout }}, board_version: {{ board_version.stdout }}, {{pcu_config_dict}} "

        - name: Assign the PCU_config file for other than n4's
          set_fact:
            pcu_config: '{{pcu_config_dict[ board_name.stdout | default("this cpu does not exist in the dict")] | default("") }}'
          when: board_name.stdout != "n4"

        - name: Assign the PCU_config file for n4
          set_fact:
            pcu_config: '{{pcu_config_dict[board_version.stdout | default("this cpu_version does not exist in the dict")] | default("") }}'
          when: board_name.stdout == "n4"


- name : Check pcu_config is available elsewhere in playbook
  hosts: vehicle
  tasks: 
    - debug:
        msg: "{{ pcu_config }}, {{ playbook_dir }}"

- name: Deploy software to vehicle
  hosts: vehicle
  vars_files:
    - "{{ playbook_dir }}/pcu_config/{{ pcu_config }}"
    - "{{ playbook_dir }}/os_config/deb-files-cache.yml"
    - "{{ playbook_dir }}/os_config/python_dep.yml"

  roles:
      - role: logger_network
        tags: base

这是我在虚拟机上运行此 playbook 时的输出。

欢迎提出解决X或Y问题的建议。(参考@Zeitounator的评论)

【问题讨论】:

  • 来自我们的 [提问页面]:请勿发布代码、数据、错误消息等的图片 - 复制或输入问题中的文本。请保留将图像用于图表或演示渲染错误,无法通过文本准确描述的事情。如需更多信息,请参阅元常见问题解答条目Why not upload images of code/errors when asking a question?
  • 另外,从错误看来,未定义的变量在文件"{{ playbook_dir }}/pcu_config/{{ pcu_config }}" 中,因为您没有提供这些文件,所以回答您会很复杂。
  • since ansible doesn't allow using vars inside tasks ansible_board_name、ansible_board_version 等中可用时,为什么要在目标文件上发布 cat ?
  • 如果ansible doesn't allow using vars inside tasks 你的意思是它不允许vars_files 这是真的。但是你可以在播放过程中使用include_vars。老实说,您应该检查您的问题并添加更多详细信息和上下文(更具体地说,是您的 var 文件的示例内容),因为我有一种非常强烈的感觉,这可以以更简单的方式解决。见x/y problem
  • @β.εηοιτ.βε 我发现输出的图像更具可读性。我会在使用屏幕阅读器的读者,或在手机上使用 SO 或无法访问图像网站的读者中抓住机会。此外,输入代码的格式正确,以防有人想要复制或编辑它。因此,我们不要将建议变成规则,并针对不同情况使用公平推理。

标签: linux ansible ansible-facts


【解决方案1】:

不是最好的方法,而是使用cacheable: yes 访问剧本中其他地方set_facts 设置的变量。

        - name: Assign the PCU_config file for other than n4's
          set_fact:
            pcu_config: '{{pcu_config_dict[ board_name.stdout | default("this cpu does not exist in the dict")] | default("") }}'
            cacheable: yes
          when: board_name.stdout != "n4"

        - name: Assign the PCU_config file for n4
          set_fact:
            pcu_config: '{{pcu_config_dict[board_version.stdout | default("this cpu_version does not exist in the dict")] | default("") }}'
            cacheable: yes
          when: board_name.stdout == "n4"

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2015-01-21
    • 2014-10-09
    相关资源
    最近更新 更多