ansible自动化管理(2)--常用模块配置

ansible模块配置

1.获取ansible命令帮助

1.1.查看某个模块的帮助信息

[root@ansible ~]# ansible-doc 模块名 

1.2.列出所有模块

[root@ansible ~]# ansible-doc -l	

1.3.列出指定模块的所有选项

[root@ansible ~]# ansible-doc yum -s

模块在ansible中是指需要快速执行一条命令,并且不需要保存的命令,对于负载的命令则用playbook。

ansible颜色提示说明

黄色:对远程节点进行相应修改
绿色:对远程节点不进行相应修改,或者只是对远程节点信息进行查看
红色:操作命令有一场
紫色:表示对命令执行发出警告信息会给一下建议

2.command命令模块

实例1:查看主机名

[root@ansible ~]# ansible web_clust -m command -a "hostname" -f 3
192.168.81.240 | CHANGED | rc=0 >>
backup
192.168.81.230 | CHANGED | rc=0 >>
nfs
192.168.81.220 | CHANGED | rc=0 >>
web

-f =forks 结果返回的数量,比如一次对2台机器进行操作

实例2:创建用户

[root@ansible ~]# ansible web_clust -m command -a "useradd jiangxiaol"
192.168.81.230 | CHANGED | rc=0 >>

192.168.81.220 | CHANGED | rc=0 >>

192.168.81.180 | CHANGED | rc=0 >>

3.shell模块

实例1:查看磁盘空间

[root@ansible ~]# ansible web_clust -m shell -a "df -hT | grep '/$'"
192.168.81.230 | CHANGED | rc=0 >>
/dev/mapper/centos-root xfs        47G  8.2G   39G   18% /
192.168.81.240 | CHANGED | rc=0 >>
/dev/mapper/centos-root xfs        47G  5.5G   42G   12% /
192.168.81.220 | CHANGED | rc=0 >>
/dev/mapper/centos-root xfs        47G   15G   33G   30% /

实例2:创建用户并设置密码

[root@ansible ~]# ansible web_clust -m shell -a "echo '123' | passwd --stdin jyi"
192.168.81.240 | FAILED | rc=252 >>
更改用户 jyi 的密码 。
passwd:所有的身份验证令牌已经成功更新。
192.168.81.230 | FAILED | rc=252 >>
更改用户 jyi 的密码 。
passwd:所有的身份验证令牌已经成功更新。
192.168.81.220 | CHANGED | rc=0 >>
更改用户 jyi 的密码 。
passwd:所有的身份验证令牌已经成功更新。

command和shell的区别

​ command只支持单条命令

​ shell支持管道这种组合命令

4.yum模块

语法格式

[root@ansible ~]# ansible web -m yum -a "name=httpd state=installed"
参数
name		//指定要安装软件包的名称,如果有多个用","隔开
state		//指定使用yum的方法
	installed|present		//安装软件包
	removed|absent			//移除软件包
	latest					//安装最新软件包

案例1:安装已存在的软件包

[root@ansible ~]# ansible web -m yum -a "name=httpd,ftp state=installed"
192.168.81.220 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.4.6-88.el7.centos.x86_64 providing httpd is already installed", 
        "ftp-0.17-67.el7.x86_64 providing ftp is already installed"
    ]
}

案例2:删除指定的安装包

[root@ansible ~]# ansible web -m yum -a "name=httpd,ftp state=removed"

案例3:安装指定软件(present)

[root@ansible ~]# ansible web -m yum -a "name=httpd,ftp state=present"

5.copy模块

语法格式

[root@ansible ~]# ansible web_clust -m copy -a "src=源文件 dest=目标文件"
参数
src			//推送数据的源文件信息
dest		//推送数据的目标路径
backup		//对目标端已有的文件进行备份再传输
content		//直接批量在被管理端文件中添加内容,如果原来有内容直接覆盖
group		//将本地文件推送到远端,指定文件属组信息
owner		//将本地文件推送到远端,指定文件属主信息
mode		//将本地文件推送到远端,指定文件权限信息

