Ansible可使用命令行方式进行自动化管理,基本语法以下:mysql
ansible <host-pattern> [-m module_name] [-a args]web
<host-pattern> 对哪些主机生效sql
[-m module_name] 须要使用的模块shell
[-a args] 模块特有的参数,这里在使用时需加单引号哦!vim
Ansible的命令行管理工具都是由一系列模块、参数所支持的,能够在命令行后加上-h或--help获取帮助。如使用ansible-doc工具能够经过ansible-doc -h查看其帮助信息。bash
ansible自带了不少模块,可以下发执行Ansible的各类管理任务。下面介绍下Ansible经常使用的一些核心模块:运维
Ansible管理工具使用-m来指定使用模块,默认使用command模块,即不指定-m选项时会默认使用command模块来执行管理任务。ide
# 例如: # ansible webserver -m command -a 'date' # ansible mysql -a 'date' #不指定-m 选项时,默认使用command模块
Ansible中的cron模块用于定义任务计划。其中有两种状态(state):present表示添加(省略时默认使用添加);absent表示移除。工具
ansible-doc -s cron #查看cron模块的帮助信息测试
# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"' #添加一个计划性任务
# ansible webserver -a 'crontab -l' #查看计划性任务
# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent' #absent表示移除
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 webserver -m user -a 'name="zyc" state=absent' #删除用户
ansible webserver -m command -a 'tail /etc/passwd' #再次查看用户列表
Ansible中的group模块用于对用户组进行管理。
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'
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'
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 #测试全部主机的连通性
8.shell模块
Ansible中的shell模块可在被管理主机上运行命令,并支持像管道符号等功能的复杂命令。
ansible mysql -m user -a 'name=zyc' #建立用户
ansible mysql -m shell -a 'echo 123123|passwd --stdin zyc' #给用户设置密码
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软件
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'
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' #查看脚本信息
12.setup模块
Ansible中的setup模块主要收集、查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)。
# ansible webserver -m setup #收集信息