一、Ansible模块回顾
1.command模块
[root@m01 ~]# ansible web01 -m command -a 'free -m'
2.shell模块
#支持管道符这种特殊符号
[root@m01 ~]# ansible web01 -m shell -a 'ps -ef | grep httpd'
[root@m01 ~]# ansible web01 -m shell -a 'yum localinstall -y /package/*.rpm'
3.script模块
[root@m01 ~]# ansible web01 -m script -a '/root/mkdir.sh'
4.yum模块
#1.使用服务名字安装
[root@m01 ~]# ansible web01 -m yum -a 'name=httpd state=present'
#2.使用网上安装包的地址安装
[root@m01 ~]# ansible web01 -m yum -a 'name=http://.../.../.../httpd.rpm state=present'
#3.使用本地rpm包(包一定在受控端)
[root@m01 ~]# ansible web01 -m yum -a 'name=/package/http.rpm state=present'
yum
name:
httpd #包的名字
http://.../.../... #包在网上的地址
/package/http.rpm #包在受控端的地址
state
latest #安装最新的包
present #安装
absent #卸载
5.yum_repository模块
6.copy模块
#1.推送控制端文件到受控端
[root@m01 ~]# ansible web01 -m copy -a 'src=/root/mkdir.sh dest=/tmp/'
#2.推送文件并授权
[root@m01 ~]# ansible web01 -m copy -a 'src=/root/mkdir.sh dest=/tmp/ owner=nginx group=nginx mode=0644'
#3.推送文件并将原文件备份
[root@m01 ~]# ansible web01 -m copy -a 'src=/root/mkdir.sh dest=/tmp/ backup=yes'
#4.直接将内容写到受控端新文件
[root@m01 ~]# ansible web01 -m copy -a 'content="rsync_backup:123456" dest=/tmp/rsync_password owner=rsync group=rsync mode=600'
#注意:
1.授权时,用户必须是存在的系统用户
2.文件可以修改名字
3.推送文件至目录,目标地址最好在最后加 /
copy
src: #控制端的文件或目录
dest: #受控端的目录
owner: #文件推送后的属主
group: #文件推送后的属组
backup: #文件推送后,是否备份原文件
follow: #识别软链接
mode: #文件推送后的权限
content: #直接将内容写入文件
7.file模块
#1.创建目录
[root@m01 ~]# ansible web01 -m file -a 'path=/code/worpdress state=directory'
#2.创建文件
[root@m01 ~]# ansible web01 -m file -a 'path=/code/worpdress/index.html state=touch'
#3.创建文件或目录时授权
[root@m01 ~]# ansible web01 -m file -a 'path=/code/worpdress/index.php state=touch owner=nginx group=nginx mode=777'
#4.递归授权
[root@m01 ~]# ansible web01 -m file -a 'path=/code/ owner=nginx group=nginx mode=777 recurse=yes'
#注意:
1.创建文件时上层目录必须存在
2.授权时,直接授权上层目录加递归参数即可
file
src: #做软连接时的原目录
dest: #做软连接时的目标文件
path: #要在受控端创建的文件或目录
owner: #属主
group: #属组
mode: #权限
recurse: #递归授权
state:
directory #创建目录
touch #创建文件
link #软链接
absent #删除
8.get_url
#1.下载文件
[root@m01 ~]# ansible web01 -m get_url -a 'url=http://10.0.0.7/index.html dest=/tmp/'
#2.下载文件并授权
[root@m01 ~]# ansible web01 -m get_url -a 'url=http://10.0.0.7/index.html dest=/tmp/ owner=nginx group=nginx mode=777'
get_url:
url: #要下载的内容
dest: #下载到哪里,可以改名
mode: #下载包后的权限
checksum: #下载是进行验证
sha256:
md5:
owner: #下载包后的属主
group: #下载包后的属组
二、Ansible 模块 新
1.service模块
1)帮助语法
[root@m01 ~]# ansible-doc service
EXAMPLES:
- name: Start service httpd, if not started
service:
name: httpd
state: started
enabled: yes
name: httpd #服务的名字
state:
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled: #开机自启
yes
no
2)实例
#1.停止nginx服务
[root@m01 ~]# ansible web03 -m service -a 'name=nginx state=stopped'
#2.启动httpd服务,并加入开机自启
[root@m01 ~]# ansible web03 -m service -a 'name=httpd state=started enabled=yes'
2.systemd模块
1)帮助语法
[root@m01 ~]# ansible-doc systemd
EXAMPLES:
- name: Start service httpd, if not started
systemd:
name: httpd
state: started
enabled: yes
daemon_reload: yes
name: httpd #服务的名字
state:
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled: #开机自启
yes
no
daemon_reload: #后台启动
2)实例
#1.停止nginx服务
[root@m01 ~]# ansible web03 -m systemd -a 'name=nginx state=stopped'
#2.启动httpd服务,并加入开机自启
[root@m01 ~]# ansible web03 -m systemd -a 'name=httpd state=started enabled=yes'
3.group模块
1)帮助语法
EXAMPLES:
- name: Ensure group "somegroup" exists
group:
name: somegroup #组名字
state:
present #创建用户组
absent #删除用户组
gid: 666 #用户组ID
2)实例
#创建用户组
[root@m01 ~]# ansible web_group -m group -a 'name=www state=present gid=666'
#删除用户组
[root@m01 ~]# ansible web_group -m group -a 'name=www state=absent'
4.user模块
1)帮助语法
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
user:
name: johnd #用户名
comment: John Doe #用户的注释
uid: 1040 #用户id
group: admin #用户的组
groups: admins,developers #指定附加组
shell: /bin/bash #指定登录脚本
append: yes #添加附加组时使用
remove: yes #移除家目录
generate_ssh_key: yes #是否生成密钥对
ssh_key_bits: 2048 #秘钥加密的位数
ssh_key_file: .ssh/id_rsa #秘钥文件
expires: 1422403387 #用户的有效时间
state:
present #添加用户
absent #删除用户
create_home:yes/no #是否创建家目录
password #给用户添加密码(单引号)
2)实践
#1.创建用户
[root@m01 ~]# ansible web_group -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present'
#2.仅删除用户
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent'
#3.删除用户及用户组
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent remove=yes'
#注意:
1.如果用户名字跟组名字相同,删除用户是会将组也删除
2.当组下面有多个用户,删除的与组同名的用户也不会删除组
5.cron模块
1)帮助语法
EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /d
cron:
name: "check dirs" #定时任务的注释
minute: "0" #分钟
hour: "5,2" #小时
day: "2" #日
month: "2" #月
weekday: "2" #周
job: "ls -alh > /dev/null" #定时任务的内容
state:
absent #删除定时任务
present #添加定时任务
2)实践
#1.添加定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" minute=*/10 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#2.修改定时任务(不修改名字,只修改内容)
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#3.删除定时任务(一定要用name参数)
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" state=absent'
#4.注释定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null" disabled=yes'
6.磁盘挂载mount模块
1)帮助语法
EXAMPLES:
# Before 2.3, option 'name' was used instead of 'path'
- name: Mount DVD read-only
mount:
path: /mnt/dvd #挂载的目录(nfs客户端)
src: /dev/sr0 #远端被挂载的目录 (nfs服务端)
fstype: nfs #挂在类型
opts: ro,noauto #自动挂载的参数
state:
present #写入自动挂载,但实际没有挂咋,需要重启服务器
unmounted #取消临时挂载,但是没有清理自动挂载
mounted #写入自动挂载,并且直接挂载了(常用)
absent #取消临时挂载,并且清理自动挂载(常用)
2)准备挂载的服务端
1.安装nfs
[root@m01 ~]# ansible nfs -m yum -a 'name=nfs-utils state=present'
2.配置nfs
[root@m01 ~]# ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'
3.创建用户
[root@m01 ~]# ansible nfs -m group -a 'name=www gid=666'
[root@m01 ~]# ansible nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no'
4.创建目录并授权
[root@m01 ~]# ansible nfs -m file -a 'path=/data state=directory owner=www group=www'
5.启动
[root@m01 ~]# ansible nfs -m systemd -a 'name=nfs state=started'
3)客户端使用模块挂载
#挂载目录,并加入开机自动挂载
[root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=mounted'
#取消挂载,并取消开机自动挂载
[root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=absent'
7.selinux模块
1)帮助语法
EXAMPLES:
- name: Enable SELinux
selinux:
policy: targeted
state:
enforcing #开启
disabled #关闭
2)关闭selinux
[root@m01 ~]# ansible web01 -m selinux -a 'state=disabled'
8.firewalld模块
1)帮助语法
EXAMPLES:
- firewalld:
service: https #防火墙开启的服务
permanent:
yes #永久生效
no #临时生效
state:
enabled #开启
disable #关闭
port: 8081/tcp 161-162/udp #防火墙配置的端口
zone: dmz #指定配置空间
rich_rule: #富规则
source: 192.0.2.0/24 #防火墙配置的源ip
masquerade:
yes #开启ip伪装
no #关闭ip伪装
interface: eth2 #绑定网卡
2)防火墙配置实践
#1.允许访问http,永久生效
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
#2.允许80端口被访问,临时生效
[root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'
#3.允许10.0.0.0/24网段访问22端口
[root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 service name=ssh accept" state=enabled'
#4.允许10.0.0.0/24网段访问所有服务
[root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'
9.unarchive 解压模块
1)帮助语法
- name: Unarchive a file that is already on the remote machine
unarchive:
src: /tmp/foo.zip #要解压的包
dest: /usr/local/bin #解压到目标位置
remote_src:
yes #要解压的包在受控端
no #要解压的包在控制端
2)实例
#1.解压控制端的包到受控端
[root@m01 /package]# ansible web01 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/'
#2.解压受控端的包到受控端
[root@m01 /package]# ansible web03 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/ remote_src=yes'
10.archive 压缩模块
1)帮助语法
EXAMPLES:
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
archive:
path: /path/to/foo #要压缩的文件或目录
dest: /path/to/foo.tgz #压缩后的文件
format:bz2, gz, tar, xz, zip #指定打包的类型
2)实例
#1.打包站点目录
[root@m01 /package]# ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz'
11.Ansible主机信息模块 setup
为什么要讲这个模块?
这个模块非常实用,在公司中总会有一些需求
比如:
1.根据不同主机不同IP创建对应IP的目录
2.根据不同主机不同主机名创建对应主机名的目录
3.自动化运维平台需要自动获取到主机的IP地址,内存信息,磁盘信息,主机名...等
4.如果安装数据库,分配内存为物理内存的80%,此时有3台不同物理内存的机器2G、4G、16G
写一个playbook的情况下,我需要获取到对应主机的内存并作出计算,写判断。
1)使用
1.获取所有主机信息
[root@m01 ~]# ansible web01 -m setup
2.获取主机名(使用setup获取的信息,指定对应的小标题获取指定的信息)
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
2)常用的参数
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搭建作业平台
1.环境准备
主机名 | IP | 角色 |
---|---|---|
m01 | 10.0.0.61 | 跳板机 |
web01 | 172.16.1.7 | web服务器 |
web02 | 172.16.1.8 | web服务器 |
2.m01配置
1.上传相关源码包
[root@m01 ~]# rz
[root@m01 ~]# ll
-rw-r--r-- 1 root root 26995 Aug 13 16:42 kaoshi.zip
-rw-r--r-- 1 root root 19889622 Aug 26 09:04 php.tar.gz
2.编写脚本
[root@m01 ~]# vim zuoye.sh