-2.Ansible中的常用模块

介绍ansible自动化部署主要模块,包括command、shell、copy、fetch、file、archive、unarchive、cron、yum_repository、dnf、service、firewalld、user、group、lineinfile、replace、setup、debug等模块。

一、Command模块
在这里插入图片描述
ansible westos1 -m command -a “touch /mnt/westosfile” 建立文件
ansible westos1 -m command -a “rm -fr /mnt/westosfile” 删除文件

creates,removes
在这里插入图片描述

颜色代表信息:

在这里插入图片描述

二、Shell模块
常用参数:
在这里插入图片描述

在这里插入图片描述

脚本运行
vim hostname.sh
ansible westos1 -m script -a ‘./hostname.sh’ 脚本执行

在这里插入图片描述

三、Copy模块
主机向远程主机复制文件
在这里插入图片描述

ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/test.yml mode=1777’ 复制本地文件到远程主机/mnt下 指定权限为1777

ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/westosfile mode=1777 owner=westos backup=yes’ 如若重复则备份

****四、Fetch模块
将远程主机的文件复制到ansible主机中,但不支持目录
ansible westos1 -m fetch -a “src=/mnt/westos dest=~/.ansible” 会复制整个目录 以ip为目录名

flat=yes 只负责负责文件,但需要给定文件名称

ansible westos1 -m fetch -a ‘src=/mnt/westos dest=~/.ansible/westos flat=yes’ 只复制文件,需要给定文件名

五、File模块
常用参数:
在这里插入图片描述

ansible westos1 -m file -a ‘path=/mnt/westos state=touch’ 创建文件
ansible westos1 -m file -a ‘path=/mnt/westos state=directory’ 创建目录
ansible westos1 -m file -a ‘path=/mnt/westos.link state=link src=/mnt/westos’ 创建软连接
ansible westos1 -m file -a ‘path=/mnt/westos.hard state=hard src=/mnt/westos’ 创建硬链接
ansible westos1 -m file -a ‘path=/mnt/westosdir/test state=touch’
ansible westos1 -m file -a ‘path=/mnt/westos mode=777’ 修改目录权限
ansible westos1 -m file -a ‘path=/mnt/westos mode=777 recurse=yes’ 修改目录权限并且目录中子文件的权限

六、Archive与unarchive模块
archive压缩:
ansible westos1 -m archive -a ‘path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777’

unarchive解压
ansible westos1 -m unarchive -a ‘src=/mnt/etc.tar.gz dest=/mnt copy=no’ copy=no 表示压缩文件在远程主机中,yes表示压缩文件在ansible主机中

七、hostname、Cron模块
hostname 修改主机名称
ansible westos1 -m hostname -a ‘name=westoshyl.westos.org’

cron模块 设定定时任务
常用参数:

在这里插入图片描述
ansible westos1 -m cron -a “job=‘echo hello westos’ name=westoscron weekday=5” 建立定时任务,每周五执行一次
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’ 查看定时任务目录
ansible westos1 -m cron -a “job=‘echo hello westos’ name=westoscron weekday=5 disabled=yes” 删除定时任务
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’ 删除定时任务的实质就是注释掉文件的相应行
删除定时任务的实质就是注释掉文件的相应行。

