admin管理员组

文章数量:1530873

Ansible的常用模块

文章目录

  • Ansible的常用模块
    • Ansible的执行
      • Ansible ad-hoc
      • ad-hoc使用场景
      • ad-hoc的命令模式(语法)
      • ad-hoc返回结构的颜色
      • ad-hoc常用模块
      • Ansible的帮助手册
    • Ansible命令模块
      • command
      • shell
      • script
    • Ansible软件管理模块
      • yum
      • yum-repository
    • mysql
    • Ansible文件管理模块
      • copy
      • file
    • Ansible文件管理模块
      • get_url
    • Ansible服务模块
      • service
    • Ansible用户管理模块
      • user
      • group
    • Ansible定时任务模块
      • cron
    • Ansible磁盘挂载模块
      • mont
    • Ansible防火墙模块
      • selinux
      • firewalld
    • Ansible解压模块
      • unarchive
    • Ansible主机信息模块
      • setup
    • ansible firewalld 模块
    • ad-hoc实战安装rsync
    • 作业一件搭建
      • 环境
      • m01准备工作
      • 编辑ad-hoc

Ansible的执行

  • ad-hoc
  • playbook

Ansible ad-hoc

ad-hoc:临时的命令,执行后结束,不会保存

ad-hoc使用场景

比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等

ad-hoc的命令模式(语法)

语法:
ansible 主机名(主机清单中的主机或者主机组) -m 模块名 [-a 动作]

ad-hoc返回结构的颜色

  • 绿色:被管理端执行成功,并且结果不会发送改变
  • 黄色:被管理端主机执行成功,但是结果时变化的
  • 红色:执行失败,注意看报错

ad-hoc常用模块

command             # 执行shell命令(不支持管道等特殊字符)
shell               # 执行shell命令
scripts             # 执行shell脚本
yum_repository      # 配置yum仓库
yum                 # 安装软件
copy                # 变更配置文件
file                # 建立目录或文件
service             # 启动与停止服务
mount               # 挂载设备
cron                # 定时任务
get_url             #下载软件
firewalld           #防火墙
selinux             #selinux

Ansible的帮助手册

ansible-doc 模块名 (查看帮助手册中模块)

[root@m01 ~]# ansible-doc -l        # 查看所有模块说明
[root@m01 ~]# ansible-doc copy      # 查看指定模块方法
[root@m01 ~]# ansible-doc -s copy   # 查看指定模块参数

Ansible命令模块

command

ansible web_group -m command -a '命令(不带特殊符号)'

shell

ansible web_group -m shell -a '命令(带特殊符号)'

script

# 编写脚本
[root@m01 ~]# vim /root/yum.sh
#!/usr/bin/bash
yum install -y vsftpd
 
#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@m01 ~]# ansible web_group -m script -a "/root/yum.sh"

Ansible软件管理模块

yum

[root@m01 ~]# ansible web_group -m yum -a "name=httpd state=present"

- name:包名
	- 直接指定包名:从仓库安装		 yum install httpd
	- http://		从指定URL安装  yum install http://mirrors.aliyun/epel/http-1.1.rpm
	- file://		从本地安装 类似  yum localinstall /root/php-1.1.rpm
- state
	- absent:卸载
	- present:安装
	- lastest:安装最新的
	
- download_only:
	- true:只下载不安装
	- false:下载并安装

- list
	- 包名
	"yumstate": "available" 证明包可以安装但是没装
	"yumstate": "installed" 已经安装了
	
[root@m01 ~]# ansible-doc yum
exclude=kernel*,foo*            #排除某些包
list=ansible                    #类似于yum list查看是否可以安装
disablerepo="epel,ol7_latest"   #禁用指定的yum仓库
download_only=true              #只下载不安装 yum install d

yum-repository

## 创建新的仓库配置文件
ansible db01 -m yum_repository -a 'name=local description=xxx baseurl=file:///mntgpgcheck=no enabled=yes'

## 创建新的仓库配置,并且文件名和仓库名不同
ansible web01 -m yum_repository -a 'name=local file=zls_local description=xxxbaseurl=file:///mnt gpgcheck=no enabled=yes'
## 追加仓库
ansible lb01 -m yum_repository -a 'name=test2_ansible file=test_ansibledescription=xxx baseurl=http://www.baidu'

##删除仓库
ansible lb01 -m yum_repository -a 'name=test2_ansible file=test_ansible state=absent'

yum_repository
	- name:仓库名字(日过有flie,只是仓库名,如果没有file,文件名和仓库名)
	- file:指定仓库的文件名
	- description:仓库的描述(name)
	- baseurl:仓库的url
	- gpgcheck:指定检查密钥
		- no:不开启 0
		- yes:开启 1(默认)
	- enabked:是否启用仓库
		- no:不开启 0
		- yes:开启 1 (默认)

