zoukankan      html  css  js  c++  java
  • Ansible——文件管理

    文件管理模块

    此类模块主要用来管理anible中文件变更替换等场景

    blockinfile

    该模块将插入/更新/删除标记线内的的多行文本块。

    参数名 含义
    path 需要编辑的文件地址
    block 常用管道符表示
    create 当要操作的文件并不存在时,是否创建对应的文件

    例:

    vim ceshi1.yml 
    ---
    - hosts: 192.168.190.134
      tasks:
        - name: blockinfile
          blockinfile:
            path: /tmp/file.txt
            block: |
              omg
              etc
              hello
              yeah
              wwwwooo
    
    example:
    # BEGIN ANSIBLE MANAGED BLOCK
    omg
    etc
    hello
    yeah
    wwwwooo
    # END ANSIBLE MANAGED BLOCK
    
    

    copy

    copy模块主要用来将主控机上的文件赋值到对应被控机上的某个位置。

    参数名 含义
    backup 是否需要备份
    src 主控机文件地址
    dest 文件预存放地址
    [root@localhost project]# vim ceshi1.yml 
    ---
    - hosts: 192.168.190.134
      tasks:
        - name: cp
          copy:
            src: files/ceshi1
            dest: /tmp/ceshi1
    

    fetch

    fetch模块主要是将远程主机中的文件拷贝到本机中,和copy模块的作用刚刚相反,并且在保存的时候使用hostname来进行保存

    参数名 含义
    flat 允许覆盖默认行为从hostname/path到/file的,如果dest以/结尾,它将使用源文件的基础名称
    src 要获取的远程系统上的文件。这必须是文件,而不是目录
    dest 将文件保存到的目录
    [root@localhost project]# vim ceshi1.yml 
    ---
    - hosts: 192.168.190.134
      tasks:
        - fetch:
            src: /tmp/file.txt
            dest: /
            flat: yes
    

    file

    设置文件,符号链接或目录的属性,或修改文件的所属用户及组和权限的修改等。

    参数名 含义
    group 修改文件的所属组
    owner 修改文件的所属用户
    dest 将文件保存到的目录
    mode 修改权限,对应chmod
    path 被管理文件的路径
    state 状态:包括directory,touch,absent,hard,link

    递归创建目录,且目录权限都为660

    vim ceshi1.yml 
    ---
    - hosts: 192.168.190.134
      tasks:
        - name: file test
          file:
            path: /tmp/playbook/test1.yml
            state: directory
            mode: '660'                     权限必须以引号引起来
    

    修改file.txt文件的权限所属组与用户改为sawyer,且文件权限为660

    [root@localhost project]# vim ceshi1.yml 
    ---
    - hosts: 192.168.190.134
      tasks:
        - name: file test
          file:
            path: /tmp/file.txt
            owner: sawyer
            group: sawyer
            mode: '0660'
    
  • 相关阅读:
    面向安全需求的VANET信道拥塞联合控制框架
    多普勒频移
    5G-Enabled Cooperative Intelligent Vehicular (5GenCIV) Framework: When Benz Meets Marconi
    Vehicle-to-Vehicle Channel Modeling and Measurements: Recent Advances and Future Challenges
    python之对象
    python之数字类型
    python之序列
    数据库进阶
    python之coding style
    python之web开发(待续)
  • 原文地址:https://www.cnblogs.com/sawyer95/p/13631551.html
Copyright © 2011-2022 走看看