Ansible 基础模块

1、语法

ansible hosts -m module_name(模块名) -a job(对主机进行什么样得操作)

例:案例

[root@localhost ~]# ansible dbserver -m command -a 'ls /root'
# 配置主机清单时dbserver中,只写入了1.4,所以结果只有1.4主机运行ls /root
192.168.1.4 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片

#查看ansible一共有多少个模块
[root@localhost ~]# ansible-doc -l | wc -l
3387

2、查看模块得帮助信息 -s

#查看模块得帮助信息 -s 模块名 
[root@localhost ~]# ansible-doc -s ping > /root/ping.mail
[root@localhost ~]# ansible-doc -s shell > /root/shell.mail

ping模块

测试终端和远程主机是否能够连接成功

语法:
ansible  hosts主机清单  -m  模块名

[root@localhost ~]# ansible webserver -m ping
[root@localhost ~]# ansible all -m ping

all代表所有主机清单

command模块

在远程主机上执行指定得命令 如:cat ls ,不能使用特殊得符号 :| > >>

语法:
ansible  主机清单 -m  模块名 -a  '执行命令'

[root@localhost ~]# ansible dbserver -m command  -a 'ls /root'


creates:当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行
removes:当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行

# /etc/fstab文件存在,则cat命令不执行
[root@localhost ~]# ansible dbserver -m command -a 'creates=/etc/fstab   cat /etc/fastab'
192.168.168.21 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists

# /etc/fstab文件存在,则cat命令执行
[root@localhost ~]# ansible dbserver -m command -a 'removes=/etc/fstab   cat /etc/fstab'
192.168.168.21 | CHANGED | rc=0 >>
 /etc/fstab
 Created by anaconda on Wed Mar 11 02:53:53 2020

shell模块

在远程主机上执行复杂的命令,比较好用得模块

语法:
ansible 主机清单 -m  模块名 -a  '执行命令'

[root@localhost ~]# ansible dbserver -m shell -a 'ls /root | grep .cfg'
[root@localhost ~]# ansible dbserver  -m shell -a 'mkdir /root/ansible'

user 用户模块

管理用户,创建用户

常见的用户操作:
useradd 用户名		# 创建普通用户
passwd 用户名		# 设置用户密码
useradd -M -s /sbin/nologin 用户名		# 创建的用户没有家目录,不能登录
useradd -u 用户id -g gid 用户名   		# 创建用户时指定uid和gid
userdel -r 用户						# 连同用户家目录一起删除用户


1.创建用户
[root@localhost ~]# ansible dbserver -m user -a 'name=zs'
name: 指定用户名,如果用户不存在,则创建该用户

#2.设置用户密码
添加密码不识别字符,只识别hash之后的字符
[root@localhost ~]# openssl passwd -1 123.com
$1$MTimIo.c$P6.1ERLnJXUid6tqS4wpK0		#胜场一个MD5的hash码
[root@localhost ~]# ansible dbserver -m user -a 'name=zs 	password=$1$MTimIo.c$P6.1ERLnJXUid6tqS4wpK0'  #为指定用户添加密码

3.指定用户的uid号,附加组
append=yes	#增量添加附加组
uid:指定用户的uid
groups:指定用户的附加组
[root@localhost ~]# ansible dbserver -m user -a 'uid=1030 name=ls'
[root@localhost ~]# ansible dbserver -m user -a 'groups=ls name=zs'
[root@localhost ~]# ansible dbserver -m user -a 'name=ww groups=ls append=yes'


4.删除用户
remove=yes	删除用户的同时删除家目录
state=absent   删除用户
[root@localhost ~]# ansible dbserver -m user -a 'name=ww state=absent remove=yes'	#即删除用户,又删除了用户家目录

创建无家目录,不能登录的用户
[root@localhost ~]# ansible 操作对象 -m user -a 'name=用户名 create_home=no shell=/sbin/nologin state=present'  

创建用户是指定uid和基本组
[root@localhost ~]# ansible 操作对象 -m user -a 'name=用户名 uid=id号 group=组名 state=present'

group组模块

创建组,管理组

name:指定组
gid:指定gid
state=absent 删除指定组

1.创建组并指定gid
[root@localhost ~]# ansible dbserver -m group -a 'name=gourp1 gid=1100'

2.删除组
[root@localhost ~]# ansible dbserver -m group -a 'name=gourp2 state=absent'

script模块

在远程主机执行主控制端脚本

chdir=/目录    进入到指定目录
creates 文件存在 脚本不执行
removes 文件存在 脚本执行

1.在ansible创建一个脚本,并赋予执行权限
[root@localhost ~]# vim 1.sh
#!/bin/bash
echo 'hello world' > /tmp/1.txt
[root@localhost ~]# chmod +x 1.sh 

2.在被控端执行脚本
[root@localhost ~]# ansible db -m script -a 'chdir=/root 1.sh'
[root@localhost ~]# ansible db -m command -a 'cat /tmp/1.txt'
192.168.10.22 | CHANGED | rc=0 >>
hello world