mysql

## mysql_user 模块
name:创建用户的名字
password:用户的密码
priv:'*.*:ALL'
host:指定可连接的主机
login_user:连接数据库的用户
login_password:连接数据库的密码
host_all:
	yes:类似于 % ,允许所有主机连接
	no:默认,不允许所有主机连接
state:
	present:创建
	absent:删除
	
## mysql_db 模块
name:数据库名字
state:
	import:导入数据
	dump:导出数据
	present:创建数据库
	absent:删除数据库
target:指定导入数据的sql文件
encoding:指定字符集

Ansible文件管理模块

copy

## 远程推送文件
ansible nginx -m copy -a 'src=/etc/passwd dest=/root'

copy
	- src:指定源文件的路径
	- dest:指定目标路径
	- owner:指定属主
	- group:指定属组
	- mode:指定权限
	- backup:
		- yes:如果目标路径,存在同名文件,就将目标文件备份
		- no:不备份直接覆盖(默认)
	- content:将指定文本内容覆盖到目标文件中
	- remote_src:将命令变成cp
		- yes:将源文件,编程远端的源文件(ansible被管理端)
		- no:源文件还是本地文件(ansible管理端 默认)
	- follow:拷贝软连接
		- yes:会将软连接一起拷贝
		- no:会生成一个新的软连接文件

file

path:要创建的文件路径
src:连接的源文件(软链接、硬链接)
dest:连接的目标文件(软链接、硬链接)
state:
	link:软链接
	hard:硬链接
	touch:将path当成普通文件创建出来
	director:将path当成目录创建出来
	absent:删除指定的文件
	file:修改path指定的文件属性
recurse:
	yes:递归修改所有目录下的所有文件权限
	no:默认
owner:属主
group:属组
mode:权限

#1.创建普通文件
[root@m01 ~]# ansible all -m file -a 'path=/root/test_ansible.txt state=touch'

#2.创建目录
[root@m01 ~]# ansible all -m file -a 'path=/root/test_dir state=directory'

#3.递归创建目录(默认支持)
[root@m01 ~]# ansible all -m file -a 'path=/root/test_dir state=directory'

#4.recurse 新创建的目录都会修改指定权限,目录中已存在的文件不会被修改
	yes:递归修改所有目录下的所有文件权限
	no:不改(no)
[root@m01 ~]# ansible web01 -m file -a 'path=/root/1 state=directory owner=www group=www recurse=yes'

#5.删除目录
[root@m01 ~]# ansible web01 -m file -a 'path=/root/1 state=absent'

#6.软连接
[root@m01 ~]# ansible web01 -m file -a 'src=/app/nginx-1.20.1 dest=/app/nginxstate=link'

Ansible文件管理模块

get_url

url:指定安装包下载url
dest:指定保存的路径
mode:下载下来之后的权限

[root@m01 ~]# ansible web01 -m get_url -a'url=http://test.driverzeng/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz dest=/tmpowner=www group=www mode=0755'

Ansible服务模块

service

name:服务名
state:
	started:启动
	stopped:停止
	restarted:重新加载文件
	reloaded:重新加载配置文件
enabled:
	yes:允许开机自启
	no:默认

#启动crond并加入开机自启
[root@m01 ~]# ansible web_group -m service -a "name=crond state=started enabled=yes"
 
#停止crond并删除开机自启
[root@m01 ~]# ansible web_group -m service -a "name=crond state=stoped enabled=no"

Ansible用户管理模块

user

name:用户名
uid:指定uid -u
group:指定gid或者组名 -g
groups:指定附加组 -G
append:追加,配合-G使用 -a
shell:指定登录的shell -s
comment:指定描述 -c
create_home:是否创建家目录
	yes:创建(默认)
	no:不创建
generate_ssh_key:是否创建秘钥对
	yes:创建
	no:不创建(默认)
ssh_key_bits:私钥长度
ssh_key_file:私钥的位置
state:
	present:创建用户(默认)
	absent:删除用户
remove
	yes:将用户的家目录等。。。全部删除 -r
	no:默认

# 创建zls用户,并生成公钥和私钥
[root@m01 ~]# ansible web01 -m user -a 'name=zls uid=69 generate_ssh_key=yesssh_key_bits=2048 ssh_key_file=.ssh/id_rsa'

# 创建mysql 禁止登陆, 不创建家目录
[root@m01 ~]# ansible web01 -m user -a 'name=mysql shell=/sbin/nologin create_home=no'

group

name:指定组名
gid:指定gid
state:
	pernsent:创建组(默认)
	absent:删除组

[root@m01 ~]# ansible all -m group -a 'name=suibian gid=333 state=present'

Ansible定时任务模块

