自动化运维神器Ansible

1、Ansible的特性

no agents:不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可(不用安装客户端)

no server:无服务器端,使用时直接运行命令即可

modules in any languages:基于模块工作,可使用任意语言开发模块

yaml,not code:使用yaml语言定制剧本playbook

ssh by default:基于ssh工作

strong multi-tier solution:可实现多级指挥

模块:

connection plugins:连接插件,负责和监控端实现通信,默认使用ssh连接

host inventory:主机清单,是一个配置文件里面定义监控的主机

modules:模块,核心模块,command模块、自定义模块等

plugins:modules功能的补充,包括连接插件,邮件插件等

playbook:编排,定义ansible多任务配置文件,非必须

2、Ansible的安装

1)准备环境-关闭防火墙和selinux与传公钥

配置解析

systemctl stop firewalld && setenforce 0   #关闭防火墙和selinux
vim /etc/hosts
192.168.111.111 ansible-1
192.168.111.112 ansible-server
#配置ssh公钥认证:控制节点需要发送ssh公钥给所有的被控制的节点
[root@server ~]# ssh-keygen
[root@server ~]# ssh-copy-id -i 192.168.1.10  #所有被控节点机器

2)安装

#安装:控制节点
[root@server ~]# yum install -y epel-release   #配置EPEL网络yum源
[root@server ~]# yum install -y ansible        #安装ansible
[root@server ~]# ansible --version             #查看版本
[root@server ~]# ansible --help                #看帮助

3)Ansible基础--inventory主机清单

#查看配置文件:
[root@server ~]# rpm  -qc ansible
/etc/ansible/ansible.cfg #主要设置一些ansible初始化的信息,比如日志存放路径、模块、等配置信息 
/etc/ansible/hosts		#ansible主机清单文件
-q:---query   #查询 
语法:
#1.添加主机或者主机组:
[root@server ~]# vim /etc/ansible/hosts  #在最后追加被管理端的机器
ansible-web1        #单独指定主机,可以使用主机名称或IP地址
#2.添加主机组:
[webservers]        #使用[]标签指定主机组 ----标签自定义
192.168.10.11       #如果未解析添加ip
ansible-web2        #解析添加主机名
#3.组可以包含其他组:
[webservers1]       #组一
ansible-web1
[webservers2]       #组二
ansible-web2
[weball:children]   #children-照写 #weball包括两个子组
webservers1         #组一
webservers2         #组二
#4.为一个组指定变量,组内每个主机都可以使用该变量:
[weball:vars]         #设置变量,vars--照写
ansible_ssh_port=22     
ansible_ssh_user=root   
ansible_ssh_private_key_file=/root/.ssh/id_rsa  
#ansible_ssh_pass=test      #也可以定义密码,如果没有互传秘钥可以使用密码。

4)测试

[root@server ~]# ansible ansible-web1 -m ping -o  #指定单台机器
[root@server ~]# ansible ansible-web1,ansible-web2 -m ping -o  #指定多台机器
[root@server ~]# ansible  webservers1 -m ping -o  #指定组名
-m module_name:  #模块名称,默认为command
-a arguments:    #传递给模块的参数
-o               #横着显示(单行显示)
[root@server ~]# ansible webservers1 -m shell -a 'uptime'

5)Ad-Hoc

#列出ansible支持的模块:
-l:获取列表
-s module_name: #获取指定模块的使用信息
#看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]# ansible-doc -l
#查看模块使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum

常用模块

1.远程复制备份模块:copy
模块参数详解:  
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group:指定属组
mode:指定权限,可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
2.软件包管理 yum模块
安装apache
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=latest" -o
state=     #状态是什么,干什么
state=absent       用于remove安装包
state=latest       表示最新的
state=removed      表示卸载
卸载软件:
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=removed" -o
或者
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=absent" -o
3.服务管理service模块
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=stopped" #停止
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=restarted" #重启
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=yes" #开机启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=no"  #开机关闭
4.文件模块file
模块参数详解:  
owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
        yes:表示使用递归设置
