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)

大家都在看

  • mac(m1)配置my.cnf

    今天开始学习了数据库,在安装MySQL之后启动一直报错,然后在网上找了很多解决方法,最后用以下方法解决 对于习惯了windows的小伙伴来说,直接去安装目录里边修改my.ini就可…

    数据库 2023年5月24日
    088
  • Tomcat配置文件Server.xml解析

    一、Sax的事件驱动模型 类图 基础实现类 DefaultHandler2: 此类扩展了SAX2基本处理程序类,以支持SAX2 LexicalHandler , DeclHandl…

    数据库 2023年6月11日
    051
  • starter包~mica-auto自动生成spring.factories等元文件

    首先,进行springboot2.7之后,官方不推荐使用 /META-INF/spring.factories,转成和SPI比较类似的 /META-INF/spring/org.s…

    数据库 2023年6月6日
    062
  • StoneDB社区答疑第一期

    当然,目前 StoneDB 的社区建设还正处于初启阶段,我们坚信,开源项目的成长,最终还是要靠社区用户一起来共创,因此,StoneDB 开源社区非常重视社区用户的声音,在 7 月份…

    数据库 2023年5月24日
    087
  • ASP.NET CORE WEB项目介绍

    首先创建一个asp.net core web应用程序 第二步 目前官方预置了7种模板项目供我们选择。从中我们可以看出,既有我们熟悉的MVC、WebAPI,又新添加了Razor Pa…

    数据库 2023年6月14日
    087
  • [LeetCode]13. 罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做…

    数据库 2023年6月9日
    0126
  • Java 线程创建与常用方法

    进程与线程 进程 程序由指令和数据组成,但这些指令要运行,数据要读写,就必须将指令加载至 CPU,数据加载至内存。在指令运行过程中还需要用到磁盘、网络等设备。进程就是用来加载指令、…

    数据库 2023年6月16日
    059
  • 一次线上MySQL死锁告警原因排查

    项目场景:一次线上MySQL死锁告警原因排查最近处理了一次在线数据警报,记录下来。 [En] Recently handled an online data alarm, reco…

    数据库 2023年5月24日
    046
  • FIO磁盘性能测试工具

    FIO磁盘性能测试工具 简介 一般我们测试硬盘或者存储的性能的时候,会用Linux系统自带的dd命令,因为是自带命令,简单易使用,因此一些客户喜欢使用dd命令来测试磁盘的读写性能。…

    数据库 2023年6月9日
    077
  • 在RAC上部署OGG并配置OGG高可用

    简介 由于业务系统要与大数据平台进行对接,需要将Oracle DB的数据同步到异构数据库上,故选用也不得不用上了Goldengate方案然鹅,OGG在RAC上的HA配置一直众说纷纭…

    数据库 2023年6月16日
    077
  • MySQL处理Java客户端连接

    在MySQL里面往往有一个主线程,这是单线程,它不断的循环查看是否有socket是否有读写事件,如果有读写事件,再从线程池里面找个工作线程处理这个socket的读写事件,完事之后工…

    数据库 2023年6月11日
    097
  • Java 全栈知识体系(2021 PDF 版本)

    40000 +字长文总结,民工哥已将此文整理成PDF文档了,需要的见文后下载获取方式。 全栈知识体系总览 Java入门与进阶 面向对象与Java基础 Java 基础 –…

    数据库 2023年6月9日
    072
  • 2_JDBC

    使用客户端工具访问数据库, 需要手工建立连接, 输入用户名和密码登陆, 编写SQL语句, 点击执行, 查看操作结果(结果集或受行数影响) 在实际开发中, 当用户的数据发生改变时, …

    数据库 2023年6月11日
    055
  • 【Java代码之美】 — Java11新特性解读

    1.背景 美国时间 09 月 25 日,Oralce 正式发布了 Java 11,这是据 Java 8 以后支持的首个长期版本。非常值得大家的关注,可以通过下面的地址进行下载: h…

    数据库 2023年6月6日
    083
  • 2_Git

    一. 引言 在单人开发过程中, 需要进行版本管理, 以利于开发进度的控制 在多人开发过程中, 不仅需要版本管理, 还需要进行多人协同控制 二. 介绍 Git是一个 开源的分布式版本…

    数据库 2023年6月11日
    077
  • mysql 5.7命令行安装(windows)

    下载mysql-5.7.xx-winx64 ZIP版,https://dev.mysql.com/downloads/mysql/5.7.html 拷贝压缩包文件mysql-5.7…

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