为什么 ansible 返回“ansible_local”未定义

发布于 2025-01-13 13:59:00 字数 1302 浏览 4 评论 0原文

我的 ansible_local 变量有问题: 我在我的ansible目标上定义了以下facts.d

[role]
version=roles/version

,我可以使用ansible设置模块或以下剧本

- name: "test"

  hosts: "{{ myhost }}"
  gather_facts: yes

  vars:

    my_role_path: "{{ ansible_local.test.role.version }}"

  tasks:

      - name: debug
        ansible.builtin.debug:
           msg: "{{ my_role_path }}"
      

标准输出成功访问此事实:

TASK [debug] **************************************************************************************************
ok: [rhel8-test-2] => {
    "msg": "roles/version"
}

PLAY RECAP **************************************************************************************************
rhel8-test-2               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

现在,我想使用var my_role_path来调用位于“中的名为“echo”的角色my_role_path” 我的 ansible 服务器的文件夹。

我添加以下语句:

 tasks:
    - name: debug
      ansible.builtin.debug:
        msg: "{{ my_role_path }}"


  roles:

      - "{{ my_role_path }}/echo"



现在 ansible 返回:

ERROR! {{ ansible_local.test.role.version }}: 'ansible_local' is undefined

这是我的变量的命名空间或范围的问题吗?欢迎任何线索。 谢谢 。

I have a problem with ansible_local variable:
I have the following facts.d defined on my ansible target

[role]
version=roles/version

I can access successfully to this fact with ansible setup module or with the following playbook

- name: "test"

  hosts: "{{ myhost }}"
  gather_facts: yes

  vars:

    my_role_path: "{{ ansible_local.test.role.version }}"

  tasks:

      - name: debug
        ansible.builtin.debug:
           msg: "{{ my_role_path }}"
      

stdout:

TASK [debug] **************************************************************************************************
ok: [rhel8-test-2] => {
    "msg": "roles/version"
}

PLAY RECAP **************************************************************************************************
rhel8-test-2               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Now, I want to use the var my_role_path to call a role called "echo" located in "my_role_path" folder of my ansible server.

I add the following statement:

 tasks:
    - name: debug
      ansible.builtin.debug:
        msg: "{{ my_role_path }}"


  roles:

      - "{{ my_role_path }}/echo"



Now ansible returns :

ERROR! {{ ansible_local.test.role.version }}: 'ansible_local' is undefined

Is it a problem of namespace or scope of my variable ? any clues are welcome.
Thank you .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫轩蝶泪 2025-01-20 13:59:00

静态导入(roles:import_role)发生在剧本解析时,因此用于导入操作的所有变量(例如在文件名中)必须在此时可用观点。事实是在剧本执行期间收集的,因此不能这样使用它们。 (通过启用事实缓存并确保缓存永不过期,可能可以实现此目的,但这是一个脆弱的前景。)

您应该使用 动态包含,将在执行期间处理,而不是在执行期间处理解析。

 tasks:
    - name: debug
      ansible.builtin.debug:
        msg: "{{ my_role_path }}"

    - ansible.builtin.include_role:
        name: "{{ my_role_path }}/echo"

Static imports (roles: and import_role) happen at playbook parsing time, so all variables used for the action of importing (for example in the filename) have to be available at that point. Facts are gathered during playbook execution, so they cannot be used like this. (It might be possible to get this working by enabling fact caching and making sure the cache never expires, but that's a fragile prospect.)

You should instead use a dynamic include, which will be processed during execution instead of during parsing.

 tasks:
    - name: debug
      ansible.builtin.debug:
        msg: "{{ my_role_path }}"

    - ansible.builtin.include_role:
        name: "{{ my_role_path }}/echo"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文