【发布时间】:2022-01-27 09:26:16
【问题描述】:
我正在尝试根据 Ansible 将部署这些文件的计算机上存在的主板版本来选择一个本地文件。
所以我的方法是使用格式为 {"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
欢迎提出解决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 tasksansible_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