state:
touch:创建一个新的空文件
directory:创建一个新的目录,当目录存在时不会进行修改
#创建一个文件
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/youngfit1.txt mode=777 state=touch'
[root@ansible-server ~]# ansible ansible-web2 -m file -a 'path=/tmp/youngfit2.txt mode=777 owner=nginx state=touch'
#创建一个目录
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/qf mode=777 state=directory'
5.收集信息模块setup
[root@ansible-server ~]# ansible webservers1 -m setup  #收集所有信息
[root@ansible-server ~]# ansible webservers1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址
filter:过滤

3、ansible-playbook剧本

1)playbook格式

核心元素:
Playbooks  
Variables     #变量元素,可传递给Tasks/Templates使用;  
Tasks          #任务元素,由模块定义的操作的列表,即调用模块完成任务;  
Templates   #模板元素,使用了模板语法的文本文件;  
Handlers     #处理器元素,通常指在某事件满足时触发的操作;  
Roles          #角色元素
playbook的基础组件:
name:
    定义playbook或者task的名称(描述信息),每一个play都可以完成一个任务。
hosts: 
    hosts用于指定要执行指定任务的主机.
user:
    remote_user则用于指定远程主机上的执行任务的用户
tasks:
    任务列表play的主体部分是task list. task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。
vars:
   定义变量(如果不使用内部变量需要提前定义)
vars_files:
  调用定义变量文件
notify:
    任务执行结果如果是发生更改了的则触发定义在handler的任务执行
handlers:
    用于当前关注的资源发生变化时采取一定指定的操作

实例

[root@ansible-server ~]# cd /etc/ansible/
[root@ansible-server ansible]# vim test.yml  #创建文件必须以.yml/.yaml结尾

参数解释:
    hosts: 参数指定了对哪些主机进行操作;
    user: 参数指定了使用什么用户登录远程主机操作;

tasks: 指定了一个任务.

name:参数同样是对任务的描述,在执行过程中会打印出来。

group模块参数:
name参数:必须参数,用于指定组名称。
state参数:用于指定组的状态,两个值可选,present,absent,默认为 present,设置为absent 表示删除组。
gid参数:用于指定组的gid。如果不指定为随机
system参数:如果是yes为系统组。--可选

检查语法

[root@ansible-server ansible]# ansible-playbook --syntax-check test.yml 
playbook: test.yml
运行Playbook:
[root@ansible-server ansible]# ansible-playbook test.yml #加剧本名称
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim play.yml
- hosts: webservers1
  user: root
  tasks:
  - name: create a group
    group: name=mygrp gid=2003 system=true
  - name: create a user
    user: name=tom group=mygrp system=true

- hosts: webservers2
  user: root
  tasks:
  - name: install apache
    yum: name=httpd state=latest
  - name: start httpd service
    service: name=httpd state=started

检查并执行

[root@ansible ansible]# ansible-playbook --syntax-check play.yml
[root@ansible ansible]# ansible-playbook play.yml

2)条件执行when模块

[root@ansible ansible]# cat /etc/ansible/hosts
[webservers1]
ansible-web1
ansible-web2

[root@ansible ansible]# vim when.yml
- hosts: webservers1
  user: root
  tasks:
  - name: use when
    file: state=touch path=/tmp/when.txt
  - name: insert data
    shell: echo 123 >> /tmp/when.txt          #2在执行这个模块命令
    when: ansible_hostname == "ansible-web1"  #1.先条件执行,先判断when是否成立,如果成立则执行上面命令,ansible-web1指的是被控节点上真正的主机名称
执行
[root@ansible ansible]# ansible-playbook when.yml
[root@ansible-web1 ~]# cat /tmp/when.txt 
123
[root@ansible-web2 ~]# cat /tmp/when.txt

