Ansible部署及使用

.什么是ansible

    什么是ansible?官方的title“Ansibleis Simple IT Automation”—>简单的自动化IT工具。ansible功能:自动化部署APP;自动化管理配置项;自动化的持续交付;自动化的(AWS)云服务管理。其本质就是在一个台或者几台服务器上,批量的执行命令。

       fabricansible有什么差别呢?简单来说fabric像是一个工具箱,提供了很多好用的工具,用来在Remote执行命令,而ansible则是提供了一套简单的流程,你要按照它的流程来做,就能轻松完成任务。当然,它们之间也是有共同点的——都是基于paramiko 开发的。paramiko是什么呢?它是一个纯Python实现的ssh协议库。因此fabricansible还有一个共同点就是不需要在远程主机上安装client/agent,因为它们是基于ssh来和远程主机通讯的。

Ansible的执行过程如下图所示,暖色调带便已经模块化。

wKioL1OZPP6REqMvAAKGlzjqxcQ006.jpg

.Ansible的安装

   配置好yum,直接yum -yinstall ansible即可。

   

yum -y install ansible

安装完成后会在/etc 下生成两个文件:ansible的配置文件ansible.cfg和定义被管理节点的hosts.


三.Ansible的具体使用

 Ansible Server:10.0.0.122   

 节点信息:

cat /etc/ansible/hosts

10.0.0.128 #nginx01
10.0.0.129 #nginx02
10.0.0.130 #nginx03

建立Server及各节点的信任关系:

ssh-keygen -t rsa -P '' -f .ssh/id_rsa
ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.128--10.0.0.130

ansible命令的用法:

建立完信任关系后,测试下各节点与Server的连通性:

[root@cobbler ~]# ansible all -m ping #all表示所有节点,-m指定模块 默认为command,ping指定动作
10.0.0.129 | success >> {
    "changed": false, 
    "ping": "pong"
}
10.0.0.130 | success >> {
    "changed": false, 
    "ping": "pong"
}
10.0.0.128 | success >> {
    "changed": false, 
    "ping": "pong"
}

ansible批量执行命令示例:

[root@cobbler ~]# ansible all -a 'uptime' #all所有节点, -a用于指定命令,‘uptime’执行的命令,如果命令有参数一定要用引号;ansible默认使用command模块,所以可以不用指定。
10.0.0.130 | success | rc=0 >>
 11:31:50 up  2:01,  1 user,  load average: 0.00, 0.00, 0.00
10.0.0.129 | success | rc=0 >>
 11:31:50 up  2:02,  1 user,  load average: 0.00, 0.00, 0.00
10.0.0.128 | success | rc=0 >>
 11:31:50 up  2:05,  1 user,  load average: 0.00, 0.00, 0.00
 
 [root@cobbler ~]# ansible-doc -l #查看ansible的所有模块
acl                  Sets and retrieves file ACL information.                    
add_host             add a host (and alternatively a group) to the ansible-playbo
airbrake_deployment  Notify airbrake about app deployments                       
apt                  Manages apt-packages

[root@cobbler ~]# ansible-doc yum #查看指定模块的文档
> YUM

  Installs, upgrade, removes, and lists packages and groups with the
  `yum' package manager.
  
 [root@cobbler ~]# ansible all -m yum -a "name=httpd state=present" #用yum模块给各节点安装httpd。name指定安装包名,state指定动作:安装可以用present和latest,卸载使用absent。
10.0.0.128 | success >> {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed"
    ]
}

10.0.0.130 | success >> {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed"
    ]
}

10.0.0.129 | success >> {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed"
    ]
}

ansible执行shell比较麻烦,建议安装ansible-shell

ansible-shell内置的命令主要有四个:

cd :     切换到指定的组/表达式筛选的机器集合上

list:    显示目前的机器集合,list groups 可以列出所有的组(对我们可能没啥用)

serial:运行时的并发度,默认是20

help:  顾名思义,他能生成简单的模块帮助信息,方便即时查询

yum -y install git
git clone https://github.com/dominis/ansible-shell.git 
yum -y install python-pip
pip install -e ./ansible-shell
[root@cobbler ~]# ansible-shell
Welcome to the ansible-shell.
Type help or ? to list commands.
root@ (0)[s:2]$ cd 10.0.0.128   #cd到指定host。
[email protected] (1)[s:2]$ ls    #list出了10.0.0.128这个主机上/root下的文件
=============== 10.0.0.128          ================
anaconda-ks.cfg
install.log
install.log.syslog
nginx-1.4.7
nginx-1.4.7.tar.gz
nginx_install.sh

其他功能还在摸索中,用到后再做补充。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章