3.可以针对某个文件存在或者不存在来执行脚本
[root@localhost ~]# ansible db -m script -a 'creates=/etc/passwd chdir=/root 2.sh'
192.168.10.22 | SKIPPED		/etc/passwd文件存在,所以后面的语句被跳过

copy模块

将主控端的文件复制到远程主机,只针对文件

src  源文件路径
dest   目标文件路径
content  将指定内容覆盖写入到目标主机文件中
force=no 	当主控端拷贝的文件名和目标名一致,但是内容不一致,放弃拷贝
force=yes   当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行覆盖
backup=yes	 当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行备份

1.将源文件,copy到被控端
[root@localhost ~]# ansible db -m copy -a 'src=/root/shm.txt dest=/tmp/shm.txt'

2.将指定内容覆盖到被控端文件内
[root@localhost ~]# ansible db -m copy -a "content='this is content\n hello world' dest=/tmp/shm.txt"

3.将文件发送到被控端,如果内容不同则进行备份,并覆盖
[root@localhost ~]# ansible db -m copy -a 'src=/root/shm.txt dest=/tmp/shm.txt backup=yes'
[root@localhost ~]# ansible db -m shell -a 'ls /tmp/shm.*'
192.168.10.22 | CHANGED | rc=0 >>
/tmp/shm.txt
/tmp/shm.txt.6134.2020-03-13@19:31:25~


权限参数
owner  属主
group  属组
mode   权限
1. 将 mode.txt 文件拷贝到被控端,并设置好属主,属组,权限
[root@localhost ~]# ansible db -m copy -a 'src=/root/mode.txt dest=/tmp owner=root group=qqq mode=744'
[root@localhost ~]# ansible db -m shell -a 'ls -lhd /tmp/mode.txt'
192.168.10.22 | CHANGED | rc=0 >>
-rwxr--r--. 1 root qqq 0 3月  13 19:47 /tmp/mode.txt

fetch模块

将被控端得指定文件,拉取到Ansible服务器

src: 被控端源文件
dest: 主控端目录位置

1.拉取一个/tmp/1.txt 到主控端
[root@localhost ~]# ansible db -m fetch -a 'src=/tmp/1.txt dest=/tmp/db-1.txt'
[root@localhost ~]#ll /tmp/db-1.txt/
192.168.10.22/tmp/1.txt 			以IP地址的形式保存在主控端

2.拉取被控端 以 .log结尾得所有文件。
由于 此模板只能拉取一个文件,所以将所有得log日志打包后,拉取到Ansible主控端

打包:
[root@localhost ~]# ansible db -m shell -a 'tar -zcf /root/log.tar.gz /var/log/*.log'
拉到至主控端:
[root@localhost ~]# ansible db -m fetch -a 'src=/root/log.tar.gz dest=/tmp/'
查看tar包内容:
[root@localhost ~]# tar tvf /tmp/192.168.10.22/root/log.tar.gz

unarchive模块

copy:copy=no为源tar包从被控端解压。 copy=tes 为源tar包从主控端解压
src:源tar包路径
dest:解压到的目标位置
mode:解压后的文件权限

#解压软件包
[root@localhost ~]# ansible db -m unarchive -a 'src=/root/tmp.tar.gz dest=/root copy=no'

archive模块

path:源路径,准备打包的文件
dest:打成什么格式的包,放到什么位置
mode:权限

#打包
[root@localhost ~]# ansible db -m archive -a 'path=/tmp/*.txt dest=/root/tmp.tar.gz'

yum模块

在远程主机上使用安装软件

state:installed 安装软件包
	   removed  卸载软件包
disable_gpg_check=yes  :取消密钥的认证
update_cache=yes  更新缓存,需要在指定安装包时使用

1.确定被控端的yum本地源或者网络源

2.安装一个httpdyum源
[root@localhost ~]# ansible db -m yum -a 'name=wget* state=installed'

2.安装一个独立的rpm软件包
[root@localhost ~]# ansible db -m yum -a 'name=rpm软件包'

3.安装一个dstat监控程序,并清除缓存
[root@localhost ansible]# ansible db -m yum -a 'name=dstat update_cache=yes'

service模块

管理远程主机上的服务

name=服务名
state=started开启 / stopped 停止 / reloaded 重新加载 /  restarted重启
enabled=yes 开机自启

1.启动httpd服务
[root@localhost ~]# ansible db -m service -a 'name=httpd state=started'

2.让httpd服务开机自启
[root@localhost ~]# ansible db -m service -a 'name=httpd enabled=yes'

3.检测httpd是否开机自启
[root@localhost ~]# ansible db -m shell -a 'systemctl is-enabled httpd'
enabled			开机自启状态
disablednon-zero return code		非开机自启状态

setup模块

查看远程主机上得信息

[root@localhost ~]# ansible db -m setup

file 模块

创建或者和删除远程主机上的文件或者目录

path 指定文件 	如果远程主机上没有该文件,则进行创建
state 创建类型   touch 文件  directory 目录 
state=absent  删除文件或者目录