案例1:将本地/etc/hosts文件推送到远端按时间信息备份

ansible端
[root@ansible ~]# ansible web_clust -m copy -a "src=/etc/hosts dest=/etc/ backup=yes"
192.168.81.180 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "backup_file": "/etc/hosts.21644.2020-06-10@22:27:31~", 
    "changed": true, 
    "checksum": "ef1e1a00259d1710511577d93041afabd43dc651", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "80dcb22cf7b283e617248324201e390e", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:net_conf_t:s0", 
    "size": 241, 
    "src": "/root/.ansible/tmp/ansible-tmp-1591799249.82-11243-223340081540480/source", 
    "state": "file", 
    "uid": 0
}

远程端:
[root@web ~]# ls /etc/*hosts*
/etc/hosts  /etc/hosts.74203.2020-06-03@23:16:11~ 

案例2:添加rsync认证文件和rsync客户端密码文件

添加rsync认证文件

对rsync服务器写入一个文件并设置权限和属组
[root@ansible ~]# ansible  backup -m copy -a "content='rsync_backup:123456' dest=/etc/rsync_passwdfile owner=root group=root mode=600"

测试文件属性和内容
[root@ansible ~]# ansible backup -m shell -a "ls -l /etc/rsync_passwdfile && cat /etc/rsync_passwdfile"

添加客户端密码文件

[root@ansible ~]# ansible web_clust -m copy -a "content=123456 dest=/etc/rsync_pass owner=root group=root mode=600"
192.168.81.240 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "7c4a8d09ca3762af61e59520943dc26494f8941b", 
    "dest": "/etc/rsync_pass", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "e10adc3949ba59abbe56e057f20f883e", 
    "mode": "0600", 
    "owner": "root", 
    "size": 6, 
    "src": "/root/.ansible/tmp/ansible-tmp-1591199068.62-53706-208979644933057/source", 
    "state": "file", 
    "uid": 0
}

[root@ansible ~]# ansible web_clust -m shell -a "ls -l /etc/rsync_pass && cat /etc/rsync_pass"
192.168.81.240 | CHANGED | rc=0 >>
-rw------- 1 root root 6 6月   3 23:44 /etc/rsync_pass
123456


6.service模块

语法格式

ansible 主机组	-m service -a "name=服务 state=状态"
参数
name		//服务的名称
state		//服务的状态
	started			启动
	restarted		重启
	stoped			停止
	reloaded		重载
enabled		//是否开机自启	如果=no则表示disable

案例1:开启web主机组的http服务,并设置端口号,创建首页

1.更改端口
[root@ansible ~]# ansible web -m shell -a "sed -ri '/^Listen/c Listen 80' /etc/httpd/conf/httpd.conf"

2.开启服务
[root@ansible ~]# ansible web -m service -a "name=httpd state=started enabled=yes"

3.开启服务
[root@ansible ~]# ansible web -m copy -a "content='this is a test' dest=/var/www/html/index.html"


7.group模块

语法格式

ansible 模块名 -m group -a "name=组名 gid=组id"
参数
name		//需要管理的组名,也就是要对那个组进行管理
gid			//设置组id
state		//执行状态
	absent		//删除
	present		//创建(默认)

案例1:创建组名www,并设置gid为777

all表示所有主机组

[root@ansible ~]# ansible all -m group -a "name=group1 gid=777 "
192.168.81.230 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 777, 
    "name": "group1", 
    "state": "present", 
    "system": false
}

案例2:修改www组的gid为888

[root@ansible ~]# ansible all -m group -a "name=www gid=888"

案例3:删除www组

[root@ansible ~]# ansible all -m group -a "name=www gid=666 state=absent"

删除组的时候可能会报错,下图报错是有程序正在使用该组

解决方法,将对应的进程杀掉即可
[root@ansible ~]# ansible all -m shell -a "ps aux | awk '/^www/{print $2}' | xargs kill -9"

8.user模块

语法格式

ansible 主机组 -m user -a "name=用户名,uid=用户id,group=组id或者组名"
参数
name		//用户名
uid			//用户的uid
group			//组id或者组名
state			//执行状态
	absent	//删除
	present	//创建
