为什么 ansible 返回“ansible_local”未定义
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态导入(
roles:
和import_role
)发生在剧本解析时,因此用于导入操作的所有变量(例如在文件名中)必须在此时可用观点。事实是在剧本执行期间收集的,因此不能这样使用它们。 (通过启用事实缓存并确保缓存永不过期,可能可以实现此目的,但这是一个脆弱的前景。)您应该使用 动态包含,将在执行期间处理,而不是在执行期间处理解析。
Static imports (
roles:
andimport_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.