自动化运维工具之Ansible(2)——常用模块

ansible常用模块

1)Command

可以在远程主机执行命令,默认模块,可忽略-m选项

注意:

使用command模块在远程主机执行命令中,不会经过远程主机的shell处理,在使用command模块时,如果含有例如"$VARNAME" , "<" , ">" , "|" , ";" , "&" 等符号,需要使用后面介绍的shell模块实现

name参数:指定路径

chdir参数:指定一个目录,在执行命令之前,先进入到指定目录中

crestes参数:指定文件存在(远程主机),则不执行指定命令;(远程主机)不存在,则执行

removes参数:指定文件存在(远程主机),则执行指定命令,(远程主机)不存在,则不执行

Exercise

ansible test70 -m command -a "ls"

ansible test70 -m command -a "chdir=/testdir ls"

ansible test70 -m command -a "creates=/testdir/test echo test"

ansible test70 -m command -a "removes=/testdir/test echo test"

2)Shell

和command相似,用shell执行命令,与command不同的是,shell模块在(远程主机)执行命令时,会经过(远程主机)的/bin/sh程序处理

注意:

这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器

executable参数:指定shell类型,默认bash;指定shell文件时,需要使用绝对路径

Exercise

ansible test70 -m shell -a "chdir=/testdir echo test > test"

ansible test70 -m shell -a 'executable=/bin/csh @ TestNum=666 ; echo $TestNum > /testdir/TestNumFile'

3)Script

在远程主机上运行指定脚本;脚本存在于ansible主机本地

Exercise

ansible test70 -m script -a "chdir=/opt /testdir/atest.sh"

ansible test70 -m script -a "creates=/opt/testfile /testdir/atest.sh"

ansible test70 -m script -a "removes=/opt/testfile /testdir/atest.sh"

4)copy 

  将本地文件复制至远程主机

backup模块: 复制覆盖时先备份

content模块: 指定一些内容当文件复制

dest模块: 目标路径

src模块: 源路径

mode模块: 权限

owner模块: 所有者

  

Exercise

ansible all -m copy -a 'src=/root/ansible/selinux dest=/etc/selinux/config backup=yes'

ansible all -m copy -a 'content="hello world" dest=/data/f1'

5)fetch 

  从远程主机文件复制到本地,必须是一个文件,在本地目录下会生成主机名对应的文件夹,文件存放于文件夹中按远程主机上的目录结构

dest模块: 本地的一个目录

src模块:远程主机系统上的一个文件(必须)

  

Exercise

ansible all -m fetch -a 'src=/var/log/messages dest=/data'

6)archive 

  打包文件

7)unarchive 

  解包文件

8)file 

  创建,删除,设置文件属性

state模块: directory | touch | link | hard | absent 目录,空文件,软链接,硬链接,递归删除

dest | name | path模块: 路径

src模块: 创建软链接时的原文件

mode模块: 权限

owner模块: 所属者

  

Exercise

ansible all -m file -a 'name=/data/testfile.ans state=touch'

ansible all -m file -a 'name=/data/dir1 state=directory'

ansible all -m file -a 'src=/etc/fstab dest=/data/f2 state=link'

9)hostname 

  设置主机名,立即生效并且同步至配置文件

name模块: 主机名

  

Exercise

ansible 192.168.0.11 -m hostname -a 'name=node1'

10)cron

  任务计划

name模块: 名称

job模块: 作业

disabled模块:=true | false 禁用|启用

state模块:=absent 删除

day模块:

hour模块: 小时

minute模块: 分钟

month模块:

weekday模块: 星期

  

Exercise

ansible all -m cron -a 'minute=* weekday=1,3,5 job="/usr/bin/wall FBI warning" name=warningcron'

ansible all -m cron -a 'disabled=true job="/usr/bin/wall FBI warning" name=warningcron'

11)yum 

  管理软件包模块

list模块:installed 列出已安装的软件包

name模块: 包名称

state模块: present | absent latest 安装 | 卸载 | 安装最新版

disable_gpg_check模块:yes 临时禁用gpg检查

update_cache模块:yes 更新yum缓存

  

Exercise

ansible all -m yum -a 'name=vsftpd state=latest'

12)service 

  管理服务模块

name模块: 服务名

enabled模块:yes 开机启动

state模块:started | stopped | restarted | reloaded 启动 | 停止 | 重启 | 重读配置文件

  

Exercise

ansible all -m service -a 'name=vsftpd state=started enabled=yes'

13)user 

  用户管理模块

name模块: 用户名

password模块: 密码

remove模块: 删除用户及家目录

system模块:yes 创建系统用户

shell模块: 设置默认shell

ansible all -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/nginx groups=root,bin uid=80 comment="nginx service"' 

ansible all -m user -a 'name=nginx state=absent remove=yes'

14)group

  组管理模块

15)setup 

  获取远程主机的系统信息

filter模块:var 过滤查看变量参数

猜你喜欢

转载自www.cnblogs.com/Gmiaomiao/p/9115792.html