shell		//登录shell,/bin/bash /sbin/nologin
create_home		//创建用户时,是否创建家目录
password		//用户密码,不能使用明文

案例1:创建一个而用户jiangxl,指定uid为10000,gid为10007,并设置密码为123

注意:要使用加密的密码

1.创建加密密码
[root@ansible ~]# echo "123" | openssl passwd -1 -stdin 
$1$c1D.OvTM$Ar9Yy8WXVmtGiU2O3FbPi.
passwd -1表示使用MD5进行加密

2.创建用户,注意password后面的字符串要用双引号引用
[root@ansible ~]# ansible all -m user -a 'name=jiang uid=7777 group=6666 password="$1$c1D.OvTM$Ar9Yy8WXVmtGiU2O3FbPi."'

案例2:创建一个程序用户linux,指定uid6666,gid6666,不允许登录,不允许创建家目录

1.首先创建gid为6666的组
[root@ansible ~]# ansible all -m group -a "name=linux gid=6666"
192.168.81.240 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 6666, 
    "name": "linux", 
    "state": "present", 
    "system": false
}

2.再创建程序用户
[root@ansible ~]# ansible all -m user -a "name=linux uid=6666 group=6666 shell=/sbin/nologin create_home=no"
192.168.81.240 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": false, 
    "group": 6666, 
    "home": "/home/linux", 
    "name": "linux", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": false, 
    "uid": 6666
}

案例3:删除jiang用户

[root@ansible ~]# ansible all -m user -a "name=jiang uid=7777 state=absent"

9.file模块

语法格式

ansible 主机组 -m file -a "path=目标路径 owner=属主 group=属组 mode=权限"
参数
path	//目录文件路径,相当于copy模块的dest,其他模块的name
src		//源文件路径
owner		//属主
group		//属组
mode		//权限
state		//控制状态
	absent		//删除
	directory	//创建目录
	file		//修改文件属性(默认)
	touch		//创建文件
	link hard	//链接
recurse		//递归,recurse=yes

案例1:修改目录/data属性,属主6666,属组6666

[root@ansible ~]# ansible all -m file -a "path=/data owner=6666 group=6666"

案例2:创建/data2目录,并设置父目录、子目录属主属组都为6666并且权限为755

[root@ansible ~]# ansible all -m file -a "path=/data2 owner=linux group=linux mode=755 state=directory recurse=yes"
192.168.81.220 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 6666, 
    "group": "linux", 
    "mode": "0755", 
    "owner": "linux", 
    "path": "/data2", 
    "size": 6, 
    "state": "directory", 
    "uid": 6666
}

[root@ansible ~]# ansible all -m command -a "ls -ld /data2"
192.168.81.220 | CHANGED | rc=0 >>
drwxr-xr-x 2 linux linux 6 6月   7 18:11 /data2

案例3:创建文件

[root@ansible ~]# ansible all -m file -a "name=/data2/aa.txt state=touch"

案例4:创建链接文件

[root@ansible ~]# ansible all -m file -a "src=/etc/hosts path=/tmp/hosts state=link"

10.mount模块

语法格式

ansible 主机组 -m mount -a "src=设备路径 path=挂载点 fstype=文件系统 state=挂载类型"
参数
path		//挂载点
src			//需要挂载的设备
fstype		//挂载设备的文件系统
	iso9660	//光驱
	ext4、xfs、nfs
	cifs samba的共享文件系统
	ntfs windows磁盘文件系统
opts		//挂载属性
	notime
	noexec
	nosuid
state		//挂载动作
	present		//开机挂载,仅将挂载配置写入/etc/fstab并不会真的挂载
	mounted		//挂载设备,并将配置写入/etc/fstab
	unmounted		//卸载设备,不会清除/etc/fstab写入的配置
	absent		//卸载设备,并清理/etc/fstab写入的配置
   

案例:配置nfs服务

1.安装服务器