cron

name:注释
分:minute
时:hour
日:day
月:month
周:weekday
job:要执行的任务
state:
	present:创建定时任务
	absent:删除定时任务(删除任务要根据name)
user:指定定时任务的用户

# 使用ansible添加一条定时任务
[root@m01 ~]# ansible web_group -m cron -a "minute=* hour=* day=* month=* weekday=*  job='/bin/sh /server/scripts/test.sh'"
[root@m01 ~]# ansible web_group -m cron -a "job='/bin/sh /server/scripts/test.sh'"
 
# 设置定时任务注释信息,防止重复,name设定
[root@m01 ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"
 
# 删除相应定时任务
[root@m01 ~]# ansible web_group -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"
 
# 注释相应定时任务,使定时任务失效
[root@m01 scripts]# ansible web_group -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"


[root@m01 ~]# ansible all -m cron -a "name='时间同步' minute=*/5 job='/sbin/ntpdatetime1.aliyun &>/dev/null'"

Ansible磁盘挂载模块

mont

path:指定挂载路径
src:被挂载的路径
fstype:指定文件系统类型
state:
	prsent:开机挂载,只写入/etc/fstab
	[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploadssrc=172.16.1.31:/data/wp_data fstype=nfs state=present'
	absent:卸载设备,会清理/etc/fstab写入的配置
	[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploadsstate=absent'
	mounted:既挂载又写入/etc/fstab文件中
	[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploadssrc=172.16.1.31:/data/wp_data fstype=nfs state=mounted'
	unmountde:卸载设备,不会清除/etc/fstab写入的配置             

opts:指定挂载的权限

[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploadsstate=absent'

Ansible防火墙模块

selinux

state:
	disabled:关闭selinux
	
#修改配置文件关闭selinux,必须重启
[root@m01 ~]# ansible web01 -m selinux -a 'state=disabled'

firewalld

service:根据服务放行端口
port:
	端口/tcp
state 
	disabled:禁用端口
	enabled:放行指定端口

[root@m01 ~]# ansible lb02 -m firewalld -a 'port=7001/tcp state=enabled'
[root@m01 ~]# ansible lb02 -m firewalld -a 'service=http state=enabled'

Ansible解压模块

unarchive

src:指定源文件
dest:目标路径
remote_src:
	yes:压缩包在远端机器上
	no:默认 ,压缩包在管理端
注意:前提条件时,被管理端上必须有所有的解压命令
[root@m01 tmp]# ansible lb_group -m unarchive -a 'src=/tmp/pymp-dmy45m_h.zipdest=/usr/local/src'

Ansible主机信息模块

setup

# 查看所有详细信息
[root@m01 ~]# ansible web01 -m setup
# 获取IP地址
[root@m01 tmp]# ansible all -m setup -a 'filter=ansible_all_ipv4_addresses'
# 获取主机名
[root@m01 tmp]# ansible all -m setup -a 'filter=ansible_fdn'
# 获取内存信息
[root@m01 tmp]# ansible all -m setup -a 'filter=ansible_memory_mb'
# 获取磁盘信息
[root@m01 tmp]# ansible all -m setup -a 'filter=ansible_devices'

ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)

ansible firewalld 模块

firewalld
service: 添加服务
permanent:是否添加永久生效
  yes:永久生效
  no:默认 临时生效
state:开启或者关闭
  enabled
  disabled
port: 指定开放8081/tcp
zone: 指定某个区域名字
rich_rule: 富规则
source:来源
interface: 绑定的接口 网卡
masquerade:ip伪装
immediate:临时生效
  yes:立即生效
  no:默认

ad-hoc实战安装rsync

## 步骤回顾
# 0.创建基础用户
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nologin -M

# 1.安装rsync
yum install -y rsync

# 2.修改配置文件
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log

[backup]
comment = Backup to Rsync
path = /backup

# 3.创建目录
mkdir /backup

# 4.修改权限
chown www.www /backup

# 5.创建密码文件
echo 'rsync_backup:123' > /etc/rsync.passwd

# 6.修改密码文件权限为600
chmod 600 /etc/rsync.passwd

# 7.启动服务
systemctl start rsyncd

# 8.加入开机自启
systemctl enable rsyncd

使用Ansible

# 0.创建基础用户
ansible backup -m group -a 'name=www gid=666 state=present'
ansible backup -m user -a 'name=www uid=666 group=666 shell=/sbin/nologincreate_home=no state=present'

# 1.安装rsync
ansible bakcup -m yum -a 'name=rsync state=present'

# 2.修改配置文件
ansible backup -m copy -a 'src=/root/rsync.moban dest=/etc/rsyncd.conf owner=root group=root mode=0644'

uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
comment = Backup to Rsync
path = /backup

# 3.创建目录
ansible backup -m file -a 'path=/backup owner=www group=www mode=0755 state=directory'

# 4.创建密码文件
ansible backup -m copy -a 'content="rsync_backup:123" dest=/etc/rsync.passwd owner=root group=root mode=0600'

# 5.启动服务
ansible backup -m service -a 'name=rsyncd state=started enabled=yes'

作业一件搭建

环境

主机名外网IP内网IP角色
m0110.0.0.61172.16.1.61ansible管理端
backup10.0.0.41172.16.1.41ansible被管理端、rsync服务端、nfs服务端
nfs10.0.0.31172.16.1.31ansible被管理端、rsync客户端、nfs服务端
web0110.0.0.7172.16.1.7ansible被管理端、部署提交作业代码,挂载上传目录即可
web0210.0.0.8172.16.1.8ansible被管理端、部署提交作业代码,挂载上传目录即可web01

m01准备工作

# 安装epel源
[root@m01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun/repo/epel-7.repo
#安装
yum install -y ansible

#修改ansible配置文件 打开以下内容(去掉注释)
[root@m01 ~]# vim /etc/ansible/ansible.cfg
host_key_checking = False  	#跳过检查主机指纹(需要开启)
log_path = /var/log/ansible.log   #ansible日志(需要开启)

# 在 ansible管理端生成密钥对
[root@m01 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Fmp4UsqUG/qI5K/Ib8F1L9ZoTxBuejt3op0Y1soKo/8 root@m01
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|     . .         |
|    + o o        |
|   + B * .       |
| .o B B S        |
|o. = = B.+       |
|...oo +o=.       |
|o o.o oo=+..     |
|.+=+.E.=+oo      |
+----[SHA256]-----+

# 下发公钥
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41

#修改主机清单(原内容可以全删)加入汇总的组
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8

[nfs_group]
nfs ansible_ssh_host=172.16.1.31

[backup_group]
backup ansible_ssh_host=172.16.1.41

[rsync_install_group:children]
backup_group
nfs_group



#编写rsync配置文件
vim /root/rsync.moban

uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log

[backup]
comment = Backup to Rsync
path = /backup

# httpd配置文件
vim /root/httpd.conf
User www
Group www

#代码编辑再次打包
mkdir app
 cd /root/app 放 kaoshi.zip 解压 修改文件
vim upload_file.php
$wen="/var/www/html/uploads";
打包

编辑ad-hoc

[root@m01 ~]# vim ansible_web.sh
#!/bin/bash
# 关闭防火墙,关闭selinux,创建用户,创建组
ansible all -m selinux -a 'state=disabled'
ansible all -m service -a 'name=firewalld state=stopped'
ansible all -m group -a 'name=www gid=666 state=present'
ansible all -m user -a 'name=www uid=666 group=666 shell=/sbin/nologincreate_home=no state=present'

# 安装rsync
ansible rsync_install_group -m yum -a 'name=rsync state=present'

# 3.推送配置文件
ansible backup -m copy -a 'src=/root/rsync.moban dest=/etc/rsyncd.conf owner=root group=root mode=0644'

# 4.创建目录
ansible backup -m file -a 'path=/backup owner=www group=www mode=0755 state=directory'

# 5.创建密码文件
ansible backup -m copy -a 'content="rsync_backup:123" dest=/etc/rsync.passwdowner=root group=root mode=0600'

# 6.启动服务
ansible backup -m service -a 'name=rsyncd state=started enabled=yes'

# 7.安装nfs
ansible nfs_install_group -m yum -a 'name=nfs-utils state=present'

# 8.编辑配置文件
ansible rsync_install_group -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'

# 9.创建共享目录
ansible rsync_install_group -m file -a 'path=/data owner=www group=www mode=0755 state=directory'

# 10.启动nfs
ansible rsync_install_group -m service -a 'name=nfs-server state=started enabled=yes'

# 11.在nfs上创建rsync的密码文件
ansible nfs -m copy -a 'content=123 dest=/etc/rsync.passwd owner=root group=root mode=0600'

# 12.部署web
ansible web_group -m yum -a 'name=httpd,php state=present'

# 13.推送配置文件
ansible web_group -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf'

# 14.部署代码
ansible web_group -m unarchive -a 'src=/root/kaoshi.tgz dest=/var/www/html'

# 15.创建上传目录
ansible web_group -m file -a 'path=/var/www/html/uploads owner=www group=www mode=0755 state=directory'

# 16.启动httpd服务
ansible web_group -m service -a 'name=httpd state=started enabled=yes'

# 17.挂载上传目录到nfs
ansible web_group -m mount -a 'path=/var/www/html/uploads src=172.16.1.31:/data fstype=nfs state=mounted'

本文标签: ansiblehoc