Ansible中常用的模块_ansible archive remove=yes(1),2024年这些高频面试知识点最后再发一次

七、unarchive

#功能
解压缩

常用参数
| copy | 默认为yes 从ansible主机复制文件到受控主机 |
| | 设定为no  从受控主机中寻找src源文件 |
| remote_src | 功能同copy且相反 |
| | 设定为yes 表示包在受控主机 |
| | 设定为no表示包在ansible主机 |
| src | 包路径,可以使ansible主机也可以使受控主机 |
| dest | 受控主机目录 |
| mode | 加压后文件权限 |

#实例

ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt owner=lee' -k
ansible all -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"
ansible list1 -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt remote_src=yes' ##包在受控主机 no的时候在ansible 主机

八、archive

#作用
压缩

常用参数
| path | 打包目录名称 |
| dest | 生成打包文件名称 |
| format | 打包格式 |
| owner | 指定文件所属人 |
| mode | 指定文件权限 |

#实例

ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=lee mode=700' -k

九、hostname

#作用
管理主机名称

#常用参数
name        ##指定主机名称

#实例

ansbile 172.25.1.10 -m hostname -a 'name=westos.westos.com'

十、cron

#作用
计划任务

常用参数
| minute    | 分钟 |
| hour | 小时 |
| day | 天 |
| month | 月 |
| weekday    | 周 |
| name | 任务名称 |
| job | 任务脚本或命令 |
| disabled | yes 禁用计划任务 |
| | no  启动计划任务 |
| state | absent 删除计划任务 |

#实例

ansible list1 -m cron -a "job='echo hello' name=test hour=5 user=westos"
ansible list1 -m cron -a "job='echo hello' name=test disabled=yes" 
ansible list1 -m cron -a "job='echo hello' name=test state=absent" 

十一、yum_repository

#作用
配置系统软件仓库源文件

常用参数
| name | 指定仓库名称 |
| baseurl    | 指定源路径 |
| description | 指定仓库描述 |
| file | 指定仓库文件名称 |
| enabled    | 仓库是否启用 |
| gpgcheck | 仓库是否检测gpgkey |
| state | 默认值present 建立 absent    为删除 |

#实例

ansible list1 -m yum_repository -a "file=linux name=AppStream description=AppSteam baseurl=http://172.25.1.1/linux/AppStream enabled=yes gpgcheck=no"
ansible list1 -m yum_repository -a "file=linux name=BaseOS description=BaseOS baseurl=http://172.25.1.1/linux/BaseOS enabled=yes gpgcheck=no"
ansible all -m yum_repository -a "name=AppStream  file=westos_test state=absent" -k

十二、dnf

#作用
管理系统中的dnf仓库及管理软件

常用参数
| name | 指定包 |
| state | 指定动作 |
| | present    安装 |
| | latest        更新 |
| | absent        删除 |
| list | 列出指定信息 |
| | httpd    |
| | installed |
| | all |
| | available |
| disable_gpg_check | 禁用gpgkey检测 |
| enablerepo | 指定安装包来源 |
| disablerepo | 禁用安装包来源 |

#实例

ansible all -m dnf -a "name=httpd state=latest"
ansible all -m dnf -a 'name="httpd,mariadb-server" state=present'
ansible all -m dnf -a 'name=httpd state=absent'
ansible all -m dnf -a 'name=httpd state=absent autoremove=no'
ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream'
ansible all -m dnf -a 'name="*" state=latest'
ansible all -m dnf -a 'name=http://172.25.1.1/software/wps-office-xxx.rpm state=present'
ansible all -m dnf -a 'name="@ Virtualization Tools" state=present'

十三、service

#作用

管理系统服务状态

常用参数
| name | 指定服务名称 |
| state | 指定对服务的动作 started stoped reloaded |
| enabled | 设定服务开机是否启动 yes开启启动 no开机不启动 |

#实例

ansible all -m service -a "name=httpd state=started enabled=yes" -k		
ansible all -m service -a "name=httpd state=restarted enabled=yes" -k

十四、user

#作用
模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作

常用参数
| name | 必须参数,用于指定要操作的用户名称。 |
| group    | 指定用户所在的基本组 |
| gourps    | 指定用户所在的附加组。 |
| append | 指定添加附加组默认值为no |
| shell | 指定用户的默认 shell。 |
| uid | 指定用户的 uid 号。 |
| comment | 指定用户的注释信息。 |
| state | 用于指定用户是否存在于远程主机 present    建立 absent        删除 |
| remove | 当删除用户是删除用户家目录,默认值为no |
| password | 此参数用于指定用户的密码。但密码为明文, |
| | 可以用openssl password -6 '密码’生成加密字符 |
| generate_ssh_key | 生成sshkey |

#实例

ansible all -m user -a 'name=lee'
ansible all -m user -a 'name=lee state=absent'
ansible all -m user -a 'name=lee remove=yes state=absent'
ansible all -m user -a 'name=lee  group=888'
ansible all -m user -a 'name=lee  group=888 groups="user1,user2"'
ansible all -m user -a 'name=lee groups="user3"'
ansible all -m user -a 'name=lee groups="user1,user2" append=yes'
openssl passwd -6 'westos'
ansible all -m user -a 'name=lee password="$6$F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/"'
这里注意外部用单引,否则会出现$被注释
ansible all -m user -a 'name=lee generate_ssh_key=yes''

十五、group

#作用
group 模块可以帮助我们管理远程主机上的组。

常用参数
| name | 用于指定要操作的组名称。 |
| state | 用于指定组的状态 present               建立 absent                删除 |
| gid | 用于指定组的gid |

#实例

ansible all -m group -a 'name=westoslee'
ansible all -m group -a 'name=westoslee state=absent'
ansible all -m group -a 'name=westoslee gid=8888'

测试:

十六、lineinfile
path指定要操作的文件。
line指定文本内容。
regexp使用正则表达式匹配对应的行当替换文本时 如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换 当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。
state当想要删除对应的文本时需要将state参数的值设置为absent state的默认值为present。
backrefs当内容无匹配规则时不对文件做任何更改,默认值为no 向后引用regexp变量信息
insertafter借助insertafter参数可以将文本插入到“指定的行”之后 insertafter参数的值可以设置为EOF或者正则表达式
insertbefore借助insertbefore参数可以将文本插入到“指定的行”之前 insertbefore参数的值可以设置为BOF或者正则表达式
backup是否在修改文件之前对文件进行备份。
create当要操作的文件并不存在时,是否创建对应的文件。

#实例

ansible all -m lineinfile -a 'path=/mnt/westos line="hello westos"'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^westos" line="hello westos" '
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^test" line="westos test"'
ansible all -m lineinfile -a 'path=/mnt/westos regexp='^test' line="westos test new" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="h.{4}.*(w.{5})" line="\1" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*w.{5}" line="\1" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\2" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end #####" insertafter=EOF'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end lee #####" insertafter="hello"'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test #####" insertbefore=BOF'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test lee #####" insertbefore="hello"'
.*中间的字符数

 

 

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)
img

最全的Linux教程,Linux从入门到精通

======================

  1. linux从入门到精通(第2版)

  2. Linux系统移植

  3. Linux驱动开发入门与实战

  4. LINUX 系统移植 第2版

  5. Linux开源网络全栈详解 从DPDK到OpenFlow

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-yeQiUrmg-1712645953822)]

  • 23
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值