ansible-复制模块

简介:临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。

1、复制模块

可在终端执行ansible-doc copy查询帮助

[root@ansible ~]# ansible-doc copy
> COPY    (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py)

        The copy' module copies a file from the local or remote machine to a location on the remote machine. Use the
        [fetch] module to copy files from remote locations to the local box. If you need variable interpolation in
        copied files, use the [template] module. Using a variable in the ' field will result in unpredictable
        output. For Windows targets, use the [win_copy] module instead.

  * This module is maintained by The Ansible Core Team
  * note: This module has a corresponding action plugin.

OPTIONS (= is mandatory):

- attributes
        The attributes the resulting file or directory should have.

        To get supported flags look at the man page for chattr' on the target system.

        This string should contain the attributes in the same order as the one displayed by '.

        The =' operator is assumed as default, otherwise +' or -' operators need to be included in the string.

        (Aliases: attr)[Default: (null)]
        type: str
        version_added: 2.3

- backup
        Create a backup file including the timestamp information so you can get the original file back if you somehow
        clobbered it incorrectly.

        [Default: False]
        type: bool
        version_added: 0.7

- checksum
        SHA1 checksum of the file being transferred.

        Used to validate that the copy of the file was successful.

        If this is not provided, ansible will use the local calculated checksum of the src file.

        [Default: (null)]
        type: str
        version_added: 2.5

- content
        When used instead of ', sets the contents of a file directly to the specified value.

        Works only when dest' is a file. Creates the file if it does not exist.

        For advanced formatting or if ' contains a variable, use the [template] module.

        [Default: (null)]
        type: str
        version_added: 1.1

- decrypt
        This option controls the autodecryption of source files using vault.

        [Default: True]
        type: bool
        version_added: 2.4

= dest
        Remote absolute path where the file should be copied to.

        If src' is a directory, this must be a directory too.

        If ' is a non-existent path and if either dest' ends with "/" or ' is a directory, dest' is created.

        If ' is a relative path, the starting directory is determined by the remote host.

        If src' and ' are files, the parent directory of dest' is not created and the task fails if it does not
        already exist.

        type: path

- directory_mode
        When doing a recursive copy set the mode for the directories.

        If this is not set we will use the system defaults.

        The mode is only set on directories which are newly created, and will not affect those that already existed.

        [Default: (null)]
        type: raw
        version_added: 1.5

- follow
        This flag indicates that filesystem links in the destination, if they exist, should be followed.

        [Default: False]
        type: bool
        version_added: 1.8

- force
        Influence whether the remote file must always be replaced.

        If ', the remote file will be replaced when contents are different than the source.

        If no', the file will only be transferred if the destination does not exist.

        Alias ' has been deprecated and will be removed in 2.13.

        (Aliases: thirsty)[Default: True]
        type: bool
        version_added: 1.1

- group
        Name of the group that should own the file/directory, as would be fed to chown'.

        [Default: (null)]
        type: str

- local_follow
        This flag indicates that filesystem links in the source tree, if they exist, should be followed.

        [Default: True]
        type: bool
        version_added: 2.4

- mode
        The permissions of the destination file or directory.

        For those used to /usr/bin/chmod' remember that modes are actually octal numbers. You must either add a leading
        zero so that Ansible's YAML parser knows it is an octal number (like 0644' or 01777')or quote it (like '644''
        or '1777'') so Ansible receives a string and can do its own conversion from string into number. Giving Ansible
        a number without following one of these rules will end up with a decimal number which will have unexpected
        results.

        As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, u+rwx' or =rw,g=r,o=r').

        As of Ansible 2.3, the mode may also be the special string preserve'.

        ' means that the file will be given the same permissions as the source file.

        [Default: (null)]
        type: path

- owner
        Name of the user that should own the file/directory, as would be fed to chown'.

        [Default: (null)]
        type: str

- remote_src
        Influence whether ' needs to be transferred or already is present remotely.

        If no', it will search for ' at originating/master machine.

        If yes' it will go to the remote/target machine for the '.

        remote_src' supports recursive copying as of version 2.8.

        remote_src' only works with mode=preserve' as of version 2.6.

        [Default: False]
        type: bool
        version_added: 2.0

