IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    ansible安装配置及实例

    铁匠发表于 2017-04-20 08:06:03
    love 0

    一、简介

    ansible 和 saltstack 一样都是基于 Python 开发的,是比 puppet 和 saltstack 更轻量级的运维自动化工具。无服务器端,使用时直接运行命令即可,不需要在被管控主机上安装任何客户端,所以任何一台机器只要安装了 ansible 就可以管控其他主机。基于模块工作,可使用任意语言开发模块。也可使用 yaml 语言定制剧本 playbook;基于SSH工作;可实现多级指挥。

    二、安装配置

    1、准备工作

    准备三台机器 Centos6.5_64,这两台机器都关闭 selinux,清空 iptables 规则并保存。

    • master:192.168.2.71
    • slaver:192.168.2.72
    • slaver:192.168.2.73
    2、编辑 hosts 文件

    两台都设置,若机器太多,可以通过搭建 DNS,则不用在每台机器上设置这个

    • 192.168.2.71 master.test.com
    • 192.168.2.72 slaver2.test.com
    • 192.168.2.73 slaver3.test.com
    3、设置 hostname
    在 master 上
    [root@tiejiangSRC1 ~]# vim /etc/sysconfig/network
        HOSTNAME=master.test.com
    
    在 slaver 上
    [root@tiejiangSRC1 ~]# vim /etc/sysconfig/network
        HOSTNAME=slaver.test.com
    4、安装
    [root@tiejiangSRC1 ~]# yum install -y epel-release 
    [root@tiejiangSRC1 ~]# yum install -y ansible
    5、SSH密钥配置
    在Ansible服务端生成密钥,并且复制公钥到节点中。
    [root@tiejiangSRC1 ~]# ssh-keygen -t rsa    //一路回车下去
    [root@tiejiangSRC1 ~]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
    
    使用ssh-copy-id命令来复制Ansible公钥到节点中
    [root@tiejiangSRC1 ~]# ssh-copy-id -i root@192.168.2.72     //输入yes和密码
    [root@tiejiangSRC1 ~]# ssh-copy-id -i root@192.168.2.73     //输入yes和密码
    6、ansible配置
    [root@tiejiangSRC1 ~]# vim /etc/ansible/hosts
        [testhost]
        192.168.2.71
        192.168.2.72
        192.168.2.73

    三、ansible 实例

    1、远程执行命令
    [root@tiejiangSRC1 ~]# ansible testhost -m command -a 'w'
        192.168.2.73 | SUCCESS | rc=0 >>
         12:12:34 up 31 min,  3 users,  load average: 0.00, 0.00, 0.00
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     pts/0    192.168.2.62     11:41   31:11   0.01s  0.01s -bash
        root     pts/1    192.168.2.62     11:44    2:16   0.00s  0.00s -bash
        root     pts/2    192.168.2.71     12:12    0.00s  0.04s  0.00s /bin/sh -c /usr
        
        192.168.2.72 | SUCCESS | rc=0 >>
         14:02:14 up 28 min,  2 users,  load average: 0.00, 0.00, 0.00
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     pts/0    192.168.2.62     13:34    2:44   0.04s  0.04s -bash
        root     pts/1    192.168.2.71     14:02    1.00s  0.10s  0.00s /bin/sh -c /usr
        
        192.168.2.71 | SUCCESS | rc=0 >>
         13:33:51 up 55 min,  4 users,  load average: 0.83, 0.29, 0.11
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     tty1     :0               12:38   55:56   3.26s  3.26s /usr/bin/Xorg :
        root     pts/0    192.168.2.62     12:42   48:00   0.04s  0.04s -bash
        root     pts/1    192.168.2.71     13:33    1.00s  0.07s  0.00s /bin/sh -c /usr
        root     pts/2    192.168.2.62     13:05    7.00s  4.23s  1.21s /usr/bin/python

    注意:"-m" 指定模块名,"-a" 指定相应命令,这样就可以批量执行命令。这里的 testhost 为之前自定义的主机组名。当然我们也可以直接写一个 ip,针对某一台机器来执行命令。如下:

    [root@tiejiangSRC1 ~]# ansible 192.168.2.72 -m command -a 'w'
        192.168.2.72 | SUCCESS | rc=0 >>
         14:04:49 up 30 min,  2 users,  load average: 0.00, 0.00, 0.00
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     pts/0    192.168.2.62     13:34    5:19   0.04s  0.04s -bash
        root     pts/1    192.168.2.71     14:04    0.00s  0.10s  0.00s /bin/sh -c /usr
    2、远程执行Shell脚本
    [root@tiejiangSRC1 ~]# cat /tmp/test.sh     //创建一个shell脚本
        #!/bin/bash
        echo `date` > /tmp/ansible_shell.log
    [root@tiejiangSRC1 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"        //把脚本分发到各远程机
    [root@tiejiangSRC1 ~]# ansible testhost -m shell -a "/tmp/test.sh"      //批量执行脚本
    [root@tiejiangSRC1 ~]# ansible testhost -m shell -a "cat /etc/passwd|wc -l "        //注意:shell 模块,还支持远程执行命令并且带管道
    3、拷贝文件和目录
    [root@tiejiangSRC1 ~]# ansible testhost -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansibletet.txt owner=root group=root mode=0644"       //拷贝文件
    [root@tiejiangSRC1 ~]# ansible testhost -m copy -a "src=/etc/ansible/ dest=/tmp/ansibletest owner=root group=root mode=0644"      //拷贝目录
    4、添加计划任务
    [root@tiejiangSRC1 ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/test.txt'  hour='1,5,10' weekday=1"
    注意:其他的时间表示:分钟 minute,小时 hour,日期 day,月份 month。
    5、删除任务计划
    若要删除该 cron 只需要加一个字段 state=absent
    [root@tiejiangSRC1 ~]# ansible testhost -m cron -a "name='test_cron' state=absent"
    6、yum安装
    这里用yum安装ftp工具
    [root@tiejiangSRC1 ~]# ansible testhost -m yum -a "name=httpd"
    7、服务管理
    [root@tiejiangSRC1 ~]# ansible testhost -m service -a "name=httpd state=started enabled=no"       //开启 httpd 服务,并关闭开机启动。
    8、文档使用
    1)列出所有的模块
        [root@tiejiangSRC1 ~]# ansible-doc -l
    2)查看指定模块的文档
        [root@master ~]# ansible-doc cron                    
        [root@master ~]# ansible-doc service


沪ICP备19023445号-2号
友情链接