Ansible

一、 ansible 服务管理模块

1.service
[root@m01 ~]# ansible-doc service
EXAMPLES:
- name: Start service httpd, if not started
service:
name: httpd
state: started

name: nginx #服务名字
state:
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled: yes
no #开机自启
2.案例
#关闭服务
[root@m01 ~]# ansible web01 -m service -a 'name=nginx state=stopped'

#启动服务
[root@m01 ~]# ansible web01 -m service-a 'name=nginx state=started'
2.systemd
2.1、语法和参数
[root@m01 ~]# ansible-doc systemd
EXAMPLES:
- name: Make sure a service is running
systemd:
state: started
name: httpd

name: nginx #服务名字
state:
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled: yes #开机自启
2.2.案例
#关闭服务
[root@m01 ~]# ansible web01 -m systemd -a 'name=nginx state=stopped'

#启动服务
[root@m01 ~]# ansible web01 -m systemd -a 'name=nginx state=started'
二、用户管理模块
1.group 模块
1.1、语法和参数
[root@m01 ~]# ansible-doc group
EXAMPLES:
- name: Ensure group "somegroup" exists
group:
name: somegroup #组的名字
state:
present #创建组
absent #删除组
gid: 666 #指定组的id
1.2、实践
#创建用户组
[root@m01 ~]# ansible web_group -m group -a 'name=www gid=666 state=present'

#删除用户组
[root@m01 ~]# ansible web_group -m group -a 'name=www state=absent'

#修改用户组
[root@m01 ~]# ansible web01 -m group -a 'name=www gid=666 state=absent'
2.user 模块
2.1语法和参数
[root@m01 ~]# ansible-doc user
EXAMPLES:
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
user:
name: johnd
coansibleent: John Doe
uid: 1040
group: admin
shell: /bin/bash
state: absent
remove: yes
create_home: false

name #用户名字
coansibleent #用户备注
uid #用户id
group #用户所在的组名字
shell
/bin/bash #用户可以登录
/sbin/nologin #用户不需要登录
state
absent #删除用户
present #创建用户
generate_ssh_key:yes #是否生成密钥
remove #移除家目录
create_home #是否创建家目录
true #创建家目录
false #不创建家目录
state:
present #创建用户
absent #删除用户
2.2 案例
#创建用户,不需要登录,有家目录
[root@m01 ~]# ansible web_group -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=true'

#删除用户并移除家目录
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent remove=yes'

#注意:
1.当组的名字与用户名字相同时,删除用户组也会被删除
2.当组的名字与用户名字相同时,而组下面还有其他用户,则删除用户时不会删除同名组
三、其他模块
1.cron 定时任务模块
1.1 语法和参数
[root@m01 ~]# ansible-doc cron
EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
cron:
name: "check dirs"
minute: "0"
hour: "5,2"
day: "*"
month: "*"
weekday: "*"
job: "ls -alh > /dev/null"
state: absent
disabled: yes

name #定时任务的注释
minute #分
hour #时
day #日
month #月
weekday #周
job #指定的定时任务内容
state
present #新建定时任务
absent #删除定时任务
disabled
yes #注释定时任务
no #取消注释
1.2实践
#添加定时任务,每十五分钟执行一次时间同步(只配置分钟,其他不配置默认是 * )
[root@m01 ~]# ansible web01 -m cron -a 'name="ansible时间同步" minute=*/15 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#查看配置
[root@web01 html]# crontab -l
#ansible: 测试ansible配置定时任务
*/15 * * * * /usr/sbin/ntpdate time1.aliyun.com

#修改定时任务,(只修改内容,不修改名字)
[root@m01 ~]# ansible web01 -m cron -a 'name="ansible时间同步" hour=*/3 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'

#注释定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="ansible时间同步" minute=*/15 job="ntpdate time1.aliyun.com" disabled=yes'

#删除定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="ansible时间同步" state=absent'

