ansible模块

模块分类模块
命令和脚本模块 command模块 默认的模块 执行简单命令,不支持特殊符号
shell模块执行命令,支持特殊符号
script模块分发脚本并执行
文件file 创建目录,文件,软连接, copy 远程分发文件,修改权限,所有者,备份
服务systemd服务管理 service 服务管理(了解)
软件包yum源 yum_repository yum命令 get_url下载软件
系统管理mount模块 挂载 cron模块 定时任务
用户管理group模块 管理用户组user模块 管理用户
其他可以研究压缩解压(unarchive) ,rsync模块(synchronize),数据库模块(mysql_db,mysql_user) ……
其他ansible管理docker k8s zabbix grafana……
用于调试模块ping 模块检查 ansible与其他节点连通性. debug 模块 用于检查/显示 变量

ansible -m 指定模块 -a 指定模块中的选项

命令与脚本类模块

command模块

是ansible默认的模块,适用于执行简单的命令,不支持特殊符号
用法案例

  • 批量获取所有主机的主机名
    ansible all -m command -a ‘ip a s eth0’

shell模块

与command模块类似,但是shell支持特殊符号
用法案例

  • 批量获取ip地址
    ansible all -m shell -a "ip a s eth0 |awk -F’[ /]+’ ‘NR==3{print $3}’”
    ⚠️shell模块不推荐执行较为复杂的指令,如果需要执行放在脚本中执行

script模块

分发脚本, 运行脚本
用法案例
ansible all -m script -a ‘脚本地址’

文件相关模块

file模块

管理文件,管理目录,软连接

file模块模块说明
path路径(目录,文件) 必须要写
src源文件一般用于link(创建软连接模式) 用于指定源文件
state状态(模式) state=directory 创建目录
state=file (默认)更新文件,如果文件不存在也不创建
state=link创建软连接
state=touch创建文件
state state=absent删除 (注意如果是目录递归删除目录 ) mode mode=755创建并修改权限
onweronwer=root
groupgroup=root

案例

创建文件
ansible all -m file -a 'path=/opt/test.txt state=touch
创建目录
ansible all -m file -a ‘path=/app/a/b/c/ state=directory’
创建软连接 /etc/hosts创建软连接到/opt/下
ln -s /etc/hosts /opt/hosts
ansible all -m file -a ’ src=/etc/hosts path=/opt/hosts state=link ’
创建/demo 目录 所有者是test
ansible all -m file -a ‘path=/demo/ owner=test group=test mode=700 state=directory’
删除
ansible all -m file -a ‘path=/demo/ state=absent’

copy模块

copy模块
srcsource 源文件
destdestination 目标
backup backup=yes则会在覆盖前进行备份
mode修改权限
owner修改为指定所有者
group修改为指定用户组

案例

ansible all -m copy -a ‘src=/etc/hosts dest=/etc/hosts backup=yes’

服务管理-systemd

#启动服务
ansible all -m systemd -a ‘name=crond enabled=yes state=started’
#关闭服务
ansible all -m systemd -a ‘name=firewalld enabled=no state=stopped’
#重启ssh
ansible all -m systemd -a ‘name=sshd state=reloaded/restarted’

软件管理

yum模块

yum模块
name指定软件包名字
stateinstalled 安装(present)(默认) removed 删除 (absent) lastest 安装或更新
update_cache可以设置为no加加速,表示不更新本地yum缓存.实际应用建议开启

ansible all -m yum -a 'name=htop,tree,lrzsz ’

** get_url模块**

cget_url下载功能
url指定要下载的地址
dest下载到哪个目录

https: ˌtengine.taobao.org/download/tengine-2.3.3.tar.gz下载到/app/tools目录下面
ansible all -m file -a ‘path=/app/tools/ state=directory’
ansible all -m get_url -a ‘url=“https://tengine.taobao.org/download/tengine- 2.3.3.tar.gz” dest=/app/tools/’

** yum_repository模块**

yum源模块 yum_repository name
yum源中名字 [epel]
descriptionyum源的注释说明 对应的 是name的内容
baseurlyum源中 baseurl 下载地址
enabled是否启动这个源 yes/no
gpgcheck是否启动gpgcheck功能 no
file指定yum源的文件 自动添加.repo 默认与模块名字一致

书写好,yum配置文件,copy分发过去即可

root@m01 ~]# cat /etc/yum.repos.d/epel.repo 
[epel] 
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority 
enabled=1 
gpgcheck=0 
gpgkey=file://	etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
-m yum_repository
-a 'name=epel 
	description="Extra Packages for Enterprise Linux 7 - $basearch" 
	baseurl="http://mirrors.aliyun.com/epel/7/$basearch"
	enabled=yes
	gpgcheck=no '
用户管理

案例

创建www-ans用户uid 2000虚拟用户
ansible all -m user -a ‘name=www-ans uid=2000 shell=/sbin/nologin create_home=no state=present’
批量更新密码
ansible all -m shell -a ‘echo 1 |passwd --stdin demo’

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值