link 软连接	src=源文件名  path=目标链接文件名
hard 硬链接	src=源文件名  path=目标链接文件名

以下三个参数,既可以修改,也可以自动添加
mod:权限	可以在添加时设置特殊权限,前提要有执行权限( set 粘滞位)
owner:属主
group:属组

1.创建一个普通文件
[root@localhost ~]# ansible db -m file -a 'path=/tmp/file.txt state=touch'

2.创建一个目录
[root@localhost ~]# ansible db -m file -a 'path=/tmp/test state=directory'

3.创建一个软连接文件
[root@localhost ~]# ansible db -m file -a 'state=link src=/tmp/test path=/tmp-test-ln'

4.创建一个硬链接文件
[root@localhost ~]# ansible db -m file -a 'state=hard src=/etc/passwd  path=/passwd'

5.创建一个文件,并设置权限,属主,属组
[root@localhost ~]# ansible db -m file -a 'state=touch path=/tmp/3.txt mode=744 owner=zs group=zs'

5-2.修改一个文件得权限,属主,属组
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt mode=777'
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt owner=root'
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt group=root'

set:4777 属主特殊权限
	2777 属组特殊权限
	1777 执行特殊权限
	7777 全部特殊权限

5.3.修改一个文件得set位特殊权限
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt mode=2777'
[root@localhost ~]# ansible db -m command -a 'ls -lhd /tmp/3.txt'
-rwxrwsrwx. 1 root root 0 3月  13 21:02 /tmp/3.txt

6.删除文件 / 目录
[root@localhost ~]# ansible db -m file -a 'state=absent path=/tmp-test-ln'
[root@localhost ansible]# ansible db -m file -a 'state=absent path=/目录名'

hostname模块

修改主机名操作

`hostname 主机名`        临时
`hostnamectl set-hostname 主机名` 永久

ansible 操作对象 -m hostname -a 'name=主机名'

Cron模块

执行任务计划

minute	分		minute=* 每分钟
hour 	时		special_time=hourly  每小时
day	  天
month	月
weekday	周
job  计划任务的工作
name=*  计算任务名称
disbaled=true  禁用某个计划任务
disabled=false  再次开启某个计划任务
state=absent  删除某个计划任务
 
 1.每周2,4,6 间隔1分钟告警
 [root@localhost ~]# ansible db -m cron -a 'minute=* weekday=2,4,6 job="/usr/bin/wall warning" name=warningcron'

2.将设置好的计划任务禁用(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ~]# ansible db -m cron -a 'disabled=true name=warningcron job="/usr/bin/wall warning"'

3.开启某个计划任务(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ~]# ansible db -m cron -a 'disabled=false name=warningcron job="/usr/bin/wall warning"'

4.删除创建好的任务计划(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ansible]# ansible db -m cron -a 'name=warningcron job="/usr/bin/wall warning" state=absent'

lineinfile 模块

替换文件中的内容,添加内容到指定文件位置

regexp='^ $' 正则匹配,匹配数字
line='...'   将匹配的内容替换成什么,直接是line时候  是在文件的最后添加内容

insertbefore=''  在文件匹配到的内容前面添加啊
insertafter=''    在文件匹配到的内容后面添加

1.匹配以 192开头的IP地址,并进行替换
[root@localhost ansible]# ansible db -m command -a 'cat /tmp/line.txt'
192.168.168.10

[root@localhost ansible]# ansible db -m lineinfile -a 'regexp='^192.' line='192.168.168.12'  path=/tmp/line.txt'
192.168.168.12


2.在匹配的内容下方添加新内容
[root@localhost ansible]# ansible db -m lineinfile -a 'insertafter='255.255.255.0' line='192.168.168.2'  path=/tmp/line.txt'

3.在匹配的内容上方添加新内容
[root@localhost ansible]# ansible db -m lineinfile -a 'insertbefore='192.168.168.12' line='ONBOOT=yes'  path=/tmp/line.txt'

4.在文件最下方添加内容
[root@localhost ansible]# ansible db -m lineinfile -a "line='soft 4096' path=/tmp/line.txt"

blockinfile 模块

给文件中添加一段内容,并注明标志

block=''   文档内容
marker='#{mark}'   标志信息

例句: 添加一段内容,并声明标志
[root@localhost ansible]# ansible db -m blockinfile -a "block='nginx' path=/tmp/line.txt marker='#{mark}nginx'"
----
#BEGINnginx
nginx
#ENDnginx

mount模块

永久性,临时性挂载某个文件

fstype   指定挂载文件的系统类型,必须指定
path   定义挂载到哪个目录,必须指定
src    定义挂载内容
state  挂载状态  
  mounted进行挂载,修改/etc/fstab信息
  unmounted临时卸载,不修改/etc/fstab信息
  absent永久性卸载,并修改 /etc/fstab信息

- name: 永久挂载
         mount: src=/dev/sr0 path=/media state=mounted fstype=iso9660
         
永久性卸载
[root@localhost ansible]# ansible nginx -m mount -a \
> 'fstype=iso9660 src=/dev/sr0 path=/media state=absent'