#注意:
1.定时任务添加,是通过名字来区分是否一样的,如果不加name参数,则会添加重复的定时任务
2.定时任务注释是通过 name 参数来注释的,所以一定要加 name 参数
3.定时任务删除是通过 name 参数来删除的,所以一定要加 name 参数
2.mount 挂载模块
1.安装NFS
#提前去/etc/ansible/hosts 配置
[nfs_group]
nfs ansible_ssh_pass='123'
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)" 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=true'

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'
2、挂载模块语法和参数
[root@m01 ~]# ansible-doc mount
EXAMPLES:
- name: Mount DVD read-only
mount:
path: /mnt/dvd
src: /dev/sr0
fstype: iso9660
opts: ro,noauto
state: present

path:/mnt/ #本机准备挂载的目录
src:/dev/sr0 #远端挂载目录
fstype:nfs #指定挂载类型
opts #自动挂载参数(/etc/fstab中的内容)
state
present #配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
unmounted #取消挂载,但是没有删除自动挂载配置
#常用配置
mounted #配置开机挂载,并且直接挂载上
absent #取消挂载,并且删除自动挂载配置
3实践
#配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
[root@m01 ~]# ansible web01 -m mount -a 'path=/mm/wordpress src=172.16.1.131:/data fstype=nfs opts=defaults state=present'

#配置开机挂载,并且直接挂载上
[root@m01 ~]# ansible web01 -m mount -a 'path=/mm/wordpress src=172.16.1.131:/data fstype=nfs opts=defaults state=mounted'

#取消挂载,但是没有删除自动挂载配置
[root@m01 ~]# ansible web01 -m mount -a 'path=/mm/wordpress src=172.16.1.131:/data fstype=nfs opts=defaults state=unmounted'

#取消挂载,并且删除自动挂载配置
[root@m01 ~]# ansible web01 -m mount -a 'path=/mm/wordpress src=172.16.1.131:/data fstype=nfs opts=defaults state=absent'
3.selinux模块
- name: Disable SELinux
selinux:
state: disabled #关闭
enforcing #关闭
#关闭selinux
[root@m01 ~]# ansible web_group -m selinux -a 'state=disabled'
4.firewalld 模块
1、语法和参数
[root@m01 ~]# ansible-doc firewalld
EXAMPLES:
- firewalld:
service: https #指定服务
permanent: #是否永久生效
yes #永久生效
no #临时生效
state:
enabled #开启防火墙
disable #关闭防火墙
port:
8081/tcp #指定端口
zone: dmz #指定区域
rich_rule: rule service name="ftp" audit limit value="1/m" accept #配置富规则
source: 192.0.2.0/24 #指定网段
interface: eth2 #帮定网卡
masquerade: yes #开启IP伪装
1.2实践
#允许访问http服务,永久生效
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http state=enabled'

#允许访问80端口,临时生效
[root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'

#配置允许192.168.15.0网段访问22端口
[root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.15.0/24 port port=22 protocol=tcp accept" state=enabled'

#配置网段白名单
[root@m01 ~]# ansible web01 -m firewalld -a 'source=192.168.15.0/24 zone=trusted state=enabled permanent=yes'
[root@m01 ~]# ansible web01 -m firewalld -a 'source=192.168.15.0/24 zone=trusted state=enabled permanent=no'
5.unarchive 解压模块
1、语法和参数
[root@m01 ~]# ansible-doc unarchive
EXAMPLES:
- name: Extract foo.tgz into /var/lib/foo
unarchive:
src: foo.tgz
dest: /var/lib/foo
remote_src: no #默认是no

src #包的路径
dest #解压后的目标路径
remote_src
yes #包在受控端服务器上
no #包在控制端服务器上
1.2实践
# 前提必须root目录有这个包
#解压包到受控端,包在控制端上
[root@m01 ~]# ansible web01 -m unarchive -a 'src=/root/php.tar.gz dest=/tmp'

#在受控端解压包,包在受控端
[root@m01 ~]# ansible web03 -m unarchive -a 'src=/tmp/php.tar.gz dest=/mm remote_src=yes'
6.archive 压缩模块
[root@m01 ~]# ansible-doc archive
EXAMPLES:
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
archive:
path: /path/to/foo #要打包的内容
dest: /path/to/foo.tgz #打好的包与存放位置
format:gz #打包的类型 bz2, gz, tar, xz, zip

#打包实践
[root@m01 ~]# ansible web01 -m archive -a 'path=/tmp dest=/opt/php.tar.gz'