zoukankan      html  css  js  c++  java
  • python自动化运维八:Ansible

    Ansible是新出现的自动化运维工具,基于Python研发。糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。仅需在管理工作站上安装ansible程序配置被管控主机的IP信息,被管控的主机无客户端。ansible应用程序存在于epel(第三方社区)源,依赖于很多python组件。主要包括:

    (1)、连接插件connection plugins:负责和被监控端实现通信;

    (2)host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

    (3)、各种模块核心模块、command模块、自定义模块;

    (4)、借助于插件完成记录日志邮件等功能;

    (5)playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

    下面来看下ansible的使用:

    一:首先安装ansible模块:apt install ansible

    二:配置Ansible以及测试。

    (1)第一步是修改主机与组配置。文件在/etc/ansible/hosts. 格式位ini。添加1台主机同时定义IPwerbserver组。

    #green.example.com

    #blue.example.com

    192.168.0.9

    # Ex 2: A collection of hosts belonging to the 'webservers' group

    #[webservers]

    #alpha.example.org

    #beta.example.org

    192.168.0.9

    ()通过ping模块测试主机的连通性。分别对单主机进行ping操作。出现如下结果表明测试成功。

    root@zhf-linux:/home/zhf# ansible 192.168.0.9 -m ping

    192.168.0.9 | SUCCESS => {

    "changed": false,

    "ping": "pong"

    }

    root@zhf-linux:/home/zhf# ansible webservers -m ping

    192.168.0.9 | SUCCESS => {

    "changed": false,

    "ping": "pong"

    }

    Ansible是依赖于SSH通信,如果机器之间没有配置SSH证书访问的话,则需要用登录密码访问。命令需改为ansible 192.168.0.9 -m ping -k.

    三:Ansible功能模块:

    (1) 远程命令模块:

    模块包括command,script,shell都可以实现远程shell命令运行。command作为ansible的默认模板,可以运行远程权限范围所有的shell命令,script是在远程主机执行主控端存储的shell脚本文件。相当于SCP+SHELL组合,shell功能是执行远程主机的shell脚本文件。

    root@zhf-linux:/home/zhf# ansible webservers -m command -a "ls -al"

    192.168.0.9 | SUCCESS | rc=0 >>

    total 72

    drwx------ 12 root root 4096 Nov 12 14:45 .

    drwxr-xr-x 22 root root 4096 Jul 9 16:59 ..

    drwxr-xr-x 3 root root 4096 Nov 12 14:45 .ansible

    -rw------- 1 root root 5408 Nov 12 14:46 .bash_history

    -rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc

    drwx------ 5 root root 4096 Aug 31 21:47 .cache

    drwx------ 4 root root 4096 Jul 26 10:47 .config

    drwx------ 3 root root 4096 Jul 9 15:59 .dbus

    drwx------ 2 root root 4096 Jul 9 16:23 .gvfs

    drwxr-xr-x 3 root root 4096 Jul 26 11:23 .local

    drwxr-xr-x 2 root root 4096 Jul 26 11:33 .pip

    -rw-r--r-- 1 root root 140 Feb 20 2014 .profile

    drwxr-xr-x 3 root root 4096 Jul 26 11:41 .python-eggs

    drwxr-xr-x 2 root root 4096 Oct 30 21:18 .rpmdb

    drwx------ 2 root root 4096 Oct 27 23:11 .ssh

    -rw-r--r-- 1 root root 0 Oct 29 10:02 test.txt

    -rw------- 1 root root 5439 Oct 31 22:07 .viminfo

    在主机上建立一个sh文件,内容很简单,就是echo “hello ansible”。通过远程端也可以调用。

    root@zhf-linux:/home/zhf# ansible webservers -m shell -a "/home/zhf/zhf/shell_prj/test1.sh"

    192.168.0.9 | SUCCESS | rc=0 >>

    hello ansible!

    拷贝文件:

    在上传文件的同时还可以设置上传文件的属主以及权限。格式为: owner=xxx group=xxx mode=0744

    root@zhf-linux:/home/zhf/zhf# ansible webservers -m copy -a "src=/home/zhf/zhf/test2.txt dest=/home/zhf owner=root group=root mode=0744"

    192.168.0.9 | SUCCESS => {

    "changed": true,

    "checksum": "07c0752c54e3883358ab0c8c6008004929954217",

    "dest": "/home/zhf/test2.txt",

    "gid": 0,

    "group": "root",

    "md5sum": "d2c01e6badaa08464b9e0cd578a5de8b",

    "mode": "0744",

    "owner": "root",

    "size": 30,

    "src": "/root/.ansible/tmp/ansible-tmp-1510471651.28-269106495340296/source",

    "state": "file",

    "uid": 0

    }

    stat模块:

    获取远程文件的状态信息,包括ctime,mtime,atime,uid,gid等信息。我们用刚才上传的文件为例。

    root@zhf-linux:/home/zhf/zhf# ansible webservers -m stat -a "path=/home/zhf/test2.txt"

    192.168.0.9 | SUCCESS => {

    "changed": false,

    "stat": {

    "atime": 1510471652.086876,

    "checksum": "07c0752c54e3883358ab0c8c6008004929954217",

    "ctime": 1510471652.130877,

    "dev": 2054,

    "exists": true,

    "gid": 0,

    "gr_name": "root",

    "inode": 130813,

    "isblk": false,

    "ischr": false,

    "isdir": false,

    "isfifo": false,

    "isgid": false,

    "islnk": false,

    "isreg": true,

    "issock": false,

    "isuid": false,

    "md5": "d2c01e6badaa08464b9e0cd578a5de8b",

    "mode": "0744",

    "mtime": 1510471651.438875,

    "nlink": 1,

    "path": "/home/zhf/test2.txt",

    "pw_name": "root",

    "rgrp": true,

    "roth": true,

    "rusr": true,

    "size": 30,

    "uid": 0,

    "wgrp": false,

    "woth": false,

    "wusr": true,

    "xgrp": false,

    "xoth": false,

    "xusr": true

    }

    }

    远程安装软件:将会远程主机安装上ansible模块。

    root@zhf-linux:/home/zhf/zhf# ansible webservers -m apt -a "pkg=ansible state=latest"

    cron模块:

    root@zhf-linux:/home/zhf/zhf# ansible webservers -m cron -a "name=check dirs hour='5,2' job='ls -al'"



    下一节讲介绍playbook模块

  • 相关阅读:
    待办
    安卓微信浏览器修改的代码总是不生效
    微信浏览器内核2
    微信浏览器内核
    随记
    三次握手最后一个ack没有收到怎么办?
    判断偶数:
    利用kubeadm工具安装Kubernetes1.15版本
    kubernetes安装Helm
    最大子列和(附加子列初始元素和末尾元素)
  • 原文地址:https://www.cnblogs.com/zhanghongfeng/p/7821801.html
Copyright © 2011-2022 走看看