030、tempfile模块
本文最后更新于 66 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

tempfile模块

1. 概要

2. 参数

参数 可选值 默认值 说明
path path,临时文件或目录创建的位置路径。如果不指定则会使用系统默认的临时目录路径
prefix "ansible." string,临时文件或目录的前缀
state "file"、"directory" "file" string,创建临时文件还是创建临时目录,默认创建临时文件
suffix "" string,临时文件或目录的后缀

3. 官方示例

- name: Create temporary build directory
  ansible.builtin.tempfile:
    state: directory
    suffix: build

- name: Create temporary file
  ansible.builtin.tempfile:
    state: file
    suffix: temp
  register: tempfile_1

- name: Use the registered var and the file module to remove the temporary file
  ansible.builtin.file:
    path: "{{ tempfile_1.path }}"
    state: absent
  when: tempfile_1.path is defined

4. 剧本的使用

创建剧本文件tempfile.yml:

- hosts: node1
  tasks:
    - name: Create temporary build directory
      ansible.builtin.tempfile:
        state: directory
        suffix: build

    - name: Create temporary file with prefix
      ansible.builtin.tempfile:
        state: file
        prefix: ansible_temp_

    - name: Create temporary file with suffix
      ansible.builtin.tempfile:
        state: file
        suffix: .temp

    - name: Create temporary file with path
      ansible.builtin.tempfile:
        state: file
        # path定义的目录需要存在
        path: /var/log/tempfile
      become: yes

检查剧本语法并执行:

[ansible@master ansible_playbooks]$ ansible-lint tempfile.yml 
[ansible@master ansible_playbooks]$ ansible-playbook tempfile.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [node1] *************************************************************************************************************************************************************

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

TASK [Create temporary build directory] **********************************************************************************************************************************
changed: [node1] => {"changed": true, "gid": 1002, "group": "ansible", "mode": "0700", "owner": "ansible", "path": "/tmp/ansible.dylJ0sbuild", "size": 4096, "state": "directory", "uid": 1002}                                                                                                                                                     

TASK [Create temporary file with prefix] *********************************************************************************************************************************
changed: [node1] => {"changed": true, "gid": 1002, "group": "ansible", "mode": "0600", "owner": "ansible", "path": "/tmp/ansible_temp_lY7kn5", "size": 0, "state": "file", "uid": 1002}                                                                                                                                                             

TASK [Create temporary file with suffix] *********************************************************************************************************************************
changed: [node1] => {"changed": true, "gid": 1002, "group": "ansible", "mode": "0600", "owner": "ansible", "path": "/tmp/ansible.1hP3WN.temp", "size": 0, "state": "file", "uid": 1002}                                                                                                                                                              

TASK [Create temporary file with path] ***********************************************************************************************************************************
changed: [node1] => {"changed": true, "gid": 0, "group": "root", "mode": "0600", "owner": "root", "path": "/var/log/tempfile/ansible.1txuKd", "size": 0, "state": "file", "uid": 0}                                                                                                                                                                 

PLAY RECAP ***************************************************************************************************************************************************************
node1                      : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[ansible@master ansible_playbooks]$ 

在节点上检查生成的临时文件:

[meizhaohui@node1 ~]$ ll -d /tmp/ansible*
-rw------- 1 ansible ansible    0 Jan 14 09:03 /tmp/ansible.1hP3WN.temp
drwx------ 2 ansible ansible 4096 Jan 14 09:03 /tmp/ansible.dylJ0sbuild
-rw------- 1 ansible ansible    0 Jan 14 09:03 /tmp/ansible_temp_lY7kn5
[meizhaohui@node1 ~]$ ll -d /var/log/tempfile/ansible.1txuKd 
-rw------- 1 root root 0 Jan 14 09:03 /var/log/tempfile/ansible.1txuKd

可以看到,临时文件和临时文件夹创建成功。

  • 当不指定path路径时,会默认在/tmp目录创建临时文件或目录。
  • 当指定path时,path指定的目录需要是已经存在的,并且ansible用户需要在该目录有读写权限,因此需要加上become: yes来提升权限。
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