ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
一、安装ansible
1、域名解析
① ansible服务器
这里只使用两台虚拟机,一个为服务器,一个为客户端。
[ansible-server]#vim /etc/hosts
192.168.0.115 ansible
192.168.0.104 host1
② ansible客户机
不需要做任何配置。
2、安装ansible
yum install -y epel-release //在ansible服务器上安装epel源
yum -y install ansible //安装ansible
检测部署是否完成
rpm -ql ansible //列出所有文件
rpm -qc ansible //查看配置文件
ansible --help //查看帮助
ansible-doc -l //看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
ansible-doc -s yum
//看yum模块,了解其功能
install (`present' or `installed', `latest'), or remove (`absent' or `removed')
yum list
Package name
enablerepo
二、ssh-key(可选)
做了免密,下一步就不需要输密码了。
免密码ssh-key的方式。
ssh-keygen
ssh-copy-id IP地址
推送公钥
三、ansible基础
1、定义主机清单
在配置文件里写客户机的域名
vim /etc/ansible/hosts
host1
2、测试连通性
ansible localhost -m ping
// -m 指定模块。什么功能
ping只是其中一个模块。还有shell,yum等等
3、简洁输出
ansible host1 -m ping -o
//-o 简洁输出 呈一行
4、konw-hosts
ansible host1 -m ping
//失败了
ansible host1 -m ping -u root -k -o //如果做了免密就不需要输密码了
//增加用户名选项
//增加密码选项
//去掉(yes/no)的询问
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
systemctl restart sshd
ansible host1 -m ping -u root -k -o
//成功不提示
四、inventory-主机清单
含义:清查;存货清单;财产目录;主机清单
1、增加主机组
vim /etc/ansible/hosts
[webserver]
host1
host2
host3
host4
ansible webserver -m ping -o
输出提示
[root@localhost ~]# ansible webserver -m ping -u root -k -o
SSH password:
host3 | SUCCESS => {"changed": false, "ping": "pong"}
host1 | SUCCESS => {"changed": false, "ping": "pong"}
host4 | SUCCESS => {"changed": false, "ping": "pong"}
host2 | SUCCESS => {"changed": false, "ping": "pong"}
2、增加用户名,密码
vim /etc/ansible/hosts
[webserver]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
ansible webserver -m ping -o
//免用户名和密码成功
ps:当主机和主机的用户名和密码不相同时
[webservers]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
3、增加端口
请将host1的sshd程序端口修改为2222
vim /etc/ssh/ssh_config
Port 2222
systemctl restart sshd
ansible webservers -m ping -o //失败,因为默认端口已更改
vim /etc/ansible/hosts
[webservers]
host1 ansible_ssh_user='root' ansible_ssh_password='777777' ansible_ssh_port='2222'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
//请将用户名密码和端口恢复原状
4、组:变量
ansible内部变量可以帮助我们简化主机清单的设置
vim /etc/ansible/hosts
[webserver]
host[1:4]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
常用变量
5、子分组
vim /etc/ansible/hosts
[apache]
host[1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
6、自定义主机列表
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 //获取主机名
ansible webserver -m shell -a 'hostname' -o -f 2
//-f 2 指定线程数
// -f FORKS, --forks=FORKS Ansible一次命令执行并发的线程数。NUM被指定为一个整数,默认是5, specify number of parallel processes to use (default=5)
ansible host2 -m shell -a 'yum -y install httpd' -o //部署apache
ansible host3 -m shell -a 'uptime' -o
//查询系统负载
2、复制模块
帮助:ansible-doc copy
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'
//如果文件有多份,可以进行备份。
[root@localhost ~]# ls /tmp/
2.txt 2.txt.17037.2017-11-16@16:23:41~
3、用户模块
帮助:ansible-doc user
创建用户:
ansible host1 -m user -a 'name=nihao state=present'
修改密码:
1、生成加密密码
echo '777777' | openssl passwd -1 -stdin
//生成加密密码值
//$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272.
2.修改密码
ansible host1 -m user -a 'nihao password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
修改shell:
ansible host1 -m user -a 'name=nihao shell=/sbin/nologin append=yes'
删除用户:
ansible host1 -m user -a 'name=qianfeng state=absent'
4、软件包管理
ansible host1 -m yum -a 'name="*" state=latest'
//升级所有包
[root@slave2 ~]# ansible host1 -m yum -a 'name="httpd" state=latest'
host1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"changes": {
"installed": [],
"updated": []
},
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date",
""
]
}
在host1上查看是否安装成功
[root@master1 ~]# yum list | grep httpd
httpd.x86_64 2.4.6-93.el7.centos @base
httpd-tools.x86_64 2.4.6-93.el7.centos @base
5、服务模块
[root@slave2 ~]# ansible host1 -m service -a 'name=httpd state=started' //启动
[root@slave2 ~]# ansible host1 -m service -a 'name=httpd state=started enabled=yes'
//开机自启
[root@slave2 ~]# ansible host1 -m service -a 'name=httpd state=stopped' //停止
[root@slave2 ~]# ansible host1 -m service -a 'name=httpd state=restarted'
//重新启动
ansible host1 -m service -a 'name=httpd state=started enabled=no'
//开机禁止启动
6、文件模块
创建一个新的文件。
[root@slave2 ~]# ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=777 state=touch'
host1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/88.txt",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
在host1上查看文件
[root@master1 ~]# ls /tmp/88.txt -l
-rwxrwxrwx 1 root root 0 9月 13 16:32 /tmp/88.txt
创建文件夹
[root@slave2 ~]# ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory'
host1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"path": "/tmp/99",
"size": 6,
"state": "directory",
"uid": 0
}
查看文件夹
[root@master1 ~]# ls /tmp/99 -d -l
drwxrwxrwx 2 root root 6 9月 13 16:36 /tmp/99
7、收集模块
帮助:ansible-doc setup
[root@slave2 ~]# ansible host1 -m setup //查询所有信息
[root@slave2 ~]# ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'
host1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.8.162.122"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
六、YAML-YAML Ain’t Markup Language-非标记语言
1、语法
列表:
fruits:
- Apple
- Orange
- Strawberry
- Mango
字典:
martin:
name: Martin D'vloper
job: Developer
skill: Elite
2、示例
需求:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
[root@ansible ~]# ansible all -m yum -a 'name=httpd state=removed' -o //清理环境
[root@ansible ~]# yum install -y httpd //准备配置文件
[root@ansible ~]# mkdir apache
[root@ansible ~]# cd apache
[root@ansible apache]# cp -rf /etc/httpd/conf/httpd.conf .
[root@ansible apache]# grep '^Listen' httpd.conf
Listen 80
Listen 8080 //修改配置,用作推送
[root@ansible apache]# vim apache.yaml //编写剧本
- hosts: host2
tasks:
- name: install apache packages
yum: name=httpd state=present
- name: copy apache conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache is running
service: name=httpd state=started enabled=yes
测试
[root@ansible apache]# ansible-playbook apache.yaml --syntax-check
playbook: apache.yaml
[root@ansible apache]# ansible-playbook apache.yaml --list-tasks
playbook: apache.yaml
play #1 (host1): host1 TAGS: []
tasks:
install apache packages TAGS: []
copy apache conf TAGS: []
ensure apache is running TAGS: []
[root@ansible apache]# ansible-playbook apache.yaml --list-hosts
playbook: apache.yaml
play #1 (host1): host1 TAGS: []
pattern: [u'host1']
hosts (1):
host1
[root@ansible apache]# ansible-playbook apache.yaml
PLAY [host1] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [host1]
TASK [install apache packages] *************************************************
changed: [host1]
TASK [copy apache conf] ********************************************************
changed: [host1]
TASK [ensure apache is running] ************************************************
changed: [host1]
PLAY RECAP *********************************************************************
host1 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
去访问网站:
http://192.168.2.142:8080/
注意端口
handles
如果配置文件发生变化 : Listen 9000
ansible-playbook apache.yaml
//再次执行,命令成功,但配置未生效,所以要增加处理程序。设置触发器
增加触发器:
vim apache.yaml
//如果配置文件再发生变化。
Listen 9080
ansible-playbook apache.yaml
// 再次执行,配置生效,触发成功
七、Role-角色扮演
roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
本次实验目标:
通过role远程部署nginx并配置
1、目录结构
图示:
nginx 角色名
files 普通文件
handlers 触发器程序
tasks 主任务
templates 金甲模板(有变量的文件)
vars 自定义变量
准备目录结构
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
2、编写任务
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 //template:模板
notify: restart nginx //notify:通知(钩子),在该模块配置发生改变
- name: make sure nginx service running
service: name=nginx state=started enabled=yes
3、准备配置文件
vim roles/nginx/templates/nginx.conf.j2
worker_processes {{ ansible_processor_cores }};
//调用内部已知变量
worker_connections {{ worker_connections }};
//自定义变量
4、编写变量
vim roses/nginx/vars/main.yaml
worker_connections: 10240
5、编写变量处理程序
vim roles/nginx/handles/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted
6、编写剧本
vim roles/site.yaml //site 关联角色和任务(主文件)
- hosts: host1
roles:
- nginx
7、实施
cd roles
ansible-playbook site.yaml --syntax-check //测试
ansible-playbook site.yaml //实施剧本
验证host1
验证成功。