[root@ansible ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"

2.修改配置文件

方法1:通过传输文件来实现
[root@ansible ~]# mkdir /server/conf -p
[root@ansible ~]# echo "/data 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)" > /server/conf/exports
[root@ansible ~]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/"

方法2:使用copy模块的content参数
[root@ansible ~]# ansible nfs -m copy -a "content='/data 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)' dest=/etc/exports"

3.启动服务

[root@ansible ~]# ansible nfs -m service -a "name=rpcbind state=restarted"

[root@ansible ~]# ansible nfs -m service -a "name=nfs state=restarted"

4.创建用户、目录、并修改所属

创建组
[root@ansible ~]# ansible nfs -m group -a "name=linuxnfs gid=6666"

创建用户
[root@ansible ~]# ansible nfs -m user -a "name=linux uid=6666 group=6666 shell=/bin/bash create_home=yes"

创建目录并修改所属
[root@ansible ~]# ansible nfs -m file -a "path=/data2 owner=linuxnfs group=linuxnfs state=directory recurse=yes"

5.在web上挂载nfs目录

[root@ansible ~]# ansible web -m mount -a "src=192.168.81.230:/data2 path=/var/www/html fstype=nfs state=mounted"

11.script模块

语法格式

ansible backup -m script -a "脚本"
直接写脚本路径即可

案例:安装rsync

1.写脚本

[root@ansible ~]#  vim /server/scripts/rsync.sh 
#!/bin/bash
#批量部署rsync
yum -y install rsync &>/dev/null
echo -e "\e[033m1.rsync install... \e[0m"

cat > /etc/rsyncd.conf < /dev/null
if [ $? -ne 0 ];then
        useradd -M -s /sbin/nolog rsync
fi

if [ ! -e /backup ];then
        mkdir /backup
fi
chown -R rsync:rsync /backup
echo -e "\e[033m3.backup dir finish... \e[0m"

echo "rsync_backup:123456" > /etc/rsync_passwd
chmod 600 /etc/rsync_passwd
echo -e "\e[033m4.rsync auth finish... \e[0m"

systemctl restart rsyncd
systemctl enable rsyncd &>/dev/null
lsof -i:873 &>/dev/null
if [ $? -eq 0 ];then
        echo -e "\e[033m5.rsync finish.....\e[0m"
fi

2.运行脚本

[root@ansible ~]# ansible backup -m script -a "/server/scripts/rsync.sh"

12.cron模块

语法格式

ansible all -m cron -a "name=任务名 minute=分 hour=时 day=日 month=月 weekday=星期 job=命令"
参数
name		//描述,必须要写,如果不写,默认为None。会导致无法指定删除某条计划任务

job			//执行的命令
state		//执行状态
	absent	//删除
	present	//创建(默认)
minute		//分钟(0-59,*,*/2,默认是*)
hour		//小时(1-23,*,*/2,默认是*)
day			//日(1-31,*,*/2.默认是*)
month		//月(1-12,*,*/2,默认是*)
weekday		//星期(0-6,*,*/2,默认是*)

案例1:在每天的21:30执行rsync备份脚本

1.准备脚本
[root@ansible ~]# ansible all -m file -a "path=/script state=directory"

2.将备份脚本上传至各主机
[root@ansible ~]# ansible all -m copy -a "src=/script/rsyncd_backup.sh dest=/script"

3.创建计划任务
[root@ansible ~]# ansible all -m cron -a "name='rsync backup' minute=30 hour=21 job='/bin/bash /script/rsyncd_backup.sh'"

案例2:在5月份的周一至周五每2小时的30分时执行rsync备份脚本

[root@ansible ~]# ansible all -m cron -a "name=rsyncbackup2 minute=30 hour=*/2 month=5 weekday=1,5 job='/bin/sh /script/rsync_backup2.sh'"

案例3:删除rsyncbackup2计划任务

[root@ansible ~]# ansible all -m cron -a "name=rsyncbackup2 state=absent"

13.综合案例-rsync,nfs,httpd,sersync

13.1.基础环境部署

1)关闭firewalld、selinux

每台服务器都要操作,也可以使用ansible的shell模块批量操作
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# systemctl disable firewalld

