Ansible常用模块的介绍及使用(二),ansible模块介绍


Ansible常用模块的介绍及使用(二)

由上篇博客Ansible的基础认识以及安装(一)我们简单了解到ansible模块的概念,在这里具体学习一下,以及常用模块的用法。

一.模块的简介

模块(也被称为 “task plugins” 或 “library plugins”)是在 Ansible 中实际在执行的.它们就 是在每个 playbook 任务中被执行的.你也可以仅仅通过 ‘ansible’ 命令来运行它们.

每个模块都能接收参数. 几乎所有的模块都接受键值对(key=value)参数,空格分隔.一些模块 不接收参数,只需在命令行输入相关的命令就能调用.

在 playbook 中, Ansible 模块以类似的方式执行:

- name: reboot the servers
  action: command /sbin/reboot -t now

也可以简写成:

- name: reboot the servers
  command: /sbin/reboot -t now

二.常用模块

1. command模块

默认模块
作用:在各个远程主机上执行命令,但是不能传递参数和变量
例子:获取远程主机IP信息

# ansible all -m command -a "ip a"

2. cron模块

作用:在指定的时间运行指定命令(计划任务)

例子:每隔2分钟再tmp下的log.txt中追加一个信息

[root@ser1 ~]# ansible all -m cron -a 'minute="*/2" job="echo 123>>/tmp/log.txt" name="job1" state="present"'
		10.220.5.253 | SUCCESS => {
		    "changed": true, 
		    "envs": [], 
		    "jobs": [
		        "job1"
		    ]
		}
		10.220.5.252 | SUCCESS => {
		    "changed": true, 
		    "envs": [], 
		    "jobs": [
		        "job1"
		    ]
		}

检查一下

[root@ser1 ~]# ansible all -a "crontab -l"
		10.220.5.252 | SUCCESS | rc=0 >>
		#Ansible: job1
		*/2 * * * * echo 123>>/tmp/log.txt

		10.220.5.253 | SUCCESS | rc=0 >>
		#Ansible: job1
		*/2 * * * * echo 123>>/tmp/log.txt

3. user模块

作用:管理用户
user模块是请求的是useradd, userdel, usermod三个指令
如下的命令含义是创建一个yxx用户,shell类型为/sbin/nologin,uid号为454,系统用户.

 # ansible all -m user -a "name=yxx shell=/sbin/nologin uid=454 state=present"

4. group模块

作用:管理用户组
goup模块请求的是groupadd, groupdel, groupmod 三个指令
命令含义是创建一个名为test1的组,gid为1122,在远程主机可用

# ansible all -m group -a "name=test1 gid=1122 state=present"

5. copy模块

作用:文件的复制
子命令:
src:原文件路径
dest:目标文件路径
mode:指定权限
backup:有目标文件存在,先对原始文件做备份,然后进行覆盖
force:强制执行覆盖
owner:指定复制后的文件的属主
content:替代src,也就是将一段内存作为原,将这个内容直接写入到目标文件中

例子1:

[root@ser1 ~]# ansible all -m copy -a 'src="/etc/passwd" dest=/tmp/passwd1 mode=777 owner=sshd'

例子2

[root@ser1 ~]# ansible all -m copy -a 'content="abcdef" dest=/tmp/passwd1 mode=777 owner=sshd'

6. service模块

作用:管理服务
子命名;
name:指定服务的名称
enabled:指定服务是否开机自启动
state:指定服务最后的状态 started stopped reloaded restarted
runlevel:指定运行级别

例子:

# ansible all -a "systemctl status httpd"
# ansible all -m service -a "name=httpd state=started enabled=on"
# ansible all -a "ss -tnl"

7. ping模块

作用:做ping测试

例子:

[root@ser1 ~]# ansible all -m ping 

8. file模块

作用:实现文件的各种管理操作
子命令:
follow:如果复制的文件是一个连接文件,那么复制以后,依然是连接文件,而且和原始文件一样,指向了相同的文件
force:强制执行覆盖
group:指定复制后的文件的属组
mode:指定权限
owner:指定复制后的文件的属主
path:指定目标路径(可以替代name\dest)
src:原文件路径
state:present &absent

例子:

# ansible all -m file -a 'state=touch path=/tmp/abc.def'
# ansible all -m file -a 'state=link src=/tmp/abc.def path=/tmp/abc.def.ln'
# ansible all -m file -a 'state=absent path=/tmp/abc.def'
# ansible all -m file -a 'force=on state=absent path=/tmp/'
# ansible all -m file -a 'force=on state=absent path=/tmp'

9. shell模块

作用:类似于command,但是可以执行复杂的命令,可以变量
比如统计远程的节点上/tmp目录下有多少个文件

#ansible all  -m shell -a "ls /tmp | wc -l"

10. script模块

作用:将一个本地脚本传递到远程主机上,并运行
无需加多余参数,只需要在-a后面加上本地脚本路径即可

 # ansible all -m script -a /tmp/test.sh

11. yum模块

作用:管理rpm包
子命令:
name:要操作的软件
state:present &absent

例子:远程下载Httpd

# ansible all -m yum -a "name=httpd state=present"

12. setup模块

作用:收集和显示各个被监控节点的信息

#ansible all -m setup

相关内容

    暂无相关文章