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/578395/

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

(0)

大家都在看

  • Linux 下统计文件夹下文件的数量

    1、查看当前目录下的文件数量(不包含子目录中的文件) 2、查看当前目录下的文件数量(包含子目录中的文件) 3、 查看当前目录下的文件夹目录个数(不包含子目录中的目录),同上述理,如…

    Linux 2023年6月13日
    099
  • jmeter 性能测试 报错信息“address already in use:connect”解决方法

    jmeter性能测试报”address already in use:connect” 报错信息 原因分析: 这个问题的原因是windows端口被耗尽了(默…

    Linux 2023年6月8日
    096
  • 如何在 pyqt 中解决启用 DPI 缩放后 QIcon 模糊的问题

    问题描述 如今显示器的分辨率越来越高,如果不启用 DPI 缩放,软件的字体和图标在高分屏下就会显得非常小,看得很累人。从 5.6 版本开始,Qt 便能支持 DPI 缩放功能,Qt6…

    Linux 2023年6月7日
    0195
  • JuiceFS v1.0 正式发布,首个面向生产环境的 LTS 版本

    今天,JuiceFS v1.0 发布了 🎉 经过了 18 个月的持续迭代和大量生产环境的广泛验证,此版本将成为第一个被长期维护的稳定版(LTS)。同时,该版本提供完整的向前兼容,所…

    Linux 2023年6月14日
    091
  • 记一次burp suite文件上传漏洞实验

    一·文件上传漏洞概念文件上传漏洞是指 Web 服务器允许用户在没有充分验证文件名称、类型、内容或大小等内容的情况下将文件上传到其文件系统。未能正确执行这些限制可能意味着即使是基本的…

    Linux 2023年6月7日
    093
  • 【windows】在windows右键菜单加入在当前路径打开cmd功能?

    在Ubuntu中可以在一般目录下点击右键选中Open in Terminal即可打开一个命令终端,由于自己平常在windows上开发时也常常使用cmd命令行进行操作,但是每次都需要…

    Linux 2023年6月13日
    0101
  • python写日志

    写日志的办法多种多样,我这个是我喜欢的办法,可以做个参考 没啥说的,直接上代码 import time def write_log(value): now_time = time….

    Linux 2023年6月6日
    074
  • Linux系统编程之进程控制(进程创建、终止、等待及替换)

    进程创建 在上一节讲解进程概念时,我们提到fork函数是从已经存在的进程中创建一个新进程。那么,系统是如何创建一个新进程的呢?这就需要我们更深入的剖析fork函数。 1.1 for…

    Linux 2023年6月8日
    0102
  • Shell第四章《正则表达式》

    1.1、名词解释 正则表达式(regular expression, RE)是一种字符模式,用于在查找过程中匹配指定的字符。在大多数程序里,正则表达式都被置于两个正斜杠之间;例如/…

    Linux 2023年6月6日
    097
  • Workshop与会议形式的分类

    在计算机科研工作阅读的过程中,我们经常遇到workshop和conference等关键词,通过这些关键词我们可以一定程度上了解文章的水平,从而方便自己的学习,下面记录了一些我从网络…

    Linux 2023年6月14日
    0171
  • MySQL——索引结构

    索引:用于快速查找数据。 索引是将数据的一些关键信息通过特定的数据结构存储到一片新的空间中,这样在文件查找的时候能快速找到。 mysql索引类型: B+TREE、HASH、R-TR…

    Linux 2023年6月7日
    0102
  • 【微服务】- Nacos-注册中心

    微服务 – 注册中心 – Nacos 😄生命不息,写作不止🔥 继续踏上学习之路,学之分享笔记👊 总有一天我也能像各位大佬一样🏆 一个有梦有戏的人 @怒放吧德…

    Linux 2023年6月6日
    0132
  • Nginx进阶篇—web模块及proxy代理

    server { #在server{下面输入 sub_filter n…

    Linux 2023年6月6日
    074
  • 1. 文件与I/O

    内核是如何处理系统调用 每个系统调用被赋予了一个系统调用号 在i386平台上,执行一个系统调用是通过int 0X80指令完成的 eax存放系统的调用号 ebx,ecx,edx,es…

    Linux 2023年6月6日
    077
  • Redis中删除过期Key的三种策略

    转载自:http://blog.csdn.net/a_bang/article/details/52986935?locationNum=9&fps=1 项目中有个接口要频…

    Linux 2023年5月28日
    0100
  • linux mv 命令中断的解决办法

    背景:使用mv对服务器文件进行迁移的过程中,因为网络中断,所以迁移到一半被停止了。 现状:有一部分文件没有迁移完,但是文件夹已经存在了,重新mv提示文件夹已经存在,切已经有文件,所…

    Linux 2023年5月27日
    0104
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球