我编写了一个简单的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 }}"
Run Code Online (Sandbox Code Playgroud)
运行上面的输出会打印一个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
Run Code Online (Sandbox Code Playgroud)
我如何访问some_dict['localhost']['msg']['path']?我查找了该hostvars变量,确实在其中看到了我的临时目录,但不知道如何访问它。
检查文档中的注册变量register: output部分以获取更多详细信息,从您的示例中,您可以使用如下方式访问路径:
- name: print stdout
debug:
msg: "{{ output.path }}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5813 次 |
| 最近记录: |