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)

大家都在看

  • rpm 打patch

    1、把patch文件放入SOURCES中 2、修改SPECS中的XXX.spec,把下图中箭头的部分都要写上,这里是一个坑 3、最后安装rmp包的时候如果有问题加上–f…

    Linux 2023年6月13日
    0114
  • 我叫Mongo,收了「查询基础篇」,值得你拥有

    这是mongo第二篇「查询基础篇」,后续会连续更新6篇 mongodb的文章总结上会有一系列的文章,顺序是先学会怎么用,在学会怎么用好,戒急戒躁,循序渐进,跟着我一起来探索交流。 …

    Linux 2023年6月14日
    0127
  • Pytorch 中 tensor的维度拼接

    torch.stack() 和 torch.cat() 都可以按照指定的维度进行拼接,但是两者也有区别,torch.satck() 是 增加新的维度进行堆叠,即其维度拼接后会增加一…

    Linux 2023年6月7日
    0106
  • 图解固件、驱动、软件的区别

    大家好,我是良许。 不管我们使用什么操作系统,无论是 Windows、macOS 还是 Linux ,里面都安装了许多软件、驱动程序和固件。但是,这三者概念有区别呢? 我在朋友圈做…

    Linux 2023年6月14日
    0106
  • [转]EVE-NG 中使用 SecureCRT 合并tab标签

    在EVE-NG中双击设备使用SecureCRT对设备进行调试连接时,会出现每次连接不同设备时都会打开一个新的SecureCRT窗口,而不是在一个窗口中添加一个新的标签页,这就会导致…

    Linux 2023年6月8日
    0100
  • 自定义 systemd service

    Red Hat Linux 自 7 版本后 采用systemd 形式取代原先 init ,用户可以参考 系统service 创建自己的service ,以便于日常统一管理,系统se…

    Linux 2023年6月6日
    0110
  • Linux下创建桌面快捷方式

    建立一个文本文件,文件名必须以.desktop结尾,.desktop前面的作为快捷方式的名称 添加如下内容 [Desktop Entry]Encoding=UTF-8Name=Po…

    Linux 2023年6月6日
    097
  • Java50个关键字之static

    关键字static主要有两种作用:第一,为某特定数据类型或对象分配单一的存储空间,而与创建对象的个数无关。第二,希望某个方法或属性与类而不是对象关联在一起,也就是说,在不创建对象的…

    Linux 2023年6月7日
    0103
  • Linux系统编程之匿名管道

    1.进程间通信介绍 1.1 进程通信的基本概念 在之前我们已经学习过进程地址空间。Linux 环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间。任何一个进程的全局变量…

    Linux 2023年6月8日
    0115
  • Redis集群架构

    Replication+Sentinel 这里Sentinel的作用有三个: 监控:Sentinel 会不断的检查主服务器和从服务器是否正常运行。 通知:当被监控的某个Redis服…

    Linux 2023年5月28日
    0101
  • 保姆教程系列三、Nacos Config–服务配置

    前言: 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 上篇我们介绍到 保姆教程系列二、Nacos实现注册中心 配置中心原理 一、 服务配置中心介绍 首先我们来看一下,微…

    Linux 2023年6月14日
    0100
  • Mysql Date操作

    根据format字符串格式化date值。 下列修饰符可以被用在format字符串中: %W 星期名字(Sunday……Saturday) %D 有英语前缀的月份的日期(1s…

    Linux 2023年6月7日
    072
  • zenity,把shell加上图形界面

    有时自己写点小工具自己用,比较喜欢非图形界面的,可是有些应用还是弹出个东西来提醒一下,才能让你注意。 zenity是个很帅的东西,它用法可以参考它的–help ,这里不…

    Linux 2023年5月28日
    093
  • MySQL主从复制常见问题及解决方法

    在从节点清除信息 前提:停止slave的相关线程:stop slave RESET SLAVE #从服务&…

    Linux 2023年6月7日
    0116
  • 【翻译】如何编写 Git 提交消息

    个人博客及创作索引页正在制作中,此处仅释出本地第一大版本。原文档基于 Hexo 及相关插件,不兼容于此处的格式暂不统一修复。 《【翻译】如何编写 Git 提交消息》[1]的简体中文…

    Linux 2023年6月13日
    0134
  • 尤娜,我去面试了

    前情回顾 从前,有一个简单的通道系统叫尤娜…… 尤娜系统的第一次飞行中换引擎的架构垂直拆分改造 四种常用的微服务架构拆分方式 面试前几天 尤娜系统经过一次拆…

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