Linux运维工具Ansibe 模块介绍之setup、copy模块使用实例

前言

Ansible有很多模块,包括云计算、命令行、包管理、系统服务、用户管理等,可以通过官方 站http:
//docs.ansible.com/modules_by_category.html查看相应的模块,也可以在命令行下通过ansible-doc -l命令查看模块,或者通过ansible-doc-s模块名查看具体某个模块的使用方法。官 的介绍比较详细,建议查看官 介绍。

本例环境信息

内 环境机器分配情况如下:

  • 192.168.5.91 主机名:ansible.haiyuan.cn,作用:Ansible主控端CentOS 7.2

  • 192.168.5.61主机名:client01.haiyuan.cn,作用:Ansible被控端机器,CentOS 7.2

  • 192.168.5.56主机名:client02.haiyuan.cn,作用:Ansible被控端机器,CentOS 6.8

  • 本例中,5.91 Ansible主控端已经向被控端机器分发了秘钥对,可以使用密钥对进行无密登录。

    关于Ansible的安装和基本配置,请参考我的上一篇文章:

    Ansible命令行调用模块的语法如下:

    ansible 操作目标 -m 模块名 -a 模块参数

    setup模块

    该模块可用于获取Ansible客户端机器的详细信息,命令如下:

    absible webserver(机器组名) -m setup

    命令显示的部分结果如下(完整结果太详细了,这里只截取了部分显示):

    [root@ansible ~]# ansible localhost -m setup

    [WARNING]: provided hosts list is empty, only localhost is available

    localhost | SUCCESS => {

    “ansible_facts”: {

    “ansible_all_ipv4_addresses”: [

    “192.168.5.91”

    ],

    “ansible_all_ipv6_addresses”: [

    “fe80::f816:3eff:feec:67cd”

    ],

    “ansible_apparmor”: {

    “status”: “disabled”

    },

    “ansible_architecture”: “x86_64”,

    “ansible_bios_date”: “03/06/2017”,

    “ansible_bios_version”: “Unknown”,

    “ansible_cmdline”: {

    “BOOT_IMAGE”: “/boot/vmlinuz-3.10.0-327.el7.x86_64”,

    “LANG”: “zh_CN.UTF-8”,

    “crashkernel”: “auto”,

    “quiet”: true,

    “rhgb”: true,

    “ro”: true,

    “root”: “UUID=32236b41-fcde-460e-8c34-ba50515b33f2”

    },

    “ansible_date_time”: {

    “date”: “2017-10-15”,

    “day”: “15”,

    “epoch”: “1508041541”,

    “hour”: “12”,

    “iso8601”: “2017-10-15T04:25:41Z”,

    “iso8601_basic”: “20171015T122541695928”,

    “iso8601_basic_short”: “20171015T122541”,

    “iso8601_micro”: “2017-10-15T04:25:41.696012Z”,

    “minute”: “25”,

    “month”: “10”,

    “second”: “41”,

    “time”: “12:25:41”,

    “tz”: “CST”,

    “tz_offset”: “+0800”,

    “weekday”: “Sunday”,

    “weekday_number”: “0”,

    “weeknumber”: “41”,

    “year”: “2017”

    },

    “ansible_default_ipv4”: {

    “address”: “192.168.5.91”,

    “alias”: “eth0”,

    “broadcast”: “192.168.5.255”,

    “gateway”: “192.168.5.1”,

    “interface”: “eth0”,

    “macaddress”: “fa:16:3e:ec:67:cd”,

    “mtu”: 1500,

    “netmask”: “255.255.255.0”,

    “network”: “192.168.5.0”,

    “type”: “ether”

    },

    copy模块

    命令实例:

    ansible webserver -m copy -a “src=/usr/local/src/test.py dest=/tmp/ owner=root group=root mode=0755 force=yes”

    其他参数都比较好理解,这里解释下force参数和backup参数。

  • force:如果目标主机包含该文件,但内容不同,则设置为yes后会强制覆盖,设置为no后,只有当目标主机的目标位置不存在该文件时,才复制该文件到目标主机;默认值为yes。

  • backup:在覆盖之前备份源文件,备份文件包含时间。该参数有两个选项yes和no。

  • [root@ansible ~]# ls

    anaconda-ks.cfg epel-release-latest-7.noarch.rpm

    [root@ansible ~]# ansible 192.168.5.61 -m copy -a “src=/root/epel-release-latest-7.noarch.rpm dest=/root/ owner=root group=root mode=0755 force=yes backup=yes”

    192.168.5.61 | SUCCESS => {

    “changed”: true,

    “checksum”: “912fc989097dae2911170caf900eacbfe3f183de”,

    “dest”: “/root/epel-release-latest-7.noarch.rpm”,

    “gid”: 0,

    “group”: “root”,

    “md5sum”: “8d1373481fed58018632132c72422ae1”,

    “mode”: “0755”,

    “owner”: “root”,

    “size”: 14848,

    “src”: “/root/.ansible/tmp/ansible-tmp-1508053396.84-161841013931697/source”,

    “state”: “file”,

    “uid”: 0

    }

    [root@ansible ~]# ssh root@192.168.5.61

    [root@client01 ~]# ls

    anaconda-ks.cfg epel-release-latest-7.noarch.rpm

    [root@client01 ~]# ll

    total 20

    -rw——-. 1 root root 1270 Aug 25 2016 anaconda-ks.cfg

    -rwxr-xr-x 1 root root 14848 Oct 15 15:43 epel-release-latest-7.noarch.rpm

    [root@client01 ~]#

    该模块可实现Ansible主机向客户端传送文件的功能,类似于scp,请大家记得提前关闭所有机器的SELinux功能,不然会出现如下 错:

    [root@ansible ~]# ssh root@192.168.5.61

    [root@client01 ~]# getenforce

    Enforcing

    [root@client01 ~]# exit

    logout

    Connection to 192.168.5.61 closed.

    [root@ansible ~]# echo “test ansible” > test.txt

    [root@ansible ~]# ansible 192.168.5.61 -m copy -a “src=/root/test.txt dest=/root/ owner=root group=root mode=0755 force=yes backup=yes”

    192.168.5.61 | FAILED! => {

    “changed”: false,

    “checksum”: “b269ba1206a7dc75f770b3542f187a836921b302”,

    “failed”: true,

    “msg”: “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”

    }

    [root@ansible ~]#

    如果出现上述错误,需要在被控端安装`libselinux-python`包进行修复,命令如下:

    [root@ansible ~]# ansible 192.168.5.61 -m command -a “yum -y install libselinux-python”

    [WARNING]: Consider using yum module rather than running yum

    192.168.5.61 | SUCCESS | rc=0 >>

    Loaded plugins: fastestmirror

    Determining fastest mirrors

    Resolving Dependencies

    –> Running transaction check

    —> Package libselinux-python.x86_64 0:2.5-11.el7 will be installed

    –> Processing Dependency: libselinux(x86-64) = 2.5-11.el7 for package: libselinux-python-2.5-11.el7.x86_64

    ……

    Installed:

    libselinux-python.x86_64 0:2.5-11.el7

    Updated:

    dracut.x86_64 0:033-502.el7 systemd.x86_64 0:219-42.el7_4.1

    Dependency Updated:

    dracut-config-rescue.x86_64 0:033-502.el7

    dracut-network.x86_64 0:033-502.el7

    libgudev1.x86_64 0:219-42.el7_4.1

    libselinux.x86_64 0:2.5-11.el7

    libselinux-utils.x86_64 0:2.5-11.el7

    libsepol.x86_64 0:2.5-6.el7

    systemd-libs.x86_64 0:219-42.el7_4.1

    systemd-sysv.x86_64 0:219-42.el7_4.1

    Complete!Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast

    [root@ansible ~]# ansible 192.168.5.61 -m copy -a “src=/root/test.txt dest=/root/ owner=root group=root mode=0755 force=yes backup=yes”

    192.168.5.61 | SUCCESS => {

    “changed”: true,

    “checksum”: “b269ba1206a7dc75f770b3542f187a836921b302”,

    “dest”: “/root/test.txt”,

    “gid”: 0,

    “group”: “root”,

    “md5sum”: “d04bb02c8579aa24a866be2467eb5f79”,

    “mode”: “0755”,

    “owner”: “root”,

    “secontext”: “system_u:object_r:admin_home_t:s0”,

    “size”: 13,

    “src”: “/root/.ansible/tmp/ansible-tmp-1508054521.3-3947439211258/source”,

    “state”: “file”,

    “uid”: 0

    }

    [root@ansible ~]#

    安装libselinux-python软件包后,就可以进行复制文件的操作。

    注意

    copy模块跟rsync命令一样,如果路径使用“/”来结尾,则只复制目录里的内容;如果没有使用“/”来结尾,则包含目录在内的整个内容全部被复制。

    声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

    上一篇 2017年9月11日
    下一篇 2017年9月11日

    相关推荐