Ansible:如何使用另一个角色的变量进行插值?

时间:2021-08-01 21:59:55

标签: ansible ansible-role

假设我有 2 个角色:solo/base 和 solo/copter,我在 solo/base 中定义了变量:

vars:
  OpenSolo_Release: "https://github.com/OpenSolo/OpenSolo/releases/download/OpenSolo-4.0.0"

作为 defaults/main 的一部分(这允许它默认导入到其他角色中)

在 solo/copter 中,我将它导入并用于变量插值,在默认/main 中我有:

vars:
  ArduCopter_File: "ArduCopter.4.0.1.CubeSolo.apj"

  _ArduCopter_URI: "{{ OpenSolo_Release }}/{{ ArduCopter_File }}" # TODO: declare to be private var

在我有的任务中:

- name: inherit
  import_role:
    name: solo/base

- name: "Download {{ ArduCopter_File }}"
  get_url:
    url: "{{ _ArduCopter_URI }}"
    dest: "{{ tmp }}/{{ ArduCopter_File }}"
    force: no
  delegate_to: localhost
  become: false
  register: result
  until: result is success
  retries: 3
  delay: 2

当我在我的剧本中运行 solo/copter 角色时,我遇到以下错误:

fatal: [instance -> localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: '_ArduCopter_URI' is undefined\n\nThe error appears to be in '/home/peng/git/uavops/roles/solo/copter/tasks/upgrade-autopilot.yml': line 6, 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: \"Download {{ ArduCopter_File }}\"\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

此错误消息似乎忽略了我对 _ArduCopter_URI 的定义,这是怎么发生的?

另外,ansible-lint 没有检测到上述错误,对于静态声明性语言,ansible-lint 似乎比大多数同行弱得多。可以通过某种方式改进吗?

0 个答案:

没有答案