八、yum_repository模块
配置软件仓库文件 /etc/yum.repos.d/*.repo
常用参数:
在这里插入图片描述

ansible all -m yum_repository -a ‘name=“AppStream” baseurl=http://172.25.9.254/westos/AppStream description=AppStream file=westos enabled=yes gpgcheck=no’ 添加AppStream

ansible all -m yum_repository -a ‘name=“BaseOS” baseurl=http://172.25.9.254/westos/BaseOS description=BaseOS file=westos enabled=yes gpgcheck=no’ 添加BaseOS

九、dnf模块
常用参数:
在这里插入图片描述

ansible all -m dnf -a ‘name=httpd state=present’ present代表下载

ansible all -m dnf -a ‘name=httpd state=absent’ absent删除

十、service、firewalld模块
service常用参数:
在这里插入图片描述

firewalld常用参数:
在这里插入图片描述

ansible westos1 -m ping 查看ansible是否通畅
ansible westos1 -m dnf -a ‘name=httpd state=present’ 下载服务
ansible westos1 -m service -a ‘name=httpd state=started enabled=yes’ 开启服务
ansible westos1 -m firewalld -a ‘service=http permanent=yes state=enabled immediate=yes’ 开启火墙,immediate为立即开启
ansible westos1 -m copy -a ‘dest=/var/www/html/index.html content=“hello westos”’ 编辑默认发布页

十一、user、group 模块
user、group模块可以对远程受控用户进行用户和用户组的增删查改。
user常用参数:
在这里插入图片描述

group常用参数:
在这里插入图片描述

ansible westos1 -m user -a ‘name=westoslee state=present’ 创建用户
ansible westos1 -m user -a ‘name=westoslee group=1000 state=present’ 修改主组
ansible westos1 -m user -a ‘name=westoslee groups=1002 state=present’ 修改附加组
ansible westos1 -m user -a ‘name=westoslee groups=1001 state=present append=yes’ 添加附加组
ansible westos1 -m user -a ‘name=westoslee shell=/sbin/nologin’ 指定shell
ansible westos1 -m user -a ‘name=westoslee uid=1111’ 修改uid
ansible westos1 -m user -a ‘name=westoslee comment=“westoslee user”’ 修改注释
ansible westos1 -m user -a ‘name=westoslee state=absent remove=yes’ 删除用户并且删除家目录

ansible westos1 -m user -a ‘name=westoslee state=present’
openssl passwd -6 生成密钥
ansible westos1 -m user -a ‘name=westoslee password=“密钥”’ 修改密码

ansible westos1 -m user -a ‘name=westoslee generate_ssh_key=yes’ 用户生成ssh密钥在~/.ssh/
ansible westos1 -m group -a ‘name=test state=present’ 创建组,也可指定gid=8888
ansible westos1 -m group -a ‘name=test state=absent’ 删除组

十二、lineinfile 模块
常用参数:
在这里插入图片描述

ansible westos1 -m lineinfile -a ‘path=/mnt/test line=“westos” create=yes’ test插入westos create=yes表示如果没有test即创建
ansible westos1 -m lineinfile -a ‘path=/mnt/test line=“hello westos” regexp="^westos"’
匹配以westos开头的,并且修改添加hello,默认匹配不到也添加
ansible westos1 -m lineinfile -a ‘path=/mnt/test line=“hello test” regexp="^westos" backrefs=yes’ 匹配westos开头的行 添加hello test backrefs=yes匹配不到不添加
ansible westos1 -m lineinfile -a ‘path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=no’ \1代表regexp中的第一个字符串 如果backrefs=no则\1不生效
ansible westos1 -m lineinfile -a ‘path=/mnt/westos line=“end” insertafter=BOF’ 结尾插入
ansible westos1 -m lineinfile -a ‘path=/mnt/westos line=“start” insertafter=BOF’ 开头插入
nsible westos1 -m lineinfile -a ‘path=/mnt/westos line=“startaa” insertafter=BOF backup=yes’ 备份

按列排放: vim test.yml
在这里插入图片描述
ansible-playbook test.yml 执行剧本

十三、replace 模块
replace模块:字符替换

常用参数:
在这里插入图片描述

ansible westos1 -m replace -a “path=/mnt/westos regexp=‘westos’ replace=‘WESTOS’”

十四、setup 模块
setup 模块 系统服务查看,用于手机远程主机的基本信息
ansible westos1 -m setup -a ‘filter=“ansible_all_ipv4_addresses”’ 查看ip

剧本式查看: vim test.yml
在这里插入图片描述

ansible-playbook test.yml
在这里插入图片描述

十五、debug模块
debug 模块 相当于echo
常用参数:
在这里插入图片描述

ansible westos1 -m debug -a ‘msg=“hello westos”’

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值