当前位置: 首页 > news >正文

Ansible自动化运维工具(二)

目录

(6)copy模块

(7)file模块

​编辑​编辑(8)hostname模块

(9)ping模块 

(10)yum 模块 

(11)service/system模块

​编辑

​编辑

(12)script模块

​编辑

(13)setup模块

(1)inventory 中的变量含义


(6)copy模块

用于复制指定主机文件到远程主机的

ansible-doc -s copy

常用的参数:

参数注释
dest指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内
src指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
mode指出复制时,目标文件的权限
owner指出复制时,月标文件的属主
group指出复制时,目标文件的属组
content指出复制到目标主机上的内容,不能与src一起使用
#将etc/fstab下的复制到opt/fstab.bak给予root属主、权限640
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'

#详细查看opt下的文件
ansible dbservers -a 'ls -l /opt'

 

#查看新创建的文件内容
ansible dbservers -a 'cat /opt/fstab.bak'

#将helloworld写入/opt/hello.txt文件中
ansible dbservers -m copy -a 'content="helloworld" de
st=/opt/hello.txt'

#查看写入的文件
ansible dbservers -a 'cat /opt/hello.txt'

(7)file模块

设置文件属性

#查看file模块下的功能,按q退出
ansible-doc -s file

举例:

#修改文件的属主属组权限等
ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'
#查看修改后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'

#设置/opt/fstab.link为/opt/fstab.bak的链接文件
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
#查看修改后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'

#创建一个文件
ansible dbservers -m file -a "path=/opt/abc.txt state=touch"
#查看修改后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'

#删除一个文件
ansible dbservers -m file -a "path=/opt/abc.txt state=absent"
#查看删除后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'

(8)hostname模块

用于管理远程主机上的主机名

ansible dbservers -m hostname -a "name=mysql01"

(9)ping模块 

检测远程主机的连通性

ansible all -m pingansible webservers -m pingansible 192.168.158.25 -m ping

 

(10)yum 模块 

在远程主机上安装与卸载软件包

ansible-doc -s yum
参数注释
name指定要安装卸载的软件
state=present/absent默认persent添加absent卸载

举例:

#安装hyyps服务
ansible dbservers -m yum -a "name=httpd"

#查看安装服务的状态
nsible dbservers -m command -a 'service httpd status'

#卸载httpd服务
ansible dbservers -m yum -a "name=httpd state=absent"

(11)service/system模块

管理远程被控制主机上的管理服务的运行状态

常用参数:

参数注释
name=“名称”管理的服务名称
enable=true/false设置服务开机自启或关闭
state=start/stop/restart设置服务的状态为开始/关闭/重启
enable=yes/no设置是否开机自启
runlevel若设置开机自启则要设置在那些系统等级使用

举例:

#下载httpd服务
ansible dbservers -m yum -a "name=httpd"
#设置开机自启,服务的状态为开启
ansible dbservers -m service -a 'enabled=yes  name=httpd state=started'

(12)script模块

实现远程批量运行本地的shell脚本

#查看模块下的功能
ansible-doc -s script
ansible服务器:
vim  /test.sh
#编写/下的test.sh脚本内容如下
#!/bin/bash
echo  "this is test"
chmod +x /test.sh
ansible webservers -m script -a "/test.sh"

(13)setup模块

setup 模块可以获取这些信息 facts 组件收集d 被管理节点信息

参数:filter 过滤可配合正则表达式。

ansible webservers -m setup -a 'filter=*ipv4'

3.hostsinverntory主机清单

hosts配置文件位置:/etc/ansible/hosts;

Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内

(1)inventory 中的变量含义

变量含义
ansible_hostansible连接节点时的IP地址
ansible_port连接对方的端口号,ssh连接时默认为22
ansible_user连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户
ansible_password连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效
ansible_ssh_ private_key_file指定密钥认证ssh连接时的私钥文件
ansible_ssh_common_args提供给ssh、sftp、 scp命令的额外参数
ansible become允许进行权限提升
ansible become_ method指定提升权限的方式,例如可使用sudo/ su/runas等方式
ansible become_user提升为哪个用户的权限,默认提升为root
ansible_become_password提升为指定用户权限时的密码
#ansible主机配置
#如果是名称类似的主机,可以使用列表的方式标识各个主机。
vim /etc/ansible/hosts
[webservers]
192.168.198.12:22		#冒号后定义远程连接端口,默认是 ssh 的 22 端口
192.168.146.1[2:3]
[dbservers]
db-[a:f].example.org	#支持匹配 a~f
(1)主机变量
[webservers]
192.168.198.13 ansible_port=22 ansible_user=root ansible_password=000000
ansible webservers -a 'ls -lh /home'

(2)组变量
[webservers]
192.168.198.13
#表示为 webservers 组内所有主机定义变量
[webservers:vars]			
ansible_user=root
ansible_password=000000[all:vars]					#表示为所有组内的所有主机定义变量
ansible_port=22

ansible webservers -a 'ls -lh /home'

(3)组嵌套
[nginx]
192.168.198.12
192.168.198.13
192.168.198.14[apache]
192.168.198.3[0:3]#表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
[webs:children]		
nginx
apache

http://www.lryc.cn/news/147055.html

相关文章:

  • uniapp中使用原生canvas标签绘制视频帧来模拟拍照,拍照后将图绘制在另外一个canvas上编辑画图,这样反复操作
  • 机器视觉工程师们,学习是工作以外的事情
  • 数据驱动的生活:探索未来七天生活指数API的应用
  • 【数据分享】2006-2021年我国城市级别的集中供热相关指标(免费获取\20多项指标)
  • 2022年研究生数学建模竞赛优秀论文汇总
  • 阿里云申请免费SSL证书的两种验证方式及配置服务器Tomcat升级HTTPS协议
  • SQL Server 和 MySql 语法和关键字的区别
  • 2023_Spark_实验三:基于IDEA开发Scala例子
  • 2023年高教社杯数学建模思路 - 案例:异常检测
  • C# Dapper 操作Oracle数据库
  • element侧边栏子路由点击不高亮问题
  • C# 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
  • Linux 进程的睡眠和唤醒详解
  • AI 绘画Stable Diffusion 研究(十五)SD Embedding详解
  • 在Jupyter Notebook中添加Anaconda环境(内核)
  • 适配器模式简介
  • MyBatis —— 多种查询及映射关系
  • 腾讯云服务器镜像TencentOS Server操作系统详细介绍
  • Docker 中下载各版本的 CentOS、CentOS Steam 方式
  • 多线程使用HashMap,HashMap和HashTable和ConcurrentHashMap区别(面试题常考),硬盘IO,顺便回顾volatile
  • 专线连接交换机设置 – 如何实现高效率的网络连接?
  • C#,数值计算——Midexp的计算方法与源程序
  • 微信小程序使用本地存储方法wx.setStorageSync()和wx.getStorageSync()
  • 题解:ABC317C - Remembering the Days
  • 【CSS】简记CSS效果:通过transition(动画过渡属性)实现侧边栏目滑入滑出
  • LeetCode——最大子数组和(中等)
  • Zookeeper集成SpringBoot
  • ModaHub魔搭社区:星环科技致力于打造更优越的向量数据库
  • Dubbo默认使用什么序列化框架?还有哪些?
  • 攻防世界-What-is-this