- selevel
        The level part of the SELinux file context.

        This is the MLS/MCS attribute, sometimes known as the '.

        When set to _default', it will use the ' portion of the policy if available.

        [Default: s0]
        type: str

- serole
        The role part of the SELinux file context.

        When set to _default', it will use the ' portion of the policy if available.

        [Default: (null)]
        type: str

- setype
        The type part of the SELinux file context.

        When set to _default', it will use the ' portion of the policy if available.

        [Default: (null)]
        type: str

- seuser
        The user part of the SELinux file context.

        By default it uses the system' policy, where applicable.

        When set to _default', it will use the user' portion of the policy if available.

        [Default: (null)]
        type: str

- src
        Local path to a file to copy to the remote server.

        This can be absolute or relative.

        If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside contents of
        that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all
        contents is copied. This behavior is similar to the ' command line tool.

        [Default: (null)]
        type: path

- unsafe_writes
        Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target file.

        By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target
        files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker
        mounted files, which cannot be updated atomically from inside the container and can only be written in an unsafe
        manner.

        This option allows Ansible to fall back to unsafe methods of updating files when atomic operations fail
        (however, it doesn't force Ansible to perform unsafe writes).

        IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption.

        [Default: False]
        type: bool
        version_added: 2.2

- validate
        The validation command to run before copying into place.

        The path to the file to validate is passed in via '%s' which must be present as in the examples below.

        The command is passed securely so shell features like expansion and pipes will not work.

        [Default: (null)]
        type: str

这里我想把ansible服务器下的nginx源码包下发到所有添加过主机清单下的主机

ansible webserver -m copy -a 'src=/root/nginx-1.8.1.tar.gz dest=/tmp'

拆解:webservser:主机清单下的组
           -m :应用什么模块
           copy :应用复制模块
           -a  :接模块对应的参数
           src : source  资源
           /root/nginx-1.8.1.tar.gz nginx:源码包路径
           dest :destination 目的地
           /tmp:对方主机/tmp目录下

执行结果为成功

[root@ansible ~]# ansible webserver -m copy -a 'src=/root/nginx-1.8.1.tar.gz dest=/tmp'
host2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "a99dc2ee4c60e3134891cd13c111f42901252c2b",
    "dest": "/tmp/nginx-1.8.1.tar.gz",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/tmp/nginx-1.8.1.tar.gz",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 833473,
    "state": "file",
    "uid": 0
}
host1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "a99dc2ee4c60e3134891cd13c111f42901252c2b",
    "dest": "/tmp/nginx-1.8.1.tar.gz",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/tmp/nginx-1.8.1.tar.gz",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 833473,
    "state": "file",
    "uid": 0
}
host3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "a99dc2ee4c60e3134891cd13c111f42901252c2b",
    "dest": "/tmp/nginx-1.8.1.tar.gz",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/tmp/nginx-1.8.1.tar.gz",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 833473,
    "state": "file",
    "uid": 0
}
[root@ansible ~]#
ansible webserver -m copy -a 'src=/root/nginx-1.8.1.tar.gz dest=/tmp/ owner=root group=bin mode=777'
拆解:
       owner = 主人

       group = 账号

       mode = 权限

这里是把ansible服务器下/root目录下的/nginx-1.8.1.tar.gz源码包复制到主机清单下的所有主机的tmp目录下 主人是root  group账号是bin mode权限777
如果默认不写owner和group那么主人和账号全部是root

执行结果为成功

