Ansible 自动化运维工具详解

一:Ansible介绍

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。

我要讲一些Ansible的安装和一些基本概念,然后我们会开始研究一些真正有意思的东西 – playbook,配置管理,部署以及语法编排.我将会写出如何使用/usr/bin/ansible执行ad-hoc并行命令,还会列出ansible的核心有什么样的模块可供使用.当然以后你也可以写你自己的模块。

二:Ansible安装部署

ansible安装需要有epel源。如果您没有安装epel源可以执行我下面的安装命令,该命令是从阿里源镜像安装的epel源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install ansible   #安装ansible

在安装部署这里可以vim /etc/hosts ,把客户机的域名写上去。

三:ssh-key(可选)

给所有客户机设置免密码ssh-key的方式。这样后面进行测试的时候会方便很多。

ssh-keygen   #生成秘钥
ssh-copy-id 192.168.133.160  #把秘钥发送给ip为192.168.133.160的主机

四:Ansible基础

1.定义一下主机清单,vim /etc/ansible/hosts,在这里写上客户机的IP

2.测试连通性 ansible   localhost   -m ping    -m 指定模块。什么功能

3.简洁输出    ansible host1 -m ping  -o 

4.know_hosts    ansible host2 -m ping -u root -k 用户密码 -o  增加用户名选项,增加密码选项  注意:这里的host2主机域名是已经在主机清单中写过的。

五:Inventory -主机清单

1 增加主机组  vim /etc/ansible/hosts

[webserver]        #webserver这是主机组名,下面的四个都是组成员
host1
host2
host3
host4

2 增加用户名 密码    vim /etc/ansible/hosts,如果已经做过了免密登录那就可以不做这一步。

[webserver]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666' #组成员host1到host4,这是个简便写法,user是root,pass是666666

3 组:变量 vim /etc/ansible/hosts

[webserver]
host[1:4]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'

常用变量

4 子分组

将不同的分组进行组合  vim /etc/ansible/hosts

[apache]  #组1
host[1:2] #组1中的成员
[nginx]    #组2
host[3:4]    #组2中的成员
[webserver:children] #webserver组,children参数是子分组的意思
apache    #子分组一
nginx    #子分组二
[webserver:vars]        #webserver的变量
ansible_ssh_user='root'
ansible_ssh_pass='666666'

5 自定义主机列表 vim hostlist 

[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
ansible -i  hostlist dockers  -m ping  -o

六:Ad-Hoc-点对点模式

临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。

1.shell模块   调用帮助:ansible-doc shell

ansible webserver -m shell -a 'hostname' -o -f 2 #获取主机名,-f2指定线程数,一次并发执行的线程数
ansible host2 -m shell -a 'yum -y install httpd' -o  #部署apache

2.复制模块    调用帮助:ansible-doc copy

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'  #src后面是原文件,dest后面是目标目录和修改后的文件名,owner用户,group用户组,mode权限,backup=yes备份,如果文件有多份,可以进行备份。

3.用户模块     调用帮助:ansible-doc user

ansible webserver -m user -a 'name=qianfeng state=present'    #创建用户
ansible webserver -m user -a 'name=qianfeng state=absent'     #删除用户
echo '777777' | openssl passwd -1 -stdin                        #生成加密密码
ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."' #修改密码
ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes' #修改shell

4.软件包管理     调用帮助:ansible-doc yum

ansible host2 -m yum -a 'name="httpd" state=latest'    #安装apache
ansible host2 -m yum -a 'name="httpd" state=absent'     #卸载apache

5.服务模块       调用帮助:ansible-doc service

ansible host2 -m service -a 'name=httpd state=started'    #启动apache
ansible host2 -m service -a 'name=httpd state=started enabled=yes'  #apache开机自启
ansible host2 -m service -a 'name=httpd state=stopped'        #关闭apache程序
ansible host2 -m service -a 'name=httpd state=restarted'        #重启apache程序
ansible host2 -m service -a 'name=httpd state=started enabled=no'    #开机禁止启动

6.文件模块     调用帮助:ansible-doc file   

ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'     #创建文件
ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory'     #创建目录

还有其他很多参数,我这里就不一一列举了,可以通过上面的帮助逐一查看。

7.收集模块      调用帮助:ansible-doc 

ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses' #查询IPv4地址

7.YAML-YAML Ain’t Markup Language-非标记语言

 

示例,编写一个剧本,完成web的部署,配置,启动的全过程。

vim apache.yaml
- hosts: host2    #针对host2主机做
  tasks:            #任务
  - name: install apache packages    #描述
    yum: name=httpd state=present    #安装httpd
  - name: copy apache conf            #描述
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf    #将本机的apache配置文件复制为        host2的配置文件。
    notify: restart apache service    #引用处理程序
  - name: ensure apache is running    #描述
    service: name=httpd state=started enabled=yes    #开启apache服务,设置为开机自启
handlers:                                #定义处理程序,如果没有被 notify,handlers 不会执行
    - name restart apache service
    service: name=httpd state=restarted

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值