目录
一、ansible服务特点说明
二、ansible模块说明:
1)yum模块
2)service模块: 管理服务器的运行状态 停止 开启 重启
3)cron模块: 批量设置多个主机的定时任务信息
4)mount模块: 批量进行挂载操作
5)user模块: 实现批量创建用户
三、 剧本的编写方法
1)服务端的操作
2)客户端的操作
四、剧本编写规范: pyyaml -- 三点要求
1)合理的信息缩进 两个空格表示一个缩进关系
2)冒号的使用方法
3)短横线应用 -(列表功能)
一、ansible服务特点说明
01. 管理端不需要启动服务程序(no server)
02. 管理端不需要编写配置文件(/etc/ansible/ansible.cfg)
03. 受控端不需要安装软件程序(libselinux-python)
被管理端selinux服务没有关闭 --- 影响ansible软件的管理
libselinux-python让selinux开启的状态也可以使用ansible程序
04. 受控端不需要启动服务程序(no agent)
05. 服务程序管理操作模块众多(module)
06. 利用剧本编写来实现自动化(playbook)
二、ansible模块说明:
1)yum模块
name --- 指定安装软件名称
state --- 指定是否安装软件
installed --- 安装软件
present
latest
absent --- 卸载软件
removed
ansible 172.16.1.31 -m yum -a "name=iotop state=installed"
2)service模块: 管理服务器的运行状态 停止 开启 重启
name: --- 指定管理的服务名称
state: --- 指定服务状态
started 启动
restarted 重启
stopped 停止
enabled: --- 指定服务是否开机自启动
ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"
3)cron模块: 批量设置多个主机的定时任务信息
crontab -e
* * * * * 定时任务动作
分 时 日 月 周
minute: # Minute when the job should run ( 0-59, *, */2, etc )
设置分钟信息
hour: # Hour when the job should run ( 0-23, *, */2, etc )
设置小时信息
day: # Day of the month the job should run ( 1-31, *, */2, etc )
设置日期信息
month: # Month of the year the job should run ( 1-12, *, */2, etc )
设置月份信息
weekday: # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
设置周信息
job 用于定义定时任务需要干的事情
基本用法:
PS:job内容应用 ' '
ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
扩展用法:
01. 给定时任务设置注释信息
ansible 172.16.1.31 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
02. 如何删除指定定时任务
ansible 172.16.1.31 -m cron -a "name='time sync01' state=absent"
PS: ansible可以删除的定时任务,只能是ansible设置好的定时任务
03. 如何批量注释定时任务
ansible 172.16.1.31 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"
4)mount模块: 批量进行挂载操作
src: 需要挂载的存储设备或文件信息
path: 指定目标挂载点目录
fstype: 指定挂载时的文件系统类型
state:
present/mounted --- 进行挂载
present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载
mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载 *****
[root@m01 ~]# ansible 172.16.1.7 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted"
absent/unmounted --- 进行卸载
absent: 会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载
unmounted: 会实现立即卸载, 但是不会会删除fstab文件信息 *****
[root@m01 ~]# ansible 172.16.1.7 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=unmounted"
5)user模块: 实现批量创建用户
基本用法:
ansible 172.16.1.31 -m user -a "name=oldboy01"
扩展用法:
1) 指定用户uid信息
ansible 172.16.1.31 -m user -a "name=oldboy02 uid=6666"
2) 指定用户组信息
ansible 172.16.1.31 -m user -a "name=oldboy03 group=oldboy02"
ansible 172.16.1.31 -m user -a "name=oldboy04 groups=oldboy02"
3) 批量创建虚拟用户
ansible 172.16.1.31 -m user -a "name=rsync create_home=no shell=/sbin/nologin"
4) 给指定用户创建密码
PS: 利用ansible程序user模块设置用户密码信息,需要将密码明文信息转换为密文信息进行设置
生成密文密码信息方法:
方法一:
ansible all -i localhost, -m debug -a "msg={{ '密码信息123456' | password_hash('sha512', 'oldboy') }}"
[root@m01 tmp]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy') }}"
localhost | SUCCESS => {
"msg":"$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1"
}
方法二:(忽略)
mkpasswd --method=sha-512
方法三:
yum install -y python-pip
pip install passlib
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password: $6$rJJeiIerQ8p2eR82$uE2701X7vY44voF4j4tIQuUawmTNHEZhs26nKOL0z39LWyvIvZrHPM52Ivu9FgExlTFgz1VTOCSG7KhxJ9Tqk.
ansible 172.16.1.31 -m user -a 'name=oldboy08 password=$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1'
三、 剧本的编写方法
剧本的作用: 可以一键化完成多个任务
自动化部署rsync服务:
1)服务端的操作
第一个历程安装软件:
ansible 172.16.1.41 -m yum -a "name=rsync state=installed"
第二个历程编写文件:
ansible 172.16.1.41 -m copy -a "src=/xxx/rsyncd.conf dest=/etc/"
第三个历程创建用户
ansible 172.16.1.41 -m user -a "name=rsync create_home=no shell=/sbin/nologin"
第四个历程创建目录
ansible 172.16.1.41 -m file -a "dest=/backup state=directory owner=rsync group=rsync"
第五个历程创建密码文件
ansible 172.16.1.41 -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"
第六个历程启动服务
ansible 172.16.1.41 -m service -a "name=rsyncd state=started enabled=yes"
2)客户端的操作
第一个历程: 创建密码文件
ansible 客户端地址 -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"
剧本的做成部分:
演员信息: 男一号 hosts
干的事情: 吻戏 tasks
演员信息: 男二号
干的事情: 看着
四、剧本编写规范: pyyaml -- 三点要求
1. 合理的信息缩进 两个空格表示一个缩进关系
标题一
标题二
标题三
PS: 在ansible中一定不能用tab进行缩进
2. 冒号的使用方法
hosts: 172.16.1.41
tasks:
yum: name=xx
PS: 使用冒号时后面要有空格信息
以冒号结尾,冒号信息出现在注释说明中,后面不需要加上空格
3. 短横线应用 -(列表功能)
- 张三
男
- 打游戏
- 运动
- 李四
女
学习
湖南
- 王五
男
运动
深圳
PS: 使用短横线构成列表信息,短横线后面需要有空格
开始编写剧本
mkdir /etc/ansible/ansible-playbook
vim rsync_server.ymal
说明: 剧本文件扩展名尽量写为yaml
1. 方便识别文件是一个剧本文件
2. 文件编写时会有颜色提示
- hosts: 172.16.1.41
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file
copy: src=/tmp/rsyncd.conf dest=/etc/
如何执行剧本:
第一个步骤: 检查剧本的语法格式
ansible-playbook --syntax-check
rsync_server.yaml
第二个步骤: 模拟执行剧本
ansible-playbook -C rsync_server.yaml
第三个步骤: 直接执行剧本
ansible-playbook rsync_server.yaml