[root@ansible ~]# ansible webserver -m copy -a 'src=/root/nginx-1.8.1.tar.gz dest=/tmp owner=root group=bin mode=777'
host3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "a99dc2ee4c60e3134891cd13c111f42901252c2b",
    "dest": "/tmp/nginx-1.8.1.tar.gz",
    "gid": 1,
    "group": "bin",
    "mode": "0777",
    "owner": "root",
    "path": "/tmp/nginx-1.8.1.tar.gz",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 833473,
    "state": "file",
    "uid": 0
}
host2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "a99dc2ee4c60e3134891cd13c111f42901252c2b",
    "dest": "/tmp/nginx-1.8.1.tar.gz",
    "gid": 1,
    "group": "bin",
    "mode": "0777",
    "owner": "root",
    "path": "/tmp/nginx-1.8.1.tar.gz",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 833473,
    "state": "file",
    "uid": 0
}
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "a99dc2ee4c60e3134891cd13c111f42901252c2b",
    "dest": "/tmp/nginx-1.8.1.tar.gz",
    "gid": 1,
    "group": "bin",
    "mode": "0777",
    "owner": "root",
    "path": "/tmp/nginx-1.8.1.tar.gz",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 833473,
    "state": "file",
    "uid": 0
}
[root@ansible ~]#
去客户机上查看源码包是否和我们想要的执行结果相同
[root@bogon tmp]# ll nginx-1.8.1.tar.gz
-rwxrwxrwx. 1 root bin 833473 9月  30 15:54 nginx-1.8.1.tar.gz
答案是相同的
[root@bogon tmp]#
ansible webserver -m copy -a 'src=/root/hostlist/ dest=/tmp owner=root group=bin mode=777 backup=yes
如果文件有多份,可以进行备份。
拆解:
     backup 备份

 把ansible服务器下的root目录下的hostlist文件分别复制到所有主机清单下的/tmp目录,主人是root  账号是bin  权限是777 进行备份
 备份这里可以理解为如果客户机下已经有hostlist这个文件了,那么如果不改变文件,那么执行命令ansible虽然显示成功其实是没有变化的
 那么如果修改了文件执行命令的时候不加入backup(备份)参数的话,复制到客户机下默认是会把之前的原文件覆盖掉的,那么如果加了backup进行复制的话
 会在原文件修改成另一个名字 source.5653.2022-09-30@16:20:14~  类似这种

执行结果为成功

[root@ansible ~]# ansible webserver -m copy -a 'src=/root/hostlist/ dest=/tmp owner=root group=bin mode=777 backup=yes'
host3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup_file": "/tmp/source.12821.2022-10-01@16:33:55~",
    "changed": true,
    "checksum": "a5358653b4f7dd46c59df4e79353af2f037485d1",
    "dest": "/tmp/source",
    "gid": 1,
    "group": "bin",
    "md5sum": "d77fb01a61e4ffdd1bb5f9d1197c4428",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 245,
    "src": "/root/.ansible/tmp/ansible-tmp-1664613236.31-6626-181541999667946/source",
    "state": "file",
    "uid": 0
}
host2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup_file": "/tmp/source.5686.2022-09-30@16:33:57~",
    "changed": true,
    "checksum": "a5358653b4f7dd46c59df4e79353af2f037485d1",
    "dest": "/tmp/source",
    "gid": 1,
    "group": "bin",
    "md5sum": "d77fb01a61e4ffdd1bb5f9d1197c4428",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 245,
    "src": "/root/.ansible/tmp/ansible-tmp-1664613236.29-6624-13795943013274/source",
    "state": "file",
    "uid": 0
}
host1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup_file": "/tmp/source.5965.2022-09-30@16:33:56~",
    "changed": true,
    "checksum": "a5358653b4f7dd46c59df4e79353af2f037485d1",
    "dest": "/tmp/source",
    "gid": 1,
    "group": "bin",
    "md5sum": "d77fb01a61e4ffdd1bb5f9d1197c4428",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 245,
    "src": "/root/.ansible/tmp/ansible-tmp-1664613236.28-6622-231481691204425/source",
    "state": "file",
    "uid": 0
}

查看客户机/tmp目录下是否生成了备份文件
[root@bogon tmp]# ll
-rwxrwxrwx. 1 root bin    1259 9月  30 16:19 anaconda-ks.cfg
-rwxrwxrwx. 1 root bin      84 9月  30 16:19 hostlist
-rwx------. 1 root root    836 9月  26 15:34 ks-script-XI38wT
-rwxrwxrwx. 1 root bin  833473 9月  30 15:54 nginx-1.8.1.tar.gz
-rwxrwxrwx. 1 root bin     245 9月  30 16:33 source
-rwxrwxrwx. 1 root bin      84 9月  30 16:19 source.5653.2022-09-30@16:20:14~

