1

我试图在我的converge步骤和我cleanup的分子步骤之间共享一个变量。由于两个剧本都在同一台主机上运行,​​我想我可以使用事实将变量缓存为事实。

converge.yml

- name: Cache some variable
    set_fact:
      cacheable: yes
      my_fact: "howdy, world"

cleanup.yml

- debug:
    msg: "{{ my_fact }}"

在我的顶层ansible.cfg我指定:

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
fact_caching_timeout = 7200

我第一次运行molecule converge,然后运行molecule cleanup我收到错误:(重新格式化)

TASK [debug] *******************************************************************
fatal: [cluster-toolchain]: FAILED! => {"msg": "The task includes an option
with an undefined variable. The error was: 'my_fact' is undefined
The error appears to be in '{REDACTED}/cleanup.yml': line 5, column 7, but may be
elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:\n\n  tasks:\n    - debug:\n      ^ here\n"}

我有一些猜测,比如可能 Molecule 没有看到我的顶级水平ansible.cfg或其他什么。

无论如何,我希望在这里得到一些帮助。也许有更好的方法来分享分子世界中的变量。

4

1 回答 1

1

Molecule 是一个测试 Ansible 角色的工具。它不是针对执行 Ansible 剧本的。通常的命令是ansible-playbook.

Molecule 无法读取您ansible.cfg的事实缓存设置。如果要使命令正常工作,请在以下位置添加设置molecule.yml

provisioner:
  name: ansible
  config_options:
    defaults:
      gathering: smart
      fact_caching: jsonfile
      fact_caching_connection: /tmp/facts_cache
      fact_caching_timeout: 7200

然后molecule converge给出molecule cleanup

PLAY [Cleanup] ************************************************************************************************************************

TASK [debug] **************************************************************************************************************************
ok: [vagrant-instance] => {
    "msg": "howdy, world"
}
于 2020-12-18T22:54:18.767 回答