ansible自动运维

ansible自动运维

1.abstract-简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。

2.模拟域名解析

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
客户机无需配置

3.安装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}

4.ansible基础

定义主机清单

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

5.inventory-主机清单

增加端口
host1 ansible_ssh_user=‘root’ ansible_ssh_pass=‘777777’ ansible_ssh_port=‘2222’ (也要修改将对应主机的sshd程序端口号)
常见变量(下图)
在这里插入图片描述

6.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 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'

shell模块

帮助: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

7.YAML-YAML Ain’t Markup Language-非标记语言

准备工作

清理一下环境: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

编写剧本: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
真机访问ip(记得加上端口号)

handlers触发

如果配置文件发生变化:Listen 9000
yaml程序增加触发器:ansible-playbook apache.yaml (如下图)
在这里插入图片描述

再次执行,配置生效,触发成功
ansible-playbook apache.yaml

8.Role-角色扮演

简介: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
使用真机浏览器进行验证

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值