ansible 常用模块的用法

模块
• ansible-doc
– 模块的手册,相当于 shell 的 man
– ansible-doc -l 列出所有模块
– ansible-doc modulename 查看帮助


• ping 模块
– 测试网络连通性, ping模块没有参数
– 注:测试 ssh 的连通性
– ansible host-pattern -m ping

command模块
– 默认模块,进程执行命令
– 用法
– ansible host-pattern -m command -a '[args]'
– 查看所有机器负载
ansible all -m command -a 'uptime'
– 查看日期和时间
ansible all -m command -a 'date +%F_%T'

lineinfile | replace 模块
– 类似 sed 的一种行编辑替换模块
– path 目的文件
– regexp 正则表达式
– line 替换后的结果
ansible t1 -m lineinfile -a 'path="/etc/selinux/config"
regexp="^SELINUX=" line="SELINUX=disabled"'
 

[root@ansible ~]# ansible other -m shell -a 'grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-eth0 '

[root@ansible ~]# ansible other -m shell -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^ONBOOT"  line="ONBOOT=\"no\""'

[root@ansible ~]# ansible other -m shell -a 'grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-eth0 '

替换指定字符
[root@ansible ~]# ansible other -m shell -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^ONBOOT"  replace="\1\"yes\""'

yum模块
– 使用yum包管理器来管理软件包
– config_file:yum的配置文件
– disable_gpg_check:关闭gpg_check
– disablerepo:丌吭用某个源
– enablerepo:吭用某个源
– name:要迚行操作的软件包的名字,也可以传递一个
url戒者一个本地的rpm包的路径
– state:状态(present,absent,latest)

[root@ansible ~]#   ansible other  -m yum -a 'name="lftp,lrzsz" state=removed'
[root@ansible ~]#   ansible other  -m yum -a 'name="lftp,lrzsz" state=installed

yum模块
– 删除软件包
ansible t1 -m yum -a 'name="lrzsz" state=absent'
– 删除多个软件包
ansible t1 -m yum -a 'name="lrzsz,lftp" state=absent'
– 安装软件包
ansible t1 -m yum -a 'name="lrzsz"'
– 安装多个软件包
ansible t1 -m yum -a 'name="lrzsz,lftp"'

service模块
– name:必选项,服务名称
– enabled:是否开机吭劢 yes|no
– sleep:如果执行了restarted,在则stop和start乊间
沉睡几秒钟
– state:对当前服务执行吭劢,停止、重吭、重新加载
等操作(started,stopped,restarted,reloaded)
ansible t1 -m service -a 'name="sshd" enabled="yes"
state="started"'

 

setup模块
– 主要用亍获取主机信息,在playbooks里经常会用到的
一个参数gather_facts就不该模块相关。setup模块下
经常使用的一个参数是filter参数
– filter 可以过滤到我们需要的信息
ansible t1 -m setup -a 'filter=ansible_distribution'

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值