ansible工作中常用模块

主机清单

[root@localhost ~]# cat /etc/ansible/hosts
[web]
192.168.111.133
192.168.111.132

1.command模块

[root@localhost ~]# ansible web -m command -a ‘ss -ntl’
192.168.111.132 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:631 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 127.0.0.1:6010 :
LISTEN 0 128 :10050 :
LISTEN 0 128 :24007 :
LISTEN 0 128 :555 :
LISTEN 0 128 :111 :
LISTEN 0 5 192.168.122.1:53 :
LISTEN 0 128 :22 :
LISTEN 0 128 [::1]:631 [::]:

LISTEN 0 100 [::1]:25 [::]:

LISTEN 0 128 [::1]:6010 [::]:

LISTEN 0 128 [::]:10050 [::]:

LISTEN 0 128 [::]:111 [::]:

LISTEN 0 128 [::]:22 [::]😗
192.168.111.133 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 :24007 :
LISTEN 0 128 127.0.0.1:10248 :
LISTEN 0 128 :111 :
LISTEN 0 128 127.0.0.1:45648 :
LISTEN 0 128 :22 :
LISTEN 0 100 [::1]:25 [::]:

LISTEN 0 128 [::]:10250 [::]:

LISTEN 0 128 [::]:111 [::]:

LISTEN 0 128 [::]:22 [::]😗

命令模块接受命令名称,后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行。它不会通过shell进行处理,比如$HOME和操作如"<",">","|",";","&" 工作(需要使用(shell)模块实现这些功能)。注意,该命令不支持| 管道命令

下面来看一看该模块下常用的几个命令:

chdir    # 在执行命令之前,先切换到该目录
executable # 切换shell来执行命令,需要使用命令的绝对路径
free_form   # 要执行的Linux指令,一般使用Ansible的-a参数代替。
creates   # 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
removes # 一个文件名,这个文件不存在,则该命令不执行

2.shell模块

shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。
[root@server ~]# ansible web -m shell -a ‘cat /etc/passwd |grep “keer”’
192.168.37.122 | SUCCESS | rc=0 >>
keer❌10001:1000:keer:/home/keer:/bin/sh

192.168.37.133 | SUCCESS | rc=0 >>
keer❌10001:10001::/home/keer:/bin/sh

3.copy模块

这个模块将文件复制到远程主机
src     #被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"
content  #用于替换"src",可以直接指定文件的值
dest    #必选项,将源文件复制到的远程主机的绝对路径
backup   #当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
directory_mode    #递归设定目录的权限,默认为系统默认权限
force    #当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"
others    #所有的 file 模块中的选项可以在这里使用
用法举例如下:
① 复制文件:
[root@localhost ~]# ansible web -m copy -a 'src=/root/pld/ip.txt dest=/data/hello’
192.168.111.133 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: true,
“checksum”: “3a245d1b59a869123e6c28cb8a5ffc23040595f6”,
“dest”: “/data/hello”,
“gid”: 0,
“group”: “root”,
“md5sum”: “86299bcd7603d8e5305096f15c5551cc”,
“mode”: “0644”,
“owner”: “root”,
“secontext”: “system_u:object_r:default_t:s0”,
“size”: 69,
“src”: “/root/.ansible/tmp/ansible-tmp-1593691139.29-38546-261132414761326/source”,
“state”: “file”,
“uid”: 0
}
192.168.111.132 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: true,
“checksum”: “3a245d1b59a869123e6c28cb8a5ffc23040595f6”,
“dest”: “/data/hello”,
“gid”: 0,
“group”: “root”,
“md5sum”: “86299bcd7603d8e5305096f15c5551cc”,
“mode”: “0644”,
“owner”: “root”,
“size”: 69,
“src”: “/root/.ansible/tmp/ansible-tmp-1593691139.32-38548-31662193559162/source”,
“state”: “file”,
“uid”: 0
}

② 给定内容生成文件,并制定权限
[root@localhost ~]# ansible web -m copy -a 'content=“I am book\n” dest=/data/cc mode=666’
192.168.111.133 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: true,
“checksum”: “b2b8da1cf4dcfac6f3ced219b29fd27a3d0f4af0”,
“dest”: “/data/cc”,
“gid”: 0,
“group”: “root”,
“md5sum”: “37070c26532cbfe1882208bee587ced5”,
“mode”: “0666”,
“owner”: “root”,
“secontext”: “system_u:object_r:default_t:s0”,
“size”: 10,
“src”: “/root/.ansible/tmp/ansible-tmp-1593691297.67-38767-104116659070106/source”,
“state”: “file”,
“uid”: 0
}
192.168.111.132 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: true,
“checksum”: “b2b8da1cf4dcfac6f3ced219b29fd27a3d0f4af0”,
“dest”: “/data/cc”,
“gid”: 0,
“group”: “root”,
“md5sum”: “37070c26532cbfe1882208bee587ced5”,
“mode”: “0666”,
“owner”: “root”,
“size”: 10,
“src”: “/root/.ansible/tmp/ansible-tmp-1593691297.32-38769-221550694163468/source”,
“state”: “file”,
“uid”: 0
}

4.file模块

该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
  下面是一些常见的命令:
  > force  #需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no

group  #定义文件/目录的属组。后面可以加上mode:定义文件/目录的权限
owner  #定义文件/目录的属主。后面必须跟上path:定义文件/目录的路径
recurse  #递归设置文件的属性,只对目录有效,后面跟上src:被链接的源文件路径,只应用于state=link的情况
dest  #被链接到的路径,只应用于state=link的情况
state  #状态,有以下选项:

directory:如果目录不存在,就创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件

