ansible基本特色与作用:

ansible批量管理服务意义

  1. 提高工作的效率
  2. 提高工作准确度
  3. 减少维护的成本
  4. 减少重复性工作

ansible批量管理服务功能

  1. 可以实现批量系统操作配置
  2. 可以实现批量软件服务部署
  3. 可以实现批量文件数据分发
  4. 可以实现批量系统信息收集

ansible服务特点说明

  1. 管理端和受控端不需要启动服务程序(no server 和 no agent)
  2. 管理端不需要编写配置文件(/etc/ansible/ansible.cfg)
  3. 受控端不需要安装软件程序
  4. 服务程序管理操作模块众多(module)
  5. 利用剧本编写来实现自动化(playbook)


ansible批量管理服务部署:

#下载安装软件,需要依赖epel的yum源,具体可见这篇文章​​centos7优化加固​

yum install -y ansible

#编写主机清单文件,ssh秘钥分发见文章​​ssh秘钥分发​

#往/etc/ansible/hosts文件中追加IP地址,这里写的IP地址必须是已经分发ssh秘钥的机器

cat >>/etc/ansible/hosts <<EOF

172.16.1.200

172.16.1.201

172.16.1.202

EOF

#查看文件里面内容

cat /etc/ansible/hosts

#测试是否可以管理多个主机

语法:ansible 主机名称 -m(指定应用的模块信息) 模块名称 -a(指定动作信息) “执行什么动作”

ansible 172.16.1.200,172.16.1.202 -m command -a "hostname" command模块为默认模块,-m command可以省略

ansible批量管理与模块应用_ansible部署

ansible模块应用:

  1. command模块(默认模块) – Execute commands on targets 在一个远程主机上执行一个命令

重要参数用法

chdir 在执行命令之前对目录进行切换

creates 如果文件存在了,不执行命令操作

removes 如果文件存在了,不执行命令操作

free_form 使用command模块的时候,-a参数后面必须写上一个合法Li nux命令信息

#切换至/tmp目录下,检测/tmp.1.txt是否存在,如果不存在则执行命令touch 2.txt

ansible 172.16.1.201 -m command -a "chdir=/tmp creates=/tmp/1.txt touch 2.txt"


  1. shell(shell模块) – Execute shell commands on targets 在目标服务器上执行命令

#基本上同command模块,区别是command不能识别变量和一些特殊字符('>' '<' '|' ';' '&'),shell是万能的模块,是命令就能执行,但不能保证目标服务器上执行成功与否

ansible 172.16.1.200 -m shell -a "hostname"

ansible批量管理与模块应用_ansible作用_02


  1. script (脚本模块 ) – Runs a local script on a remote node after transferring it 在远程主机上运行本地的脚本

#注意:这里运行的脚本是本地的,执行是在被管理端执行脚本

ansible 172.16.1.201 -m script -a "/root/yum.sh"


  1. copy(复制模块) – Copies files to remote locations 将数据信息进行批量分发

#重要参数用法

src 源路径

dest 目的路径

owner 属主

group 属组

content 在被管理端上写内容进文件(没有则创建,有的话里面内容被清空写入新的信息)

backup 备份,源文件会被覆盖,执行backup命令将源文件进行备份

#以content为例,写一个信息到文件中/root/hosts

ansible 172.16.1.200 -m copy -a "content='123' dest=/root/hosts"

ansible批量管理与模块应用_ansible作用_03


  1. file (文件模块) – Sets attributes of files 设置文件属性信息

基础用法

#重要参数用法

owner 属主

group 属组

mode 权限位

recurse 递归,默认是no,用法:recurse=yes

#目录/tmp/wzy下所有文件目录属主属组权限位更改

ansible 172.16.1.201 -m file -a "dest=/tmp/wuzy recurse=yes owner=wuzy group=wuzy mode=666"

扩展用法

state 参数

=absent ---缺席/删除信息

=directory --- 创建一个目录,一级目录或者多级目录

=file ---检查创建的数据信息是否存在 绿色存在,红色不存在(不重要,鸡肋)

=hard ---创建一个硬链接文件

=link ---创建一个软链接文件

=touch ---创建一个文件信息

创建目录信息(一级或多级目录都可)

ansible 172.16.1.31 -m file -a "dest=/root/wuzy/wuzy01 state=directory"

ansible批量管理与模块应用_ansible作用_04

创建文件信息:

ansible 172.16.1.31 -m file -a "dest=/root/wuzy/wuzy01/wuzy.txt state=touch"