[root@ansible ~]# sed -ri '/SELINUX=/c SELINUX=disabled' /etc/sysconfig/selinux 
[root@ansible ~]# sed -ri '/SELINUX=/c SELINUX=disabled' /etc/selinux/config 

2)ssh密钥生成,及公钥推送

1.生成公钥文件
[root@ansible ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

2.将公钥推送至远程服务器
[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

3)镜像配置

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo 

4)安装软件包rsync、nfs-utils

[root@ansible ~]# ansible all -m yum -a "name=nfs-utils,rpcbind,rsync state=installed"

5)创建用户、组

组
[root@ansible ~]# ansible all -m group -a "name=linuxowner gid=9999"

用户
[root@ansible ~]# ansible all -m user -a "name=linuxowner uid=9999 group=9999 create_home=no shell=/sbin/nologin"

6)创建目录、并修改所属和权限

rsync备份
[root@ansible ~]# ansible all -m file -a "path=/backup owner=6666 group=6666 state=directory"
"

数据目录
[root@ansible ~]# ansible all -m file -a "path=/data owner=6666 group=6666 state=directory"

脚本目录
[root@ansible ~]# [root@ansible ~]# ansible all -m file -a "path=/script owner=6666 group=6666 state=directory"

7)创建rsync客户端密码文件

[root@ansible ~]# ansible all -m copy -a "content=123456 dest=/etc/rsync_pass mode=600" 

8)推送脚本

[root@ansible ~]# mkdir /server/script/ -p

准备rsync安装脚本
准备rsync备份脚本
root@ansible ~]# tree /server/script
/server/script
├── rsyncd_backup.sh
├── rsyncd_shjc_install.sh
└── rsyncd_sjjy2.sh


推送至客户端
1.备份脚本推送至每个客户端
[root@ansible ~]# ansible all -m copy -a "src=/server/script/rsyncd_backup.sh dest=/script"

2.对rsync执行安装脚本
[root@ansible ~]# ansible backup -m script -a "/server/script/rsyncd_shjc_install.sh"

4.推送数据校验脚本至rsync
[root@ansible ~]# ansible backup -m copy -a "src=/server/script/rsyncd_sjjy2.sh dest=/script"

9)计划任务

web服务器
[root@ansible ~]# ansible web -m cron -a "name=rsync_backup hour=1 minute=0 job='/bin/sh /script/rsyncd_backup.sh'"

nfs服务器
[root@ansible ~]# ansible nfs -m cron -a "name=rsync_backup hour=1 minute=0 job='/bin/sh /script/rsyncd_backup.sh'"


backup服务器
[root@ansible ~]# ansible backup -m cron -a "name=rsync_check hour=5 minute=0 job='/bin/sh /script/rsyncd_sjjy2.sh'"

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3ANTYqeS-1593935468984)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200610234617538.png)]

13.2.安装rsynnc

1)安装rsync

[root@ansible ~]# ansible backup -m yum -a "name=rsync state=installed"

2)配置rsync服务端

[root@ansible ~]# mkdir -p /server/conf
[root@ansible conf]# ansible backup -m copy -a "src=/server/conf/rsyncd.conf dest=/etc/"
[root@ansible conf]# ansible backup -m copy -a "content=rsync_backup:123456 dest=/etc/rsyncd.conf"
[root@ansible conf]# ansible backup -m file -a "path=/etc/rsync_passwd mode=666"

3)启动rsync

[root@ansible conf]# ansible backup -m service -a "name=rsyncd state=restarted"

13.3.安装NFS

1)安装配置nfs

[root@ansible conf]# ansible nfs -m yum -a "name=nfs-utils,rpcbind state=installed"
[root@ansible conf]# echo '/data2 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)' > /server/conf/exports 
[root@ansible conf]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/"

2)启动nfs

[root@ansible conf]# ansible nfs -m service -a "name=nfs state=started"

3)挂载

[root@ansible conf]# ansible all -m mount -a "src=192.168.81.230/data2 path=/data2 fstype=nfs state=mounted"

你可能感兴趣的:(ansible)