ansible的部署与使用
1.ansible介绍
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。
无客户端。
2.工作原理
3.ansible环境部署
首先准备5台服务器,一台是ansible主机,四台host机器。
ansible主服务器做域名解析:
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.9.27.243 ansible
10.9.27.238 host1
10.9.27.237 host2
10.9.27.240 host3
10.9.27.242 host4
~
完成后测试一下客户机的连通性,客户机不需要配置。
安装ansible
yum -y install ansible
4.ssh-key
生成秘钥
ssh-keygen
分别将秘钥推送到各host机上
ssh-copy-id IP
5.ansible配置
定义主机清单
vim /etc/ansible/hosts
在最后一行加入我们的host机器
host1
host2
host3
host4
当然这里也可以加入ip地址,但是我们不推荐用ip地址。
接下来测试一下连通性
ansible localhost -m ping
ansible host1 -m ping
ansible host2 -m ping
ansible host3 -m ping
ansible host4 -m ping
host4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
-m 是调用模块
第一次连接host机时会有一个ssh认证,我们选yes就可以,因为上面做了免密,所以连接时不需要输入host机密码。
6.inventory-主机清单
增加主机组
vim /etc/ansible/hosts
[webserver]
host1
host2
host3
host4
完成后测试
ansible webserver -m ping -o
host3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host4 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
-o 是简洁化输出
若我们没有对host机进行免密登录,我们可以增加用户名 密码,以达到连接host机器的目的。
例如:
vim /etc/ansible/hosts
[webserver]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='123'
组:变量
定义组内所有机器的变量
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'
常用变量
子分组
vim /etc/ansible/hosts
[apache]
host1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'
自定义主机列表:
在当前目录下新建一个hostlist
vim hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'
7.Ad-Hoc 点对模式
简介:临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
复制模块 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'
用户模块 ansible-doc user
创建用户
ansible webserver -m user -a 'name=zyz state=present'
删除用户
ansible webserver -m user -a 'name=zyz state=absent'
软件包管理模块 ansible-doc yum
ansible host1 -m yum -a 'name="*" state=latest' //升级所有包
ansible host1 -m yum -a 'name="httpd" state=latest' //安装apache
服务模块 ansible-doc service
ansible host1 -m service -a 'name=httpd state=started' //启动apache
ansible host1 -m service -a 'name=httpd state=started enabled=yes' //开机启动apache
文件模块 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' //创建目录
收集模块 ansible-doc setup
ansible host3 -m setup 查询所有信息
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'
shell模块 ansible-doc shell
ansible webserver -m shell -a 'hostname' -o //获取主机名
ansible webserver -m shell -a 'hostname' -o -f 2 //-f 2 指定线程数
ansible host2 -m shell -a 'yum -y install httpd' -o //部署apache
ansible host3 -m shell -a 'uptime' -o //查询系统负载
8.YAML 非标记语言
编写剧本
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
测试编写剧本是否正常
ansible-playbook apache.yaml --syntax-check //检验语法
ansible-playbook apache.yaml --list-tasks //列出任务
ansible-playbook apache.yaml --list-hosts //列出主机
ansible-playbook apache.yaml //执行
http://192.168.2.142:8080/ //注意端口
handlers
如果配置文件发生改变,依旧可以拷贝过去,并且覆盖。
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
- name: ensure apache is running
service: name=httpd state=started enabled=yes
handlers:
name: restart apache service
service: name=httpd state=restarted
再次执行触发handlers
9.Role-角色扮演
roles是在ansible中,playbooks的目录组织结构。
将代码或文件进行模块化,成为roles的文件目录组织结构,
易读,代码可重用,层次清晰。