创建链接文件信息:

ansible 172.16.1.31 -m file -a "src=/root/wuzy/wuzy01/wuzy.txt dest=/root/wuzy/wuzy01/wuzy_hard.txt state=hard"

ansible 172.16.1.31 -m file -a "src=/root/wuzy/wuzy01/wuzy.txt dest=/root/wuzy/wuzy01/wuzy_link.txt state=link"

可以利用模块删除数据信息,可以是文件也可以是目录

ansible 172.16.1.31 -m file -a "dest=/root/wuzy/wuzy01/wuzy.txt state=absent"


  1. fetch (拉取数据模块) – Fetch files from remote nodes 从远程主机上拉取文件到本地

ansible 172.16.1.200 -m fetch -a "src=/root/hosts dest=/server"

ansible批量管理与模块应用_ansible作用_05


  1. yum(安装模块) – Manages packages with the yum package manager 管理包安装

重要参数用法

name --- 指定安装软件名称

state --- 指定是否安装软件

installed --- 安装软件(下面两个都是安装,推荐使用installed)

present

latest

absent --- 卸载软件(建议使用absent)

removed

#下载安装iftop工具

ansible 172.16.1.201 -m yum -a "name=iotop state=installed"


ansible批量管理与模块应用_ansible应用模块_06


  1. service (服务模块) – Manage services 管理服务器的运行状态

重要参数用法

name --- 指定管理的服务名称

state --- 指定服务状态

started 启动

restarted 重启

stopped 停止

enabled --- 指定服务是否开机自启动

#远程主机上,启动nfs服务,设置开机自启动

ansible 172.16.1.202 -m service -a "name=nfs state=started enabled=yes"

ansible批量管理与模块应用_ansible应用模块_07


  1. cron (定时任务模块) – Manage cron.d and crontab entries 批量设置多个主机的定时任务信息

重要参数用法

minute Minute when the job should run ( 0-59, *, */2, etc ) 设置分钟信息

hour Hour when the job should run ( 0-23, *, */2, etc ) 设置小时信息

day Day of the month the job should run ( 1-31, *, */2, etc ) 设置日期信息

month Month of the year the job should run ( 1-12, *, */2, etc ) 设置月份信息

weekday Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc ) 设置周信息

job 用于定义定时任务需要干的事情

name 指加上注释信息

disabled 是否注释定时任务(yes/no)

absent 删除指定定时任务(用state指定),ansible可以删除的定时任务,只能是ansible设置好的定时任务

#创建定时任务,名字是time sync,每隔5分钟执行一次命令

ansible all -m cron -a "name='time sync' minute=*/5 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"

ansible批量管理与模块应用_ansible作用_08

#删除定时任务

ansible 172.16.1.200 -m cron -a "name='time sync' state=absent"

ansible批量管理与模块应用_ansible作用_09


  1. mount(挂载模块) – Control active and configured mount points 控制与配置挂载信息

重要参数用法

src 需要挂载的存储设备或文件信息

path 指定目标挂载点目录

fstype 指定挂载时的文件系统类型

state

present/mounted ---进行挂载

present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载

mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载,推荐使用

absent/unmounted ---进行卸载

absent: 会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载

unmounted: 会实现立即卸载, 但是不会删除fstab文件信息,推荐使用

#将目录挂载到nfs机器上

ansible 172.16.1.201 -m mount -a "src=172.16.1.202:/data path=/mnt/nfsdata fstype=nfs state=mounted"

ansible批量管理与模块应用_ansible作用_10


  1. user(用户模块) – Manage user accounts 实现批量创建用户

name 表示用户名

uid 指定用户属于什么uid

group 指定用户属于哪个组,注意,这里没有gid

create_home=no 不创建家目录

shell 指定shell的类型,虚拟用户为 /sbin/nologin

password 设置的是明文密码,不建议用,加密后再用

#创建一个用户

ansible 172.16.1.200 -m user -a "name=wzy password=123456 uid=2000"

#创建一个虚拟用户

ansible 172.16.1.200 -m user -a "name=wuzy create_home=no shell=/sbin/nologin"

ansible批量管理与模块应用_ansible部署_11



  1. ping (检测模块)– Try to connect to host, verify a usable python and return pong on success 用于检测目标主机是否可以用ansible连接上,连上了回复pong

#ping模块后面什么都不需要接,主要是检测是否可以连接的上

ansible 172.16.1.200 -m ping

ansible批量管理与模块应用_ansible部署_12