您现在的位置是:主页 > news > 2h1g做视频网站/郑州制作网站公司
2h1g做视频网站/郑州制作网站公司
admin2025/5/6 7:09:38【news】
简介2h1g做视频网站,郑州制作网站公司,seo和sem的联系,网站建设栏目规划目录1、ansible概述2、ansible环境安装部署2.1、修改主机名,安装epel源2.1.1、切换到被管理点查看效果2.2、ansible命令行常用模块2.2.1、主机连通性测试2.2.2、command模块2.2.2.1、查看时间和根目录下的信息2.2.2.2、列出模块信息和动作2.2.2.3、列出已安装的模块…
目录
- 1、ansible概述
- 2、ansible环境安装部署
- 2.1、修改主机名,安装epel源
- 2.1.1、切换到被管理点查看效果
- 2.2、ansible命令行常用模块
- 2.2.1、主机连通性测试
- 2.2.2、command模块
- 2.2.2.1、查看时间和根目录下的信息
- 2.2.2.2、列出模块信息和动作
- 2.2.2.3、列出已安装的模块
- 2.2.2.4、检查可添加的参数信息
- 2.2.3、cron模块
- 2.2.3.1、查看cron模块信息
- 2.2.3.2、执行计划任务
- 2.2.3.3、删除计划任务(假如没有名字,name=None即可)
- 2.2.4、user模块
- 2.2.4.1、检查包含的参数信息
- 2.2.4.2、创建用户(2个节点里都没有这个用户,同时创建)
- 2.2.4.3、删除一个用户(另外一个没有影响)
- 2.2.5、group模块
- 2.2.5.1、检查包含的参数信息
- 2.2.5.2、创建组
- 2.2.5.2.1、查看网段
- 2.2.5.2.2、查看协议
- 2.2.5.3、把用户加入到组
- 2.2.6、copy模块
- 2.2.6.1、检查包含的参数信息
- 2.2.6.2、创建复制
- 2.2.6.3、创建生成文件
- 2.2.7、file模块
- 2.2.7.1、检查包含的参数信息
- 2.2.7.2、修改文件的属组、属主权限
- 2.2.7.3、创建一个链接文件
- 2.2.7.3.1、软链接实际环境中应用
- 2.2.7.4、删除一个文件
- 2.2.7.5、创建一个文件
- 2.2.7.6、创建一个目录
- 2.2.8、yum模块
- 2.2.8.1、检查包含的参数信息
- 2.2.8.2、yum安装apache
- 2.2.8.3、yum卸载apache
- 2.2.9、service模块
- 2.2.9.1、检查包含的参数信息
- 2.2.9.2、对服务进行管理
- 2.2.10、shell模块
- 2.2.10.1、检查包含的参数信息
- 2.2.10.2、比较command和shell的区别
- 2.2.11、script模块
- 2.2.11.1、检查包含的参数信息
- 2.2.11.2、管理端创建脚本
- 2.2.12、setup模块
- 2.2.12.1、检查包含的参数信息
- 2.2.12.2、获取MySQL组主机的facts信息
- 3、总结
1、ansible概述
■ Ansible可以同时管理Redhat系的Linux,Debian 系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansbile
■ ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具( puppet、cfengine、chef、func、 fabric) 的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible 是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible. 只是提供一种框架
● 连接插件connection plugins:负责和被监控端实现通信
● host inventory:指定操作的主机,是一个配置文件里面定义监控的主机
● 各种模块核心模块、command 模块、自定义模块
● 借助于插件完成记录日志邮件等功能
● playbook: 剧本执行多个任务时,非必需可以让节点一次性运行多个任务
■ ansible的架构:连接其他主机默认使用ssh协议 (端口号22)
2、ansible环境安装部署
管理端ansible:192.168.140.10
被管理端mysql: 192.168.140.20
被管理端webserver: 192.168.140.30
安装步骤
2.1、修改主机名,安装epel源
[root@server1 ~]# hostnamectl set-hostname ansible
[root@server1 ~]# su
[root@server2 ~]# hostnamectl set-hostname mysql
[root@server2 ~]# su
[root@server3 ~]# hostnamectl set-hostname webserver
[root@server3 ~]# su
[root@ansible ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo #阿里镜像站下载镜像
[root@ansible ~]# yum makecache
[root@ansible ~]# yum install -y epel-release #安装epel源
[root@ansible ~]# yum install -y ansible
[root@ansible ~]# ansible --version #查看ansible版本
[root@ansible ~]# yum -y install tree #安装树状结构展示安装包
[root@ansible ~]# tree /etc/ansible #树状结构展示文件夹
注:
ansible.cfg: ansible的配置文件
hosts:ansible的主仓库,用于存储需要管理的远程主机的相关信息
roles:角色
[root@ansible ~]# cd /etc/ansible/
[root@ansible ~]# vim /etc/ansible/hosts #配置主机清单
[root@ansible ansible]# ssh-keygen -t rsa #生成密钥
[root@ansible ansible]# ssh-copy-id root@192.168.140.20 #配置密钥对验证,免密登录
[root@ansible ansible]# ssh-copy-id root@192.168.140.30 #配置密钥对验证,免密登录
[root@ansible ansible]# ssh-agent bash #免交互代理
[root@ansible ansible]# ssh-add
2.1.1、切换到被管理点查看效果
[root@mysql ~]# ls -a
[root@mysql ~]# cd .ssh/
[root@mysql .ssh]# ls -lh
[root@mysql .ssh]# cat authorized_keys
2.2、ansible命令行常用模块
2.2.1、主机连通性测试
[root@ansible ~]# ansible all -m ping
2.2.2、command模块
2.2.2.1、查看时间和根目录下的信息
[root@ansible ansible]# ansible mysql -a 'date' #查看mysql节点的时间
[root@ansible ansible]# ansible all -a 'date' #查看所有节点的时间
[root@ansible ~]# ansible 192.168.140.20 -a 'date' #指定IP执行date
[root@ansible ansible]# ansible all -a 'ls \' #查看所有节点根目录下的目录、文件 (不加-m模块,默认的就是command模块)
2.2.2.2、列出模块信息和动作
[root@ansible ~]# ansible-doc -s yum #列出yum模块描述信息和操作动作,q退出
2.2.2.3、列出已安装的模块
[root@ansible ~]# ansible-doc -l #列出所有已安装的模块
2.2.2.4、检查可添加的参数信息
[root@ansible ~]# ansible-doc -s command #检查可添加的参数信息
2.2.3、cron模块
注:两种状态(state) : present表示添加(可以省略),absent表示移除
2.2.3.1、查看cron模块信息
[root@ansible ~]# ansible-doc -s cron #查看cron模块信息
2.2.3.2、执行计划任务
[root@ansible ~]# ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo hello >> /opt/info.txt" name="test cron job"'
注: webserver #主机标识 m #模块cron #模块名称-a #后面跟参数minute="*/1" #每隔一分钟,执行一次
job="/usr/bin/echo hello #输出为hello的字段>> /opt/info.txt #追加到opt/info.txt文件中
name="test cron job"' #执行任务的名称
如果把主机标识换成all就是所有节点执行这个计划任务
[root@ansible ~]# ansible webserver -a 'crontab -l' #切换任务的位置
[root@webserver ~]# cd /opt
[root@webserver opt]# ls -lh
[root@webserver opt]# cat info.txt #查看执行任务
2.2.3.3、删除计划任务(假如没有名字,name=None即可)
[root@ansible ~]# ansible webserver -m cron -a 'name="test cron job" state=absent'
[root@ansible ~]# ansible webserver -a 'crontab -l'
2.2.4、user模块
注:user模块是请求的是useradd, userdel, usermod三个指令
2.2.4.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s user
2.2.4.2、创建用户(2个节点里都没有这个用户,同时创建)
[root@mysql ~]# id zhangsan
[root@webserver ~]# id zhangsan
[root@ansible ~]# ansible all -m user -a 'name="zhangsan"'
2.2.4.3、删除一个用户(另外一个没有影响)
[root@ansible ~]# ansible mysql -m user -a 'name="zhangsan" state=absent'
2.2.5、group模块
注:group模块请求的是groupadd, groupdel, groupmod 三个指令
2.2.5.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s group
2.2.5.2、创建组
[root@ansible ~]# ansible webserver -m group -a 'name=xiaoshou gid=1020 system=yes'
[root@webserver ~]# getent group | grep xiaoshou #查询组的信息
扩展:getnet功能
[root@webserver ~]# getent --help
2.2.5.2.1、查看网段
[root@webserver ~]# getent networks
2.2.5.2.2、查看协议
[root@webserver ~]# getent protocols
2.2.5.3、把用户加入到组
[root@ansible ~]# ansible webserver -m user -a 'name=zhangsan uid=1020 system=yes group=xiaoshou'
[root@webserver ~]# id zhangsan
2.2.6、copy模块
2.2.6.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s copy
2.2.6.2、创建复制
[root@ansible ~]# ansible webserver -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=zhangsan mode=600'
[root@webserver ~]# cd /opt
[root@webserver opt]# ls
2.2.6.3、创建生成文件
[root@ansible ~]# ansible webserver -m copy -a 'content="this is my web" dest=/opt/web.txt'
[root@webserver opt]# cat web.txt
this is my web[root@webserver opt]# ls
2.2.7、file模块
2.2.7.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s file
2.2.7.2、修改文件的属组、属主权限
[root@ansible ~]# ansible webserver -m file -a 'owner=root group=xiaoshou mode=666 path=/opt/fstab.bak'
[root@webserver opt]# ll
2.2.7.3、创建一个链接文件
[root@ansible ~]# ansible webserver -m file -a 'path=/fstab.link src=/opt/fstab.bak state=link'
[root@webserver opt]# ll / #查看
2.2.7.3.1、软链接实际环境中应用
■ 根分区容量不足:
● 首先明确根分区不能建立LVM,不能进行扩展
■ 解决:
● 清理日志文件,将日志文件进行备份迁移
● 建立软链接,将大文件拷贝到其他分区(分区使用LVM做的),在原本的根分区位置建立软链接
2.2.7.4、删除一个文件
[root@ansible ~]# ansible webserver -m file -a 'path=/opt/fstab.bak state=absent'
[root@webserver opt]# ls -lh
2.2.7.5、创建一个文件
[root@ansible ~]# ansible webserver -m file -a 'path=/opt/abc state=touch'
[root@webserver opt]# ls -lh
2.2.7.6、创建一个目录
[root@ansible ~]# ansible webserver -m file -a 'path=/opt/share state=directory mode=755'
[root@webserver opt]# ls
2.2.8、yum模块
2.2.8.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s yum
2.2.8.2、yum安装apache
[root@ansible ~]# ansible webserver -m yum -a 'name=httpd'
[root@webserver ~]# rpm -q httpd
2.2.8.3、yum卸载apache
[root@ansible ~]# ansible webserver -m yum -a 'name=httpd state=absent'
[root@ansible ~]# rpm -q httpd #检查apache软件包是否安装
2.2.9、service模块
2.2.9.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s service
2.2.9.2、对服务进行管理
[root@ansible ~]# ansible webserver -m yum -a 'name=httpd'
[root@ansible ~]# ansible webserver -a 'systemctl status httpd' #查看web服务器httpd的运行状态
[root@ansible ~]# ansible webserver -m service -a 'name=httpd state=started enabled=true' #开启服务
[root@ansible ~]# ansible webserver -m service -a 'name=firewalld state=stopped' #关闭防火墙
[root@ansible ~]# ansible webserver -a 'systemctl status firewalld' #查看防火墙的状态
测试效果
[root@ansible ~]# ansible webserver -m copy -a 'content="this is my web" dest=/var/www/html/index.html' #创建主页
2.2.10、shell模块
chdir:指定工作目录,在执行对应的命令之前,会先进入到chdir参数指定的目录中
creates:指定一个文件,当指定的文件存在时,就不执行对应命令
removes:使用此参数指定一个文件,当指定的文件不存在时,就不执行对应命令
2.2.10.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s shell
2.2.10.2、比较command和shell的区别
[root@ansible ~]# ansible mysql -m command -a 'echo this is a > /opt/a.txt'
[root@ansible ~]# ansible mysql -m command -a 'echo this is a >> /opt/a.txt'
[root@ansible ~]# ansible mysql -m shell -a 'echo this is a > /opt/a.txt'
[root@ansible ~]# ansible mysql -m shell -a 'echo this is a >> /opt/a.txt'
[root@mysql opt]# cat a.txt
[root@ansible ~]# ansible mysql -m shell -a 'chdir=/usr/local echo this is test > test.txt'
[root@mysql opt]# cd /usr/local
[root@mysql local]# ls -lh
总结:shell模块可以识别重定向和追加符号,可以执行多条语句,而command模块不可以
2.2.11、script模块
2.2.11.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s script
2.2.11.2、管理端创建脚本
[root@ansible ~]# vim test.sh
[root@ansible ~]# chmod +x test.sh
[root@ansible ~]# vim test.sh
[root@ansible ~]# ansible all -m script -a 'test.sh'
[root@mysql opt]# cat hello.txt
[root@webserver opt]# cat hello.txt
2.2.12、setup模块
2.2.12.1、检查包含的参数信息
[root@ansible ~]# ansible-doc -s setup
2.2.12.2、获取MySQL组主机的facts信息
[root@ansible ~]# ansible mysql -m setup
3、总结
ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的,部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作,是每位运维工程师必须掌握的技能之一