ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
ansible服务器配置域名解析
192.168.216.154 host4
192.168.216.155 host3
192.168.216.156 host2
192.168.216.158 host1
192.168.216.161 ansible
客户机无需配置
yum install -y epel-release(使用了阿里云的yum源)
yum install -y ansible (完成后检查是否部署完成)
yum install -y ansible (完成后检查是否部署完成)
rpm -qc ansible 查看配置文件ansible –help 查看ansible帮助
ansible-doc -l 看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
ansible-doc -s yum
{install (`present' or `installed', `latest'), or remove (`absent' or `removed');yum list;Package name;enablerepo}
vim /etc/ansible/hosts
如果使用免密ssh-key,则无需配置密码
ssh-keygen
ssh-copy-id ip地址(推送公钥)
测试连通性
ansible localhost -m ping
简洁输出
ansible host1 -m ping -o
连接测试
ansible host2 -m ping -u root -k -o
增加端口
host1 ansible_ssh_user=‘root’ ansible_ssh_pass=‘777777’ ansible_ssh_port=‘2222’ (也要修改将对应主机的sshd程序端口号)
常见变量(下图)
临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
帮助:ansible-doc copy
示例: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=qianfeng state=present'
删除用户:ansible webserver -m user -a 'name=qianfeng state=absent'
生成加密密码:echo '777777' | openssl passwd -1 -stdin
修改密码:ansible webserver -m user -a 'name=leyou ``password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
修改shell:ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes'
帮助:ansible-doc yum
升级所有包:ansible host1 -m yum -a 'name="*" state=latest'
安装apache:ansible host2 -m yum -a 'name="httpd" state=latest'
帮助:ansible-doc service
启动:ansible host2 -m service -a 'name=httpd state=started'
开机启动:ansible host2 -m service -a 'name=httpd state=started enabled=yes'
停止:ansible host2 -m service -a 'name=httpd state=stopped'
重启:ansible host2 -m service -a 'name=httpd state=restarted'
开机禁止启动:ansible host2 -m service -a 'name=httpd state=started enabled=no'
帮助: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
查询ipv4:ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'
帮助:ansible-doc shell
获取主机名:ansible webserver -m shell -a 'hostname' -o
-f 2 指定线程数:ansible webserver -m shell -a 'hostname' -o -f 2
部署apache:ansible host2 -m shell -a 'yum -y install httpd' -o
查询系统负载:ansible host3 -m shell -a 'uptime' -o
清理一下环境:ansible all -m yum -a 'name=httpd state=removed' -o
准备配置文件:yum install -y httpd
mkdir apache
cd apache
cp -rf /etc/httpd/conf/httpd.conf .
grep '^Listen' httpd.conf
修改配置,用作推送:Listen 8080
- 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
真机访问ip(记得加上端口号)
如果配置文件发生变化:Listen 9000
yaml程序增加触发器:ansible-playbook apache.yaml
(如下图)
再次执行,配置生效,触发成功
ansible-playbook apache.yaml
简介:roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
目标:通过role远程部署nginx并配置
目录结构
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
yum -y install tree
tree /root/roles/
(如下图)
nginx 角色名;files 普通文件;handlers 触发器程序;tasks 主任务;templates 金甲模板(有变量的文件);vars 自定义变量
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
cd roles
ansible-playbook site.yaml --syntax-check
ansible-playbook site.yaml
使用真机浏览器进行验证