Ansible-doc 模块学习

网友投稿 338 2022-11-09

Ansible-doc 模块学习

一、什么是ad-hoc1、ad-hoc就是临时命令,执行完即结束,并不会保存2、ad-hoc的模式的使用场景e.g 比如在多台机器上查看某个进程是否启动,或者拷贝指定文件到本地3、ad-hoc模式的命令使用,ansib;e 'oldboy' -m command -a 'df -h'

颜色区别:绿色表示未发生改变黄色发生改变红色发生故障

Ad-hoc 常用模块yum 仓库 yumrepo联网下载 get_url安装 yum配置 copy启动 service、systemd创建用户与组 user、group授权 file 定时任务 crond挂载 mountfirewalld firewalld、selinux selinux

如何使用doc 模块帮助

ansible-doc -l 列出所有的模块ansible-doc copy (模块)ansible-doc -s copy (查看模块参数)

二、ansible 常用模块

1、command:默认模块默认执行bash 不支持重定向和管道符

2、shell 模块

3、script 脚本模块在本地执行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行[root@k8s-master ansible]# cat chech.sh #!/bin/bashhostnameip a |grep gl [root@k8s-master ansible]# ansible -i ./hosts all -m script -a "chech.sh"我们边缘云第一次巡检使用的脚本

4、yum模块:软件安装模块ansible -i ./hosts all -m yum -a "name=state=latest"name: #指定安装的软件包的名称 #包名file:// #指定从本地某个目录安装rpm #指定从哪个网站安装state: #指定使用yum的方法installed,present #安装软件的包removed,absent #移除软件包latest #安装最新的软件包disablerepo:“epel,ol7_lastest”download_only#只下载不安装exclude: #提取默写软件list 列出当前仓库的软件包还可以指定软件包@Development tools #安装组包

(1)、安装软件包

ansible -i ./hosts all -m yum -a "name=state=present"

(2)、删除软件包ansible -i ./hosts all -m yum -a "name=state=absent"

ansible -i ./hosts all -m yum -a "name=state=removed"

(3)、列出当前软件包ansible -i ./hosts all -m yum -a "list=vsftpd "

不通过某个指定仓库安装具体可以使用##查看具体使用实例ansible-doc yum

查看yum的使用具体参数

ansible-doc -s yum

5、文件管理模块: 主要包括:copy文件拷贝、file文件创建、get_url文件下载(1)、copy模块通过ansible-doc copy查看实例一切以为控制端为准copy :

name: Copy file with owner and permissionscopy:src: /srv/myfiles/foo.conf ##推送数据的源文件信息dest: /etc/foo.conf ##推送数据的目标路径owner: foo group: foomode: '0644'backup:对推送传输过去的文件,进行备份content: 直接批量在被管端文件中添加备份validate #验证文件

简单使用拷贝文件

ansible -i ./hosts group_2 -m copy -a "src=./chech.sh dest=/opt/ owner=root group=root mode=0644"

备份文件:在文件与被控端对比发生改变时才会进行备份!!! backup=yes

ansible -i ./hosts group_2 -m copy -a "src=./chech.sh dest=/opt/ owner=root group=root mode=0644 backup=yes"

添加文件信息:向被控端主机写入数据,并且覆盖远端文件内容原有数据信息

ansible -i ./hosts group_2 -m copy -a "content='uptime' dest=/opt/chech.sh"

(2)、file模块同样ansible-doc fileansible-doc filee.g、例子

name: Create a symbolic linkfile:src: /file/to/link/todest: /path/to/symlinkowner: foogroup: foostate: linkpath: 指定远程主机目录或文件信息recurce #递归授权state:directory #在远端创建目录touch:#在远端创建文件link #link或hard表示创建链接文件absent #表示删除文件或者目录mode: 设置文件或目录权限owner 设置文件或目录属主信息group 设置文件或目录属组信息 ansible-doc -s file

 创建目录

ansible -i ./hosts group_2 -m file -a "path=/opt/nginx state=directory owner=root group=root mode=755"

 创建文件

ansible -i ./hosts group_2 -m file -a "dest=/opt/nginx/nginx.conf state=touch owner=root group=root mode=755"

 添加文件内容

ansible -i ./hosts group_2 -m copy -a "content="nginx-version=1.17.2" dest=/opt/nginx/nginx.conf "

验证信息:

备注:递归的添加目录和文件权限添加recurse=yes##递归的设置文件权限

ansible -i ./hosts group_2 -m file -a "dest=/opt/nginx state=directory owner=root group=root mode=775 recurse=yes"

(3)、get_url模块文件下载

ansible-doc get_url

ansible-doc -s get_url

name: Download foo.confget_url:url: /etc/foo.confmode: '0440'下载包镜像 ansible -i ./hosts group_2 -m get_url -a "url= dest=/root mode=0777"