Original: https://www.cnblogs.com/Jqazc/p/16745387.html
Author: 我真的兔了
Title: ansible-复制模块

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611178/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • go的调度

    操作系统根据资源访问权限的不同,体系架构可以分为用户空间和内核空间;内核空间主要操作访问CPU资源,IO资源,内存资源等硬件资源,为应用程序提供最基本的基础资源;用户空间是上层应用…

    数据库 2023年6月9日
    082
  • mysql创建用户并授权

    新建用户 create user ‘username’@’%’ identified by ‘password’; • user_name:要创建用户的名字。• host:表示要这…

    数据库 2023年5月24日
    063
  • Redis-实现分布式锁

    单节点分布式锁 本地的锁操作非常常见,无非就是申请一个锁变量lock, 加锁时,判断锁变量是否被持有,如果被持有中,则加锁失败,如果不被持有,则加锁成功,将锁的状态改为持有锁的这个…

    数据库 2023年6月11日
    072
  • django中的模板层简介

    1.什么是模板层 模板层可以根据视图中传递的字典数据动态生产相应的HTML页面 2.模板层的配置 1.在项目&am…

    数据库 2023年6月14日
    078
  • HTTP 协议概述

    什么是 HTTP 协议 什么是协议? 协议是指双方,或多方,相互约定好,大家都需要遵守的规则,叫协议。所谓 HTTP 协议,就是指,客户端和服务器之间通信时,发送的数据,需要遵守的…

    数据库 2023年6月11日
    074
  • Nginx 简介、安装、核心配置

    Nginx简介 背景介绍 Nginx(”engine x”)一个具有高性能的【HTTP】和【反向代理】的【WEB服务器】,同时也是一个【POP3/SMTP/…

    数据库 2023年6月6日
    0101
  • 事务

    事务 *事务的简介 事务是一组操作的合集,它是一个不可分割的工作单位,事务会把所有的操作作为一个整体,一起向系统提交或撤销操作请求,这些操作只有同时成功、同时失败; 事务的操作 查…

    数据库 2023年6月16日
    096
  • 字节一面:事务还没提交的时候,redolog 能不能被持久化到磁盘呢?

    又是被自己菜醒的一天,总结面经看到这题目听都没听过,打开百度就像吃饭一样自然 老规矩,背诵版在文末。点击阅读原文可以直达我收录整理的各大厂面试真题 首先,咱需要明白的是,啥是持久化…

    数据库 2023年6月6日
    0128
  • MySQL连接的建立与使用

    在 MYSQL的启动过程中,可以看到在 mysqld_main() 函数的最后调用了 mysqld_socket_acceptor->connection_event_loo…

    数据库 2023年6月9日
    078
  • Linux–>进程管理

    基本介绍 在Linux中, 每个执行程序都称为一个进程。每一个进程都会分配一个ID号(pid,进程号) 每个进程都可能以俩种方式存在的。分别是 前台与 后台,所谓前台进程就是用户目…

    数据库 2023年6月14日
    0103
  • Python–模块Module

    1、定义模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件,(文件名:test.py,对应的模块名test)包:用来…

    数据库 2023年6月9日
    085
  • 程序员“迷惑代码”大赏

    谈到程序员,对于外行人来说一贯的印象就是格子衫大裤衩外加人字拖,蓬头(秃头)垢面黑眼圈,还有就是”人傻钱多死得快”🤣,这是外界对程序员固有的思想,但是作为新…

    数据库 2023年6月11日
    094
  • 【黄啊码】这种PHP验证码你值得拥有(注:支持中文)

    csharp;gutter:true; width = $width; $this->height = $height; $this->codeNum = $codeN…

    数据库 2023年6月16日
    094
  • 视频语义分割基准数据集与评估方法

    概述 本文来源于《A Benchmark Dataset and Evaluation Methodology for Video Object Segmentation》,论文主…

    数据库 2023年6月11日
    081
  • 人的思维定势

    人的思维定势 前段时间,因为咳嗽时间太长,去了医院看”呼吸内科”,检查了一番,最终发现是”变异性咳嗽”,也叫”变异性哮喘…

    数据库 2023年6月9日
    074
  • MySQL隐式转换的坑

    MySQL以以下规则描述比较操作如何进行转换: 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 实际使用中经研究发现字符串和数字比较时,优先把字符串转换为…

    数据库 2023年6月9日
    074
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球