我编写了一个简单的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/