3)使用变量并不显示搜集主机相关信息

[root@ansible ansible]# vim create_user.yml
- hosts: ansible-web1
  user: root
  gather_facts: false  #是否执行setup模块,搜集对方机器的信息
  vars:                #自定义变量
  - user: "jack"       #user是自定义变量名称,“jack”是变量值
  - src_path: "/root/a.txt"    #同上
  - dest_path: "/mnt/"
  tasks:
  - name: create user
    user: name={
   
   { user }}
  - name: copy file
    copy: src={
   
   { src_path }} dest={
   
   { dest_path }}
[root@ansible ansible]# vim /root/a.txt  #创建测试文件
123

执行

[root@ansible ansible]# ansible-playbook create_user.yml

Role角色

两台机器配置本地解析
[root@ansible-server ~]# vim /etc/hosts
192.168.1.9    ansible-server
192.168.1.13   ansible-web4
[root@ansible-web4 ~]# vim /etc/hosts
192.168.1.9    ansible-server
192.168.1.13   ansible-web4
添加主机组
[root@ansible-server ansible]# pwd
/etc/ansible
[root@ansible-server ansible]# vim hosts
[webservers4]
ansible-web4
配置免密登录:
[root@ansible-server ~]# ssh-copy-id -i 192.168.1.13

检测语法:
[root@ansible-server ansible]# ansible-playbook --syntax-check list.yml 
playbook: list.yml
执行:
[root@ansible-server ansible]# ansible-playbook  list.yml

 自定义vars_files变量

创建变量目录:
[root@ansible-server ~]# mkdir /etc/ansible/vars
[root@ansible-server ~]# cd /etc/ansible/vars/
[root@ansible-server vars]# vim file.yml     #创建变量文件。
src_path: /root/test/a.txt
dest_path: /opt/test/
创建一个测试文件
[root@ansible-server vars]# mkdir /root/test
[root@ansible-server vars]# vim /root/test/a.txt  #编辑测试文件
123
创建play-book引用变量文件:
[root@ansible-server vars]# cd /etc/ansible/
[root@ansible-server ansible]# vim vars.yml
- hosts: ansible-web1
  user: root
  vars_files:
   - /etc/ansible/vars/file.yml
  tasks:
   - name: create directory
     file: path={
   
   { dest_path }} mode=755 state=directory
   - name: copy file
     copy: src={
   
   { src_path }} dest={
   
   { dest_path }}
检测语法:
[root@ansible-server vars]# cd ..
[root@ansible-server ansible]# ansible-playbook --syntax-check vars.yml
playbook: vars.yml
执行:
[root@ansible-server ansible]# ansible-playbook  vars.yml

登录查看:

实战:通过playbook安装apache
1.准备工作:
[root@ansible-server ansible]# vim hosts     #添加主机web3
[webservers3]
ansible-web3
2.安装apache,准备配置文件
[root@ansible-server ~]# yum install -y httpd
[root@ansible-server ~]# mkdir /apache    
[root@ansible-server ~]# cp /etc/httpd/conf/httpd.conf /apache/  #将配置文件推送到web3
3.修改端口将原来的80修改为8080
[root@ansible-server ~]# vim /apache/httpd.conf
Listen 8080
[root@ansible-server ~]# cd /etc/ansible/  #编写剧本
[root@ansible-server ansible]# vim apache.yml
---
- hosts: webservers3
  user: root
  tasks:
  - name: install apache
    yum: name=httpd state=latest
  - name: copy conf file
    copy: src=/apache/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: start httpd
  handlers:
  - name: start httpd
    service: name=httpd state=restarted
语法检测:
[root@ansible-server ansible]# ansible-playbook --syntax-check apache.yml 
playbook: apache.yml
执行play-book
[root@ansible-server ansible]# ansible-playbook apache.yml

猜你喜欢

转载自blog.csdn.net/qq_50660509/article/details/120790480