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

标签 ansible

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

相关文章:

python-2.7 - 我有一个可靠的文件。我只想在 flask API 中运行 ansible 文件

ubuntu - 在 Ansible 中比较 Ubuntu 主要和次要版本

ansible - 剧本检查

Ansible - 将库存主机和变量读取到 group_vars/all 文件

ansible - 使用带方括号的文件名时,带有 'creates' 的命令不是幂等的

regex - 如何在ansible lineinfile regexp中指定不区分大小写的模式?

centos - 如何使用 Ansible 删除 ufw 中预定义的 mDNS 规则?

ansible - 在 ansible 中,我可以为模块使用变量而不是使用模块名称吗?

ansible - 如何重试可能失败的 Ansible 任务?

python - 无需互联网访问即可在 python 中使用 Docker 组合