从剧本访问local_tmp

时间:2018-08-25 05:06:10

标签: ansible

我一直在设置必须下载的每个临时文件的目标路径。例如:

- name: Downloading file
  uri:
    url: "{{url}}"
    dest: ~/.ansible/tmp/

是否可以从剧本中读取配置变量local_tmp(和remote_tmp)?

1 个答案:

答案 0 :(得分:0)

我不确定您为什么要将临时文件放在Ansible处理的local_tmp / remote_tmp目录下。

有一个名为tempfile的模块,它允许您在系统临时目录中指定的路径下创建一个临时目录:

- name: Ensure a temporary directory for download exists
  tempfile:
    state: directory
    suffix: my_download
  register: temp_dir

- name: Ensure a file is downloaded from {{ url }}
  uri:
    url: "{{ url }}"
    dest: "{{ temp_dir.path }}"

,如果您想在整个剧本运行中保留名称,只需在lookup('env', 'TMPDIR') | default('/tmp')中为本地创建子目录,或在ansible_env.TMPDIR | default('/tmp')中为远程创建子目录。