本文目录
一、Ansible 简介
- absible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
- 我们需要学一些Ansible的安装和一些基本概念,然后我们会开始研究一些真正有意思的东西-playbook配置管理,部署以及语法编排。我们会学习如何使用/usr/bin/ansible执行an-hoc并行命令,我们还会学习ansible的核心有什么样的模块可供使用,当然以后你也可以写你自己的模块。
二、基本架构
上图为ansible的基本架构,从上图可以了解到其由以下部分组成:
-
核心:ansible
-
核心模块(Core Modules):这些都是ansible自带的模块
-
扩展模块(Custom Modules):如果核心模块不足以完成某种功能,可以添加扩展模块
-
插件(Plugins):完成模块功能的补充
-
剧本(Playbooks):ansible的任务配置文件,将多个任务定义在剧本中,由ansible自动执行
-
连接插件(Connectior Plugins):ansible基于连接插件连接到各个主机上,虽然ansible是使用ssh连接到各个主机的,但是它还支持其他的连接方法,所以需要有连接插件
-
主机群(Host Inventory):定义ansible管理的主机
三、Ansible部署
1.环境
五台设备
192.168.145.200 ansible
192.168.145.201 host1
192.168.145.207 host2
192.168.145.208 host3
192.168.145.209 host4
域名解析
[root@ansible ~]# vim /etc/hosts 192.168.145.200 ansible 192.168.145.201 host1 192.168.145.207 host2 192.168.145.208 host3 192.168.145.209 host4
2.安装ansible
在ansible服务器上
[root@ansible ~]# yum install -y epel-release #安装epel源 [root@ansible ~]# yum install -y ansible #安装ansible
检查ansible是否安装成功
[root@ansible ~]# rpm -ql ansible #列出所有文件 # q指使用询问模式,当遇到任何问题时,rpm指令会先询问用户;l指显示套件的文件列表;c指只列出组态配置文件 [root@ansible ~]# rpm -qc ansible #查看配置文件 [root@ansible ~]# ansible --help #查看ansible操作手册 [root@ansible ~]# ansible-doc -l #查看所有模块 [root@ansible ~]# ansible-doc -s yum #查看yum模块
3.ssh-key(可选)
为了让ansible能够免密控制其它服务器,将生成秘钥并拷贝到其它服务器(要进行控制的服务器上)
[root@ansible ~]# ssh-keygen #生成秘钥 [root@ansible ~]# ssh-copy-id host1 #拷贝秘钥到其它服务器。注意,这里只拷贝了host1,其它机器没有拷贝 [root@ansible ~]# ssh-copy-id host2 [root@ansible ~]# ssh-copy-id host3 [root@ansible ~]# ssh-copy-id host4
4.ansible基础
①定义主机清单
在ansible服务器上[root@ansible ~]# vim /etc/ansible/hosts 添加以下三行内容。没有添加主机清单的服务器,ansible会ping不通 host1 host2 host3 host4
②测试连通性
在ansible服务器上
[root@ansible ~]# ansible localhost -m ping # 测试对本地的连通性 [root@ansible ~]# ansible host1 -m ping #测试对host1的连通性
③简洁输出
在ansible服务器上
[root@ansible ~]# ansible host1 -m ping -o #-o指简洁输出(行输出)
④know_hosts
在ansible服务器上
[root@ansible ~]# ansible host2 -m ping #结果会显示失败,因为host2上没有免密登录秘钥 [root@ansible ~]# ansible host2 -m ping -u root -k -o #表明身份后即可成功
去掉(yes/no)的询问 (可选)
[root@ansible ~]# vim /etc/ssh/ssh_config 将下行内容注释解除,再将ask改为no StrictHostKeyChecking no [root@ansible ~]# systemctl restart sshd #重启生效
⑤请注意ping和ssh的区别
ping ICMP:网际消息管理协议 ssh 应用程安全登录协议 实验如下: 关闭host1主机sshd进程,再在ansible服务器上ping一下host1服务器,发现可以Ping通 [root@ansible ~]# systemctl stop sshd [root@ansible ~]# ping host1 再在ansible服务器上使用ansible对host1进行连通测试,结果却是失败的 [root@ansible ~]# ansible host1 -m ping 结论:ansible的ping,是探测ssh程序是否连接,不是icmp协议
四、Inventory-主机清单 ★
含义:
清查;存货清单;财产目录;主机清单
1.增加主机组
[root@ansible ~]# vim /etc/ansible/hosts 添加以下内容
[webserver] #主机组名
host1
host2
host3
host4
调用组。这里如果没有做免密,实验会报错,但是不影响该实验
[root@ansible ~]# ansible webserver -m ping -o #-o指以行的形式展示
2.增加用户名和密码
[root@ansible ~]# vim /etc/absible/hosts
[webserver]
host1 ansible_ssh_user='root' ansible_ssh_pass='666666'
host2 ansible_ssh_user='root' ansible_ssh_pass='666666'
host3 ansible_ssh_user='root' ansible_ssh_pass='666666'
host4 ansible_ssh_user='root' ansible_ssh_pass='666666'
#也可以这样正则写法:host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
3.增加端口
请将host1的sshd程序端口修改为2222(随机)
[root@host1 ~]# vim /etc/ssh/sshd_config 以下一行内容解除注释,并将22修改为2222 Port 2222 #1024~65535之间的端口都可以 [root@host1 ~]# systemctl restart sshd
在随机的host2服务器上对host1进行远程登录测试
[root@host2 ~]# ssh root@192.168.145.201 -p 2222 #再次远程登录host1时需要指定端口号才可以登录
如果想要ansible能够正常连接的话,需要在ansible里添加指定端口号
在ansible服务器上
[root@ansible ~]# vim /etc/ansible/hosts 添加以下内容 ansible_ssh_port='2222' [webserver] host1 ansible_ssh_user='root' ansible_ssh_pass='666666' ansible_ssh_port='2222' host2 ansible_ssh_user='root' ansible_ssh_pass='666666' host3 ansible_ssh_user='root' ansible_ssh_pass='666666' host4 ansible_ssh_user='root' ansible_ssh_pass='666666' 最后进行连通测试,显示为成功,说明组变量设置成功 [root@ansible ~]# ansible webserver -m ping -o
4.组:变量
ansible内部变量可以帮助我们简化主机清单的设置,如下:
在ansible服务器上
[root@ansible ~]# vim /etc/ansible/hosts 内容如下 [webserver] # webserver组 host1 ansible_ssh_port='2222' host2 host3 host4 [webserver:vars] # webserver组的变量 ansible_ssh_user='root' ansible_ssh_pass='666666' 最后进行连通测试,显示为成功,说明组变量设置成功 [root@ansible ~]# ansible webserver -m ping -o
5.子分组
将不同的分组进行组合
在ansible服务器上 例子如下: [root@ansible ~]# vim /etc/absible/hosts 内容如下 [apache] #apache组 host[1:2] [nginx] #nginx组 host[3:4] [webserver:children] # children指孩子 apache nginx [webserver:vars] # webserver组的变量 ansible_ssh_user='root' ansible_ssh_pass='666666' 最后进行连通测试,显示为成功。webserver调用了两个孩子apache和nginx组 [root@ansible ~]# ansible webserver -m ping -o
6.自定义主机列表
在ansible服务器上
[root@ansible ~]# pwd #当前路径在/root
[root@ansible ~]# vim hostlist #新建主机列表(清单)
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
这里可以假设 hostlist 这个文件是其它机器拷贝到ansible服务器上的,
请问:ansible如何使用它,命令如下:
ansible -i hostlist dockers -m ping -o
# -i链接外部主机清单,hostlist文件名,dockers组名。如果放在其它路径,请打绝对路径
五、Ad-Hoc-点对点模式 ★
简介:
临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为playbook。
1.shell模块
简介:
shell模板其实就是ansible基本语句+shell命令(正常的shell命令)即可执行。-a '单引号里面写正常的shell命令即可'
①帮助
ansible-doc shell
②查看webserver组成员的主机名
ansible webserver -m shell -a 'hostname' -o
③查看webserver组成员的主机名
ansible webserver -m shell -a 'hostname' -o -f 2 # -f指指定线程数
④对webserver组进行安装mariadb数据库
ansible webserver -m shell -a 'yum -y install mariadb-server' -o
2.复制模块
在ansible服务器上
[root@ansible ~]# vim /etc/absible/hosts 先定义主机列表坏境,添加以下内容 [webserver] #webserver组 host[1:4] [webserver:vars] # webserver组的变量 ansible_ssh_user='root' ansible_ssh_pass='666666'
执行以下拷贝命令,将/etc/hosts文件拷贝到其它服务器上 [root@ansible ~]# ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' # -a指属性,src指源头,dest指目的地,owner指属主,group指属组,mode指权限 [root@ansible ~]# ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/3.txt owner=root group=bin mode=777 backup=yes' #加了一个backup='yes',表示内容修改后不会被覆盖,会生成新文件,后缀不同,如下图
使用backup的前提是文件一定是被修改过的
3.用户模块
①帮助
ansible-doc user
②创建用户
ansible webserver -m user -a 'name=qianfeng state=present'
③删除用户
ansible webserver -m user -a 'name=qianfeng state=absent'
④修改密码
生成加密密码,然后复制密码 echo '520520' | openssl passwd -1 -stdin #-1指密码类型,-stdin指标准输入 修改密码 ansible webserver -m user -a 'name=qianfeng password="$1$y7KsAm5X$LXGqbGXWcXhdVMs0VqsS11"'
⑤修改shell
ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes' #append指追加修改
4.软件包管理模块
①帮助
ansible-doc yum
②升级所有包
ansible host1 -m yum -a 'name="*" state=latest'
③安装apache
ansible webserver -m yum -a 'name="httpd" state=latest'
④卸载apache
ansible webserver -m yum -a 'name="httpd" state=absent'
5.服务模块
①模块
ansible-doc service ansible-doc servic
②启动webserver组的http服务
ansible webserver -m service -a 'name="httpd" state=started'
③设置webserver组的http服务开机自启
ansible webserver -m service -a 'name="httpd" state=started enabled=yes' #开机禁止启动就换成"no"
④关闭webserver组的http服务
ansible webserver -m service -a 'name="httpd" state=stopped'
6.文件模块
①帮助
absible-doc file
②创建文件
ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch' #如果创建目录就把touch改成"directory"
7.收集模块
帮助
ansible-doc setup
查看host1主机的所有设备信息
ansible host1 -m setup
收集host1主机信息,并过滤出IP地址
ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'
收集host1主机信息,并过滤出处理器信息
ansible host1 -m setup -a 'filter=ansible_processor'
六、YAML-YAML Ain't Markup Language-非标记语言
1.语法
列表 fruits: -Apple -Orange -Strawberry -Mango 字典 martin: name: Martin D'vloper job: Developer skill: Elite
2.示例
需求:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程
这里实验的环境为"三、Ansible部署--1.环境"
在ansible服务器上 ansible all -m yum -a 'name=httpd state=removed' -o #卸载服务器的所有httpd ansible all -m yum -a 'name=httpd-tools state=removed' -o #卸载服务器的所有httpd-tools yum -y install httpd #安装httpd mkdir apache cd apache cp /etc/httpd/conf/httpd.conf . grep '^Listen' httpd.conf sed -ri 's/Listen 80/Listen 8080/' httpd.conf
vim apache.yaml #编写剧本 - hosts: webserver tasks: #任务 - name: install apache packages #第一个任务名字(自定义) yum: name=httpd state=present #第一个任务模块 - name: copy apache conf #第二个任务名字 copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf #第二个任务模块 notify: restart apache service for xulei #引用处理程序(notify指通知) - name: ensure apache is running #第三个任务名字 service: name=httpd state=started enabled=yes #第三个任务模块 handlers: - name: restart apache service for xulei #任务 (定义处理程序) service: name=httpd state=restarted
测试剧本 ansible-playbook apache.yaml --syntax-check #检查剧本语法是否正确 ansible-playbook apache.yaml --list-tasks #列出剧本的任务名字 ansible-playbook apache.yaml --list-hosts #列出剧本的主机名字
执行剧本 ansible-playbook apache.yaml
七、Role-角色扮演
1.简介
roles则是在ansible中,playbook的目录组织结构。
将代码或文件进行模块化,成为roles的文件目录组织结构。
易读,代码可重用,层次清晰。
2.目标
通过reles远程部署nginx部署。请看如下操作
3.目录结构及实战
以下实验全部都在zabbinx服务器上操作
这里实验的环境为"三、Ansible-部署" (zabbix主机列表)
①准备目录
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml echo 1234 > roles/nginx/files/index.html yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
使用tree命令查看结构。如下图,
tree roles
②编写任务
vim roles/nginx/tasks/main.yaml 以下空格不能少,格式要求 --- - name: install epel-release packge yum: name=epel-release state=latest - name: install nginx packge yum: name=nginx state=latest - name: copy index.html copy: src=index.html dest=/usr/share/nginx/html/index.html - name: copy nginx.conf template template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx - name: make sure nginx service running service: name=nginx state=started enabled=yes
③准备配置文件
vim roles/nginx/templates/nginx.conf.j2 修改以下两行内容 worker_processes {{ ansible_processor_cores }}; #调用内部已知变量 worker_connections {{ worker_connections }}; #自定义变量
④编写变量
vim roles/nginx/vars/main.yaml worker_connections: 10240
⑤编写处理程序
vim roles/nginx/handlers/main.yaml --- - name: restart nginx service: name=nginx state=restarted
⑥编写剧本
vim roles/site.yaml - hosts: webserver roles: #角色 - nginx #这里的nginx对应的是:roles里nginx该文件夹名称
⑦实施
cd roles ansible-playbook site.yaml --syntax-check #先检查语法是否正确 ansible-playbook site.yaml #执行剧本
执行完后验证
在host1主机上查看剧本执行后的状态 systemctl status nginx #检查nginx的状态 netstat -anpt | grep nginx #检查nginx的80端口是否正常启动 vim /etc/nginx/nginx.conf #检查nginx配置文件里的两个变量值,如图一 192.168.145.201:80 #真机浏览器访问nginx主页,如图二
图一
图二
此时,说明roles剧本角色实验成功。