前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ansible安装配置及实例

Ansible安装配置及实例

原创
作者头像
刘远飞
修改2017-06-19 19:08:24
2.3K0
修改2017-06-19 19:08:24
举报
文章被收录于专栏:刘远飞的专栏刘远飞的专栏

一、简介

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

代码语言:javascript
复制
在 master 上
[root@tiejiangSRC1 ~]# vim /etc/sysconfig/network
    HOSTNAME=master.test.com

在 slaver 上
[root@tiejiangSRC1 ~]# vim /etc/sysconfig/network
    HOSTNAME=slaver.test.com

4、安装

代码语言:javascript
复制
[root@tiejiangSRC1 ~]# yum install -y epel-release 
[root@tiejiangSRC1 ~]# yum install -y ansible

5、SSH密钥配置

代码语言:javascript
复制
在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配置

代码语言:javascript
复制
[root@tiejiangSRC1 ~]# vim /etc/ansible/hosts
    [testhost]
    192.168.2.71
    192.168.2.72
    192.168.2.73

三、ansible 实例

1、远程执行命令

代码语言:javascript
复制
[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,针对某一台机器来执行命令。如下:

代码语言:javascript
复制
[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脚本

代码语言:javascript
复制
[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、拷贝文件和目录

代码语言:javascript
复制
[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、添加计划任务

代码语言:javascript
复制
[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、删除任务计划

代码语言:javascript
复制
若要删除该 cron 只需要加一个字段 state=absent
[root@tiejiangSRC1 ~]# ansible testhost -m cron -a "name='test_cron' state=absent"

6、yum安装

代码语言:javascript
复制
这里用yum安装ftp工具
[root@tiejiangSRC1 ~]# ansible testhost -m yum -a "name=httpd"

7、服务管理

代码语言:javascript
复制
[root@tiejiangSRC1 ~]# ansible testhost -m service -a "name=httpd state=started enabled=no"       //开启 httpd 服务,并关闭开机启动。

8、文档使用

代码语言:javascript
复制
1)列出所有的模块
    [root@tiejiangSRC1 ~]# ansible-doc -l
2)查看指定模块的文档
K    [root@master ~]# ansible-doc cron                    
    [root@master ~]# ansible-doc service

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、安装配置
    • 1、准备工作
      • 2、编辑 hosts 文件
        • 3、设置 hostname
          • 4、安装
            • 5、SSH密钥配置
              • 6、ansible配置
              • 三、ansible 实例
                • 1、远程执行命令
                  • 2、远程执行Shell脚本
                    • 3、拷贝文件和目录
                      • 4、添加计划任务
                        • 5、删除任务计划
                          • 6、yum安装
                            • 7、服务管理
                              • 8、文档使用
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档