首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >手拉手入门Ansible常用模块

手拉手入门Ansible常用模块

原创
作者头像
QGS
发布2025-01-23 13:41:24
发布2025-01-23 13:41:24
1901
举报
文章被收录于专栏:QGS探索QGS探索

自动化运维Devops-Ansible

Ansible是新出现的自动化运维工具,基于Python 开发,集合了众多运维工具(puppet 、cfengine、chef、func、fabric)的优点,实现了批量系统配置 、批量程序部署、批量运行命令 等功能。Ansible架构相对比较简单,仅需通过SSH 连接客户机 执行任务即可。

测试环境

操作系统版本

IP地址

主机名

Centos7.6

192.168.68.133

manage

Centos7.6

192.168.68.134

node01

Centos7.6

192.168.68.135

node02

Ansible常用模块

Ansible模块共计3387个

Ansible执行命令的结果颜色含义

绿色:命令执行成功,但是状态没有变化(软件已经安装过)

黄色:命令执行成功,状态发生改变(首次安装)

紫色:warning警告信息,ansible提升有更合适的用法

红色:命令错误,执行失败

蓝色:详细的执行过程

Command模块

Command是ansible默认的末流,也就是默认指定 -m -command

Command模块执行命令,只支持简单命令执行,不得使用变量(%HOME),不得出现特殊符号<、>、|、;、&

代码语言:txt
复制
ansible node01 -m command -a 'free -m' => ansible node01 -a 'free -m'

代码语言:txt
复制
ansible node01 -a 'touch /opt/test001.log'

获取负载信息

代码语言:txt
复制
ansible node01 -a 'uptime'

备份一份日志

代码语言:txt
复制
ansible node01 -a "cp /opt/test001.log /var/log chdir=/"
ansible node01 -a "ls -l /var/log chdir=/"

如果文件夹不存在,则不执行操作

代码语言:txt
复制
ansible node01 -a "cp /opt/tt/test001.log /var/log chdir=/ removes=/opt/"

Shell模块

Shell模块可以在linux上执行任何复杂的命令

使用重定向符号,创建文件

代码语言:txt
复制
ansible node01 -m shell -a "date > /opt/datetest01.log"
ansible node01 -m shell -a "cat /opt/datetest01.log chdir=/"

过滤mysql进程信息

代码语言:txt
复制
ansible node01 -m shell -a "ps -ef|grep mysql"

代码语言:txt
复制
ansible node01 -m shell -a "date '+%F %T' > /opt/datetest01.log"

一条命令执行多个命令:1、创建文件夹 2、生成sh脚本文件 3、赋予脚本可执行权限 4、执行脚本

代码语言:txt
复制
ansible node01 -m shell -a "mkdir /opt/ansibletest/;echo 'ps -ef'  > /opt/ansibletest/pstest.sh;chmod +x /opt/ansibletest/pstest.sh;bash  /opt/ansibletest/pstest.sh"

忽略warning信息

代码语言:txt
复制
ansible node01 -m shell -a "mkdir /opt/ansibletest/;echo 'ps -ef'  > /opt/ansibletest/pstest.sh;chmod +x /opt/ansibletest/pstest.sh;bash  /opt/ansibletest/pstest.sh; warn=false"

Copy模块

Copy模式是远程推送数据模块,只能把数据推送给节点主机,无法拉取数据到本地。

Manage->Node ;Node!=-> Manage

简单发送文件

src源文件绝对路径 ,dest目的路径

代码语言:txt
复制
ansible node01 -m copy -a "src=/opt/cpu_per.sh dest=/opt/"

发送文件并修改文件权限

代码语言:txt
复制
ansible node01 -m copy -a "src=/opt/linux_Inspection.sh dest=/opt/linux_Inspection.sh mode=700"

创建一个备份文件(若目标文件内存不同,另外生成加上时间戳)

代码语言:txt
复制
ansible node01 -m copy -a "src=/opt/cpu_per.sh dest=/opt/ backup=yes"

将指定数据写入远程文件中

覆盖原有内容

代码语言:txt
复制
ansible node01 -m copy -a "content='hello word' dest=/opt/test001.log"
ansible node01 -m shell -a "cat /opt/test001.log"

开启备份

代码语言:txt
复制
ansible node01 -m copy -a "content='hello word' dest=/opt/test001.log backup=yes"

复制文件夹

拷贝/opt/下所有内容至目标虚拟机

代码语言:txt
复制
ansible node01 -m copy -a "src=/opt/ dest=/opt/copyDemo"

拷贝/opt整个目录到目标虚拟机

代码语言:txt
复制
ansible node01 -m copy -a "src=/opt dest=/opt/copyDemo"

File模块

File模块用于创建文档、目录数据、及文件、目录权限修改

文档帮助ansible-doc -s file

创建文件

代码语言:txt
复制
ansible node01 -m file -a "path=/opt/ansibleTest.log state=touch"

修改文件属性并设定权限

代码语言:txt
复制
ansible node01 -m file -a "path=/opt/linux-ansible.log state=touch owner=adm group=adm mode=700"

创建文件夹

代码语言:txt
复制
ansible node01 -m file -a "path=/opt/ansibleDemo state=directory"

创建软连接文件

代码语言:txt
复制
ansible node01 -m file -a "src=/opt/ansibleTest.log dest=/opt/ansibleDemo/ansibleTest.log state=link"

强制创建软连接文件

代码语言:txt
复制
ansible node01 -m file -a "src=/opt/ansible1234 dest=/opt/ansibleDemo/ansibleTest123.log state=link force=yes"

Ping模块

代码语言:txt
复制
ansible node01 -m ping

代码语言:txt
复制
ansible all -m ping

Script模块

将本地脚本在远程节点并执行

代码语言:txt
复制
ansible node01 -m script -a "/opt/cpu_per.sh"

查看命令执行详细过程

代码语言:txt
复制
ansible node01 -vvvv -m script -a "/opt/cpu_per.sh"

Cron定时任务模块

分时日月周

*****

创建定时任务

每10分钟做一次时钟同步

代码语言:txt
复制
ansible node01 -m cron -a "name='synchronization' minute=*/10 job='ntpdate -u ntp.aliyun.com'"

每分钟追加一句话到文本

代码语言:txt
复制
ansible node01 -m cron -a "name='testhello1' job='echo "hello" >>/opt/hello.log'"

删除定时任务

Name=指定定时任务名称

代码语言:txt
复制
ansible node01 -m cron -a "name='synchronization' state=absent "

根据定时任务名称修改任务

代码语言:txt
复制
ansible node01 -m cron -a "name='testhello1' minute=30 hour=9 job='echo "hello" >>/opt/hello.log'"

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Ansible常用模块
    • Command模块
    • Shell模块
    • Copy模块
    • File模块
    • Ping模块
    • Script模块
    • Cron定时任务模块
      • 创建定时任务
      • 删除定时任务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档