gpt4 book ai didi

ansible - 读取使用ansible的tempfile模块创建的目录名称

转载 作者:行者123 更新时间:2023-12-02 03:37:00 24 4
gpt4 key购买 nike

我编写了一个简单的ansible脚本来创建一个临时目录,并希望将该目录的名称保存到一个变量中。我的 .yml 文件是:

- hosts: " {{ lookup('env', 'HOSTNAME') }} "
tasks:
- name : Create staging directory
tempfile:
state: directory
suffix: staging
path: "{{ lookup('env', 'HOME') }}"
become: true
register: output

- name: print stdout
debug: msg="{{ output }}"

运行上面的输出会打印一个dict

$ ansible-playbook -i hosts tempfile.yml  

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

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

TASK [Create staging directory] ***************************************************************************
changed: [localhost]

TASK [print stdout] ***************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0700",
"owner": "root",
"path": "/home/xxxx/ansible.Fb7rbKstaging",
"size": 4096,
"state": "directory",
"uid": 0
}
}

PLAY RECAP ************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0

如何访问 some_dict['localhost']['msg']['path']?我查找了 hostvars 变量,确实在其中看到了我的临时目录,但不知道如何访问它。

最佳答案

检查Registered variables文档中的部分以获取更多详细信息,从您的示例register:output中,您可以使用如下方式访问路径:

 - name: print stdout
debug:
msg: "{{ output.path }}"

关于ansible - 读取使用ansible的tempfile模块创建的目录名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49864943/

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