gpt4 book ai didi

ansible - 在ansible中获取Caller Playbook目录名

转载 作者:行者123 更新时间:2023-12-04 17:32:57 26 4
gpt4 key购买 nike

我有两个剧本 pb1.ymldir/pb2.yml。我正在从 pb1.yml 剧本中调用 pb2.yml。我需要打印调用 pb2.yml 剧本的目录的名称。

cat pb1.yml
---
- hosts: localhost
tasks:
- debug: var={{ playbook_dir |basename }}

- import_playbook: dir/pb2.yml

cat dir/pb2.yml
---
- hosts: localhost
tasks:
- debug: var={{ playbook_dir |basename }}

在下面的示例中,我能够获取 pb2.yml 的目录名称,但我有兴趣从 中知道 pb1.yml 的目录名称>pb2.yml

home/monk>ansible-playbook pb1.yml
PLAY [localhost] *******************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"samples": "VARIABLE IS NOT DEFINED!"
}

PLAY [localhost] *******************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"dir": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0

预期结果:

从pb2.yml,打印/home/monk/

最佳答案

要么变量被正确扩展并且使用参数msg打印值

  - debug:
msg: "{{ playbook_dir|basename }}"

,或者变量用参数var打印,不展开

  - debug:
var: playbook_dir|basename


说明

代码 {{ playbook_dir|basename }} 分别计算为“sample”或“dir”

tasks:
- debug: var={{ playbook_dir|basename }}

这样的变量没有定义,播放会失败

"samples": "VARIABLE IS NOT DEFINED!

引自debug

var: A variable name to debug.Mutually exclusive with the msg option. Be aware that this option already runs in Jinja2 context and has an implicit {{ }} wrapping, so you should not be using Jinja2 delimiters unless you are looking for double interpolation.

“双插值”其实就是“间接寻址”。下面的戏

- hosts: localhost
vars:
x: y
y: z
tasks:
- debug:
var: "{{ x }}"

给予

ok: [localhost] => {
"y": "z"
}

关于ansible - 在ansible中获取Caller Playbook目录名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com