用法举例如下:
  ① 创建目录:
  [root@server ~]# ansible web -m file -a ‘path=/data/app state=directory’
192.168.37.122 | SUCCESS => {

“changed”: true,
“gid”: 0,
“group”: “root”,
“mode”: “0755”,
“owner”: “root”,
“path”: “/data/app”,
“size”: 6,
“state”: “directory”,
“uid”: 0
}
192.168.37.133 | SUCCESS => {
“changed”: true,
“gid”: 0,
“group”: “root”,
“mode”: “0755”,
“owner”: “root”,
“path”: “/data/app”,
“size”: 4096,
“state”: “directory”,
“uid”: 0
}

我们可以查看一下:
root@server ~]# ansible web -m shell -a ‘ls -l /data’
192.168.37.122 | SUCCESS | rc=0 >>
total 28
drwxr-xr-x 2 root root 6 Dec 6 10:21 app

192.168.37.133 | SUCCESS | rc=0 >>
total 44
drwxr-xr-x 2 root root 4096 Dec 5 10:21 app

③ 删除文件
[root@localhost ~]# ansible web -m file -a ‘path=/data/a state=absent’
192.168.111.132 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“path”: “/data/a”,
“state”: “absent”
}
192.168.111.133 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“path”: “/data/a”,
“state”: “absent”
}

5.fetch模块

该模块用于从远程某主机获取(复制)文件到本地。
  有两个选项:

dest:用来存放文件的目录
src:在远程拉取的文件,并且必须是一个file,不能是目录

具体举例如下:
  [root@server ~]# ansible web -m fetch -a ‘src=/data/hello dest=/data’
192.168.37.122 | SUCCESS => {
“changed”: true,
“checksum”: “22596363b3de40b06f981fb85d82312e8c0ed511”,
“dest”: “/data/192.168.37.122/data/hello”,
“md5sum”: “6f5902ac237024bdd0c176cb93063dc4”,
“remote_checksum”: “22596363b3de40b06f981fb85d82312e8c0ed511”,
“remote_md5sum”: null
}
192.168.37.133 | SUCCESS => {
“changed”: true,
“checksum”: “22596363b3de40b06f981fb85d82312e8c0ed511”,
“dest”: “/data/192.168.37.133/data/hello”,
“md5sum”: “6f5902ac237024bdd0c176cb93063dc4”,
“remote_checksum”: “22596363b3de40b06f981fb85d82312e8c0ed511”,
“remote_md5sum”: null
}

6.cron模块

该模块使用管理cron计划任务的
其使用的语法跟我们的crontab文件中的语法一致,同时,可以指定以下选项:

day= #日应该运行的工作( 1-31, , /2, )
hour= # 小时 ( 0-23, , /2, )
minute= #分钟( 0-59, , /2, )
month= # 月( 1-12, *, /2, )
weekday= # 周 ( 0-6 for Sunday-Saturday, )
job= #指明运行的命令是什么
name= #定时任务描述
reboot # 任务在重启时运行,不建议使用,建议使用special_time
special_time #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务
user # 以哪个用户的身份执行

① 添加计划任务

[root@server ~]# ansible web -m cron -a ‘name=“ntp update every 5 min” minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"’
192.168.37.122 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“ntp update every 5 min”
]
}
192.168.37.133 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“ntp update every 5 min”
]
}
我们可以去查看一下:
[root@server ~]# ansible web -m shell -a ‘crontab -l’
192.168.37.122 | SUCCESS | rc=0 >>
#Ansible: ntp update every 5 min
*/5 * * * * /sbin/ntpdate 172.17.0.1 &> /dev/null

192.168.37.133 | SUCCESS | rc=0 >>
#Ansible: ntp update every 5 min
*/5 * * * * /sbin/ntpdate 172.17.0.1 &> /dev/null

7.yum模块

顾名思义,该模块主要用于软件的安装。
  其选项如下:

name=   #所安装的包的名称
state=  #present—>安装, latest—>安装最新的, absent—> 卸载软件。
update_cache  #强制更新yum的缓存
conf_file  #指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
disable_pgp_check  #是否禁止GPG checking,只用于presentor latest
disablerepo  #临时禁止使用yum库。 只用于安装或更新时。
enablerepo   #临时使用的yum库。只用于安装或更新时。

下面我们就来安装一个包试试看:
  [root@server ~]# ansible web -m yum -a ‘name=htop state=present’

8.service 模块

该模块用于服务程序的管理。
  其主要选项如下:

arguments #命令行提供额外的参数
enabled #设置开机启动。
name= #服务名称
runlevel #开机启动的级别,一般不用指定。
sleep #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
state #有四种状态,分别为:started—>启动服务, stopped—>停止服务, restarted—>重启服务, reloaded—>重载配置

① 开启服务并设置自启动
[root@server ~]# ansible web -m service -a ‘name=nginx state=started enabled=true’
192.168.37.122 | SUCCESS => {
“changed”: true,
“enabled”: true,
“name”: “nginx”,
“state”: “started”,
……
}
192.168.37.133 | SUCCESS => {
“changed”: true,
“enabled”: true,
“name”: “nginx”,
“state”: “started”,
……
}
 我们可以去查看一下端口是否打开:
 [root@server ~]# ansible web -m service -a ‘name=nginx state=started enabled=true’
192.168.37.122 | SUCCESS => {
“changed”: true,
“enabled”: true,
“name”: “nginx”,
“state”: “started”,
……
}
192.168.37.133 | SUCCESS => {
“changed”: true,
“enabled”: true,
“name”: “nginx”,
“state”: “started”,
……
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值