自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

自动化运维系列之Ansible命令应用基础(模块的应用)

模块简介

Ansible可以使用命令行方式进行自动化管理,基本语法如下:

ansible <host-pattern> [-m module_name] [-a args]

<host-pattern> 对哪些主机生效

[-m module_name] 需要使用的模块

[-a args] 模块特有的参数,这里在使用时需加单引号哦!

Ansible的命令行管理工具都是由一系列模块、参数所支持的,可以在命令行后加上-h或--help获取帮助。如使用ansible-doc工具可以通过ansible-doc -h查看其帮助信息。

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

ansible自带了很多模块,能够下发执行Ansible的各种管理任务。下面介绍下Ansible常用的一些核心模块:

  1. command模块

Ansible管理工具使用-m来指定使用模块,默认使用command模块,即不指定-m选项时会默认使用command模块来执行管理任务。

# 例如:
# ansible webserver -m command -a 'date'
# ansible mysql -a 'date'     #不指定-m 选项时,默认使用command模块

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  1. cron模块

Ansible中的cron模块用于定义任务计划。其中有两种状态(state):present表示添加(省略时默认使用添加);absent表示移除。

  • 可以先使用-h获取帮助信息查看cron模块的帮助

ansible-doc -s cron #查看cron模块的帮助信息

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  • 使用cron模块在webserver组的主机上创建任务计划
# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'     #添加一个计划性任务

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  • 这里可以查看下我们添加的任务
# ansible webserver -a 'crontab -l'    #查看计划性任务

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  • 移除计划性任务
# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'    #absent表示移除

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  1. user模块

Ansible中的user模块用于创建新用户和更改、删除已存在的用户。其中name选项用来指明创建的用户名称。

user模块是请求的是useradd, userdel, usermod三个指令

  • 创建用户

ansible webserver -m user -a 'name="zyc"' #创建用户zyc

ansible webserver -m command -a 'tail /etc/passwd' #查看webserver主机上的用户列表

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  • 删除用户

ansible webserver -m user -a 'name="zyc" state=absent' #删除用户

ansible webserver -m command -a 'tail /etc/passwd' #再次查看用户列表

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

  1. group模块

Ansible中的group模块用于对用户组进行管理。

  • 创建mysql组,将mysql用户添加到mysql组。

ansible-doc -s group

ansible mysql -m group -a 'name=mysql gid=306 system=yes' #创建mysql组 将mysql用户添加进去

ansible mysql -a 'tail /etc/group'

ansible mysql -m user -a 'name=mysql uid=306 system=yes group=mysql'

ansible mysql -a 'tail /etc/passwd'

ansible mysql -a 'id mysql'

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

5.copy模块

Ansible中的copy模块用于实现文件复制和批量下发文件。其中src用来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content是通过指定信息内容来生成目标文件。

ansible-doc -s copy

ansible webserver -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
#(属主root 权限640)

ansible webserver -a 'ls -l /opt'

ansible webserver -a 'cat /opt/fstab.back'

ansible webserver -m copy -a 'content="hello heihei!"dest=/opt/fstab.back' #将hello heihei!写入/opt/fstab.back

ansible webserver -a 'cat /opt/fstab.back'

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

6.file模块

Ansible中file模块用来设置文件属性。(path指定文件路径,src指定源文件路径,name或者dest替换创建文件的符号链接)

ansible-doc -s file

ansible webserver -m user -a 'name=mysql system=yes'

ansible webserver -m group -a 'name=mysql system=yes'

ansible webserver -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' #修改文件的属主属组权限等

ansible webserver -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' #设置/opt/fstab.link为/opt/fstab.back的链接文件

ansible webserver -m file -a "path=/opt/fstab.back state=absent" #删除一个文件

ansible webserver -m file -a "path=/opt/school state=touch" #创建一个文件

7.ping模块

Ansible中ping模块用来测试指定主机的连通性

ansible all -m ping #测试所有主机的连通性

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

8.shell模块

Ansible中的shell模块可在被管理主机上运行命令,并支持像管道符号等功能的复杂命令。

  • 创建用户后使用免交互模式给用户设置密码。

ansible mysql -m user -a 'name=zyc' #创建用户

ansible mysql -m shell -a 'echo 123123|passwd --stdin zyc' #给用户设置密码

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

9.yum模块

Ansible中的yum模块负责在被管理主机上安装与卸载软件包,但是需要提前在每个节点配置自己的yum仓库。

name指定需要安装的软件包,需要带上软件包的版本号,否则默认安装最新版

state指定安装软件包的状态,present、latest表示安装;absent表示卸载

ansible webserver -m yum -a 'name=http' #安装http软件

ansible webserver -m yum -a 'name=http state=absent' #卸载http软件

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

10.service模块

Ansible中使用service模块来控制管理服务的运行状态。

enabled表示是否开机自启动,取值为true和false

name定义服务名称

state指定服务状态,取值为:started(开启)、stoped(停止)、restarted(重启)

# ansible webserver -m yum -a 'name=http'   #安装http软件
# ansible webserver -m service -a 'enabled=true name=httpd state=started'
# ansible webserver -a 'systemctl status httpd.service'

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

11.script模块

Ansible中的script模块可以将本地脚本复制到被管理主机上运行。(注意:脚本路径需要使用相对路径)

# ansible-doc -s script
# vim abc.sh    #在本地编写一个abc的脚本
    #!/bin/bash
    echo "hello ansible from script"> /opt/script.txt
# chmod +x abc.sh    #赋予执行权限
# ansible webserver -m script -a 'abc.sh'  #将脚本复制到被管理主机上运行
# ansible webserver -a 'cat /opt/script.txt'    #查看脚本信息

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

12.setup模块

Ansible中的setup模块主要收集、查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)。

# ansible webserver -m setup  #收集信息

自动化运维系列之Ansible命令应用基础(模块的应用)【持续更新中···】

猜你喜欢

转载自blog.51cto.com/13659301/2158523