• 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 `content' 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 `lsattr'.
    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 `src', 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 `content' 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 `dest' is a non-existent path and if either `dest' ends with "/" or `src' is a directory, `dest' is created.
    If `dest' is a relative path, the starting directory is determined by the remote host.
    If `src' and `dest' 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 `yes', 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 `thirsty' 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 `u=rw,g=r,o=r').
    As of Ansible 2.3, the mode may also be the special string `preserve'.
    `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 `src' needs to be transferred or already is present remotely.
    If `no', it will search for `src' at originating/master machine.
    If `yes' it will go to the remote/target machine for the `src'.
    `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 `range'.
    When set to `_default', it will use the `level' 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 `role' 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 `type' 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 `rsync' 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~

     


    __EOF__

    本文作者Echo
    本文链接https://www.cnblogs.com/Jqazc/p/16745387.html
    关于博主:评论和私信会在第一时间回复。或者直接私信我。
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
  • 相关阅读:
    专升本三本计科仔学习java到实习之路4
    【前沿】数据目录是什么?您为何需要它?
    设计模式—结构型模式之装饰器模式
    应急响应-分析(windows)
    mybatis一级缓存和二级缓存区别点
    Excel做题记录——整数规划优化模型
    Java 18 还未用上,最新Java 19 则出来了
    Numpy、Matplotlib and Pandas
    从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
    Java“牵手”淘宝商品评论数据采集+淘宝商品评价接口,淘宝商品追评数据接口,行业商品质检接口,API接口申请指南
  • 原文地址:https://www.cnblogs.com/Jqazc/p/16745387.html