Ansible内置模块之file

  • RHCE9.0 (点击查看课程介绍) 

  • 上课课时:72课时/12天+考试辅导1-2天

  • 开班频率:2个月开一期

  • 最新新班:7月27日 周末班

  • 上课方式:面授/直播;提供随堂录播


    图片

    ansible.builtin.file 模块用于管理文件和目录的属性。可以创建、删除文件或目录,修改文件权限、所有者等属性。

    01  选项说明:

    选项

    必须

    类型

    默认值

    说明

    path

    str

    要管理的文件或目录的路径

    state

    str

    状态或类型。可为 file,directory,absent,link 等

    mode

    str

    权限,如 0644、0755 等

    owner

    str

    所有者

    group

    str

    属组

    recurse

    bool

    no

    是否递归更改目录及其内容的所有者、组和权限

    src

    str

    用于创建符号链接时,指向源文件的路径

    force

    bool

    no

    在创建符号链接或硬链接时,如果目标已存在,是否强制替换

    selevel

    str

    设置文件或目录的 SELinux 安全级别

    serole

    str

    设置文件或目录的 SELinux 角色

    setype

    str

    设置文件或目录的 SELinux 类型

    seuser

    str

    设置文件或目录的 SELinux 用户

    02  用例:

    # 创建或修改一个文件的属主,属组及权限            
    - name: Change file ownership, group and permissions            
      ansible.builtin.file:            
        path: /etc/foo.conf            
        owner: foo            
        group: foo            
        mode: '0644'            
               
    # 创建一个目录            
    - name: Create a directory if it does not exist            
      ansible.builtin.file:            
        path: /etc/some_directory            
        state: directory            
        mode: '0755'            
               
    # 创建一个带httpd_sys_content_t标签的目录            
    - name: Create the /webdev directory with SELinux label            
      ansible.builtin.file:            
        path: /webdev            
        state: directory            
        group: webdev            
        setype: httpd_sys_content_t            
        mode: '2775'            
               
    # 递归修改目录属主和属组            
    - name: Recursively change ownership of a directory            
      ansible.builtin.file:            
        path: /etc/foo            
        state: directory            
        recurse: yes            
        owner: foo            
        group: foo            
               
    # 创建一个连接文件            
    - name: Create a symbolic link            
      ansible.builtin.file:            
        src: /file/to/link/to            
        dest: /path/to/symlink            
        owner: foo            
        group: foo            
        state: link            
               
    # 强制创建/更新link地址            
    - name: Force replace an existing symbolic link            
      ansible.builtin.file:            
        src: /etc/newsourcefile            
        path: /etc/linkfile            
        state: link            
        force: yes            
               
    # 更新修改和访问时间            
    - name: Update modification and access time of given file            
      ansible.builtin.file:            
        path: /etc/some_file            
        state: file            
        modification_time: now            
        access_time: now            
               
    # 更新文件访问时间            
    - name: Set access time based on seconds from epoch value            
      ansible.builtin.file:            
        path: /etc/another_file            
        state: file            
        access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'            
               
    # 删除文件            
    - name: Remove file (delete file)            
      ansible.builtin.file:            
        path: /etc/foo.txt            
        state: absent            
               
    # 递归删除目录            
    - name: Recursively remove directory            
      ansible.builtin.file:            
        path: /etc/foo            
        state: absent            


     

    往期相关文章推荐

  • Ansible 命令行指南

  • 在RHEL 9上使用Ansible安装GitLab CE

  • 16张PPT让你一小时学会Ansible

  • 一文看懂Ansible自动化运维

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门微思网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值