实例二、下载zabbix-agent 软件包

ansible -i ./hosts all -m get_url -a "url=dest=/opt/ owner=root group=root mode=755"

添加md5下载文件时做md5校验,通过下载不同过直接报错checksum=md5:xx支持md5、sha256

ansible -i ./hosts all -m get_url -a

"url=checksum=md5:398304117e711ea6afcec16f0812d0cc dest=/opt/ owner=root group=root mode=755 "

(4)、systemd、service模块:ansible管理服务的启动与停止name #定义启动服务的名称state: #指定服务状态started #启动服务stoped #停止服务restarted #重启服务reloaded #重载服务enabled #开机自启 启动crond服务,并加入开机自启ansible -i ./hosts all -m service -a "name=crond state=started"ansible -i ./hosts all -m service -a "name=crond enabled=yes" 停止crond服务,并删除开机自启ansible -i ./hosts all -m service -a "name=nginx state=stopped"ansible -i ./hosts all -m service -a "name=nginx enabled=no" 重启crond服务

 重载crond服务

(5)、创建组group

用法:name statepresent #创建用户absent #删除用户创建组ansible -i ./hosts all -m group -a "name=test gid=8888"删除组:

ansible -i ./hosts all -m group -a "name=test state=absent"

(6)、User模块用法:name #用户名state ##动作状态absentpresentcomment#描述信息uidgroup#组groups #附加组ssh_key_bitsssh_key_filesample #是否创建家目录

实例: 创建用户指定uid和gid,不创建家目录也不允许登录ansible -i ./hosts all -m user -a "name=tmd uid=12345 group=root groups=root shell=/bin/bash create_home=no" 删除用户

ansible -i ./hosts all -m user -a "name=tmd state=absent"

 给新创建信用生成ssh密钥对ansible -i ./hosts all -m user -a "name=linzhen uid=666 group=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rs"

生成密码第一步生成密码hash值ansible -i ./hosts all -m debug -a "msg={{'passw0rd' |password_hash('sha512','salt')}}"

ansible -i ./hosts all -m user -a 'name=linzhen password=$6$salt$pYOstRsjxkYrKEUNFPWNjZLylHgRSxTQLXeBqm/0QQG3bJdljn6Vcgphfjvh0qPQtqV9VAGG1GYx5AceFFpfX1' ##-a 后面必须需要是单引号,否则密码无法登录

7、定时任务模块croncronname ##添加描述minute ##分钟hour #时间job:动作state:absent 删除默认没有写的时间都是*

创建ansible -i hosts all -m cron -a "minute=0 hour=5,2 job='/bin/bash test.sh &>/dev/null'"

删除ansible -i hosts all -m cron -a "name=None minute=0 hour=5,2 job='/bin/bash test.sh &>/dev/null' state=absent"

一定要加上name,防止重复,否则删除时问题很多ansible -i hosts all -m cron -a "name=xunjian minute=0 hour=5,2 job='/bin/bash test.sh &>/dev/null' state=present"

注释相应定时任务,使定时任务失败

ansible -i hosts all -m cron -a "name=xunjian minute=0 hour=5,2 job='/bin/bash test.sh &>/dev/null' disabled=yes"

8、ansible Mount模块mount例子:

name: Mount DVD read-onlymount:path: /mnt/dvd ##挂载点src: /dev/sr0 ##fstype: iso9660opts: ro,noautostate: present present ###开机挂载,仅将挂载配置写入/etc/fstabmounted ##挂载设备,并写入配置unmounted ##卸载设备absent ##卸载设备,并清理/etc/fstab表

安装nfs master-1做客户端

ansible -i hosts group_1 -m yum -a 'name=nfs-utils state=present'

备注 可以使用file创建目录

ansible -i hosts group_1 -m file -a "path="/share-file" state=directory owner=root group=root"

配置共享目录

ansible -i hosts group_1 -m copy -a 'content="/backup 192.168.100.0/24(rw,sync,no_all_squash)" dest=/etc/exports '

设置服务开机自启

ansible -i hosts group_1 -m systemd -a "name=nfs state=started enabled=yes"

操作挂载

ansible -i hosts group_2 -m mount -a "path=/opt src=192.168.100.10:/backup fstype=nfs opts=defaults state=present"

9、firewalld模块

firewalld:service: yesstate: enabled firewalld:port: 8081/tcppermanent: yesstate: disabled firewalld:port: 161-162/udppermanent: yesstate: enabledservice ##指定开放或关闭的服务名称port ##指定开放或关闭的端口permanent #是否添加永久生效state ##开启或是关闭immediate #临时生效

zone ##指定配置某个区域rich_rule #配置复规则source #指定来配置IP

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:仿真工具重振大型SoC中的功耗分析
下一篇:Java实现简易画图板
相关文章

 发表评论

暂时没有评论,来抢沙发吧~