12.18命令模块、软件管理模块

一、命令模块

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'
web02 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to web02 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to web02 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
web01 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to web01 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to web01 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

#查看
[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:
- name: install the latest version of Apache
  yum:
    name: httpd
    state: latest
    
name:
	httpd					   #服务名字
	file					   #软件包的名字
	http://nginx.org/...		#软件包在网上的地址
state:
	latest		#安装最新版本的包
	absent		#卸载软件包
	present		#安装软件包
	
#直接yum安装服务httpd
[root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=present'
相当于在远程机器上执行:yum install -y httpd

#安装本地的rpm包(包一定先传到远程机器上)
[root@m01 ~]# ansible web_group -m yum -a 'name=/tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state=present'
相当于在远程机器上执行:yum localinstall -y /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm

#安装云上的服务
[root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present'
相当于在远程机器上执行:yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm

#卸载服务包
[root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=absent'
2.yum_repository 模块(傻子才用)
[root@m01 ~]# ansible-doc yum_repository
EXAMPLES:
- name: Add repository
  yum_repository:
    name: epel
    description: EPEL YUM repo
    baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
    file: external_repos
    enabled: no
    state: absent

#添加yum源
[root@m01 ~]# ansible web_group -m yum_repository -a 'name=nginx.repo description="nginx stable repo" baseurl="http://nginx.org/packages/centos/7/$basearch/" enabled=1 gpgcheck=1 file=nginx state=present'

#查看添加的yum源
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx.repo]
baseurl = http://nginx.org/packages/centos/7/$basearch/
enabled = 1
gpgcheck = 1
name = nginx stable repo

#参数介绍
name			#yum源中 [] 里面的内容
description		#yum源中 name 部分的内容
baseurl			#yum源中 yum 仓库的地址
gpgcheck		#yum源中 gpgcheck,是否检查yum源
enabled			#yum源中的 enabled,是否开启yum源
file			#yum源的文件名字

#注意:
	1.file参数不修改,name参数也不修改的前提下,是修改yum源
	2.file参数不修改,name参数修改的情况下,是添加yum源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值