第二节:ansible模块

一、命令模块

1.command模块

#默认模块,远程执行命令
[root@m01 ~]# ansible web01 -m command -a 'free -m'
web01 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 128 479 7 364 658
Swap: 1023 0 1023
#不支持特殊字符
[root@m01 ~]# ansible web01 -m command -a "ifconfig eth0 | awk 'NR==2 {print \$2}'"
web01 | FAILED | rc=1 >>
|: Unknown host
ifconfig: `--help' gives usage information.non-zero return code

2.shell模块

[root@m01 ~]# ansible web01 -m shell -a 'free -m'
web01 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 128 479 7 364 658
Swap: 1023 0 1023
#支持特殊字符
[root@m01 ~]# ansible web01 -m shell -a "ifconfig eth0 | awk 'NR==2 {print \$2}'"
web01 | CHANGED | rc=0 >>
10.0.0.7

3.scripts 模块

# 在控制机上
[root@m01 ~]# vim mkdir.sh
#!/bin/bash
mkdir /dir
#远程执行脚本
[root@m01 ~]# ansible web_group -m script -a 'mkdir.sh'
#查看
[root@m01 ~]# ansible web_group -m shell -a 'ls -ld /dir'
web01 | CHANGED | rc=0 >>
drwxr-xr-x. 2 root root 6 Dec 18 08:40 /dir
web02 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Dec 18 08:40 /dir

二、软件管理模块

1.yum模块

[root@m01 ~]# ansible-doc yum
EXAMPLES: # 搜EXAMP可以看到示例
- name: install the latest version of Apache
yum:
name: httpd
state: latest # 如果是lastest就不用指定包的版本,默认就安装到最新
name可以是
要安装的软件包名可以带版本httpd-2.4.6
一个本地rpm包路径
一个rpm包的url地址
state:
latest #确保是安装过的,并且是最新版本,无论有无都会更新至最新版
absent #确保没有安装,即卸载
present #确保是安装过的,有则不更新,无则安装最新
#直接yum安装服务httpd
[root@m01 ~]# ansible 192.168.71.13 -m yum -a "name=httpd-2.4.* state=present" -vv
相当于在远程机器上执行:yum install -y httpd-2.4.*
#安装本地的rpm包(包一定先传到远程机器上,注意是传到远程主机上)
wget https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm,然后传到远程主机的/tmp目录下
[root@m01 ~]# ansible 192.168.71.13 -m yum -a 'name=/tmp/zabbix-release-4.0-2.el7.noarch.rpm state=present disable_gpg_check=yes' # 末尾加上disable_gpg_check=yes,否则会报错Failed to validate GPG
相当于在远程机器上执行:yum localinstall -y /tmp/zabbix-release-4.0-2.el7.noarch.rpm
#安装云上的服务
[root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm state=present disable_gpg_check=yes' # 如果报错,请将https换成http协议
相当于在远程机器上执行:yum install -y url地址
#卸载服务包
[root@m01 ~]# ansible web_group -m yum -a 'name=zabbix* state=absent' # 相当于yum remove zabbix* -y

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 沪ICP备2022009235号 沪公网安备31011802005110号 青浦区尚茂路798弄 联系方式-13697081366