ansible服务模块
1) file --- 修改远程主机数据属性信息
创建和删除远程主机数据信息
path --- 指定远程主机上已有的一个文件数据
mode --- 修改数据权限数值
owner --- 修改属主
group --- 修改属组
ansible 172.16.1.41 -m file -a "path=/backup/oldboy.txt mode=666 owner=oldboy group=oldboy" --- 修改文件权限
state --- directory touch link hard absent
ansible 172.16.1.41 -m file -a "path=/backup/oldboy state=directory" --- 创建目录
ansible 172.16.1.41 -m file -a "path=/backup/oldboy/oldboy01/oldboy02/ state=directory" --- 创建多级目录
ansible 172.16.1.41 -m file -a "path=/backup/oldboy.txt state=touch" --- 创建文件
ansible 172.16.1.41 -m file -a "src=/backup/oldboy.txt path=/backup/oldboy_soft_link.txt state=link" --- 创建软链接
ansible 172.16.1.41 -m file -a "src=/backup/oldboy.txt path=/backup/oldboy_hard_link.txt state=hard" --- 创建硬链接
ansible 172.16.1.41 -m file -a "path=/backup/oldboy_hard_link.txt state=absent" --- 删除数据操作
ansible 172.16.1.41 -m file -a "path=/backup/oldboy02.txt state=file" --- 检查文件是否存在
2) yum --- 批量安装软件模块
name --- 指定要安装的软件名称
state --- 安装软件 或是 卸载软件
present --- 安装软件
installed
latest --- 更新软件
removed --- 移除卸载软件
absent
ansible 172.16.1.41 -m yum -a "name=htop state=absent" --- 卸载软件
ansible 172.16.1.41 -m yum -a "name=htop state=installed" --- 安装软件
ansible 172.16.1.41 -m yum -a "name=htop state=removed" --- 卸载软件
3) service --- 批量管理服务启动状态
name --- 管理哪个服务名称
state --- 指定服务运行状态
reloaded
restarted
started
stopped
ansible 172.16.1.41 -m service -a "name=crond state=stopped"
ansible 172.16.1.41 -m service -a "name=crond state=started"
ansible 172.16.1.41 -m service -a "name=crond state=restarted"
enabled --- 设置服务是否开机运行
ansible 172.16.1.41 -m service -a "name=crond enabled=no" --- 开机不运行
ansible 172.16.1.41 -m service -a "name=crond enabled=yes" --- 开机自动运行服务
4) corn --- 批量部署定时任务
name --- 定义定时任务注释信息
state --- absent删除定时任务
disabled --- 给定时任务添加注释
crontab -e
* * * * * 定时任务信息 &>/dev/null
分 时 日 月 周
minute hour day month weekday job="ntpdate ntp1.aliyun.com"
ansible定时任务设置命令
01. 要求: 每隔五分钟,进行时间同步
ansible 172.16.1.41 -m cron -a "minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null'"
02. 要求: 每周五,晚上10:30, 需要去大保健
ansible 172.16.1.41 -m cron -a "name='大保健' minute=30 hour=22 weekday=5 job='make shufu &>/dev/null'"
说明: 定时任务配置时.需要添加name注释信息,否则会出现反复创建相同定时任务
ansible 172.16.1.41 -m cron -a "name='大保健03' state=absent" --- 删除指定定时任务
ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=yes" --- 给定时任务添加注释
ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=no" --- 取消定时任务注释
==================================================
补充: ansible帮助信息查看方法
01. 查看ansible所有模块
ansible-doc -l
02. 查看ansible模块参数信息
ansible-doc -s cron
03. 查看ansible模块详细信息
ansible-doc cron
==================================================
5) user --- 批量创建创建用户
name --- 创建的用户名称
password --- 设置用户密码信息 必须设置为密文
create_home --- yes 表示创建家目录 no 不创建家目录
shell --- 指定用户登录方式 shell=/sbin/nologin
group --- 指定用户属于哪个组 主要组
groups --- 指定用户属于哪个组 附属组
uid --- 指定用户uid数值
state --- absent删除用户
ansible 172.16.1.41 -m user -a "name=alex create_home=no shell=/sbin/nologin" --- 批量创建虚拟用户
ansible 172.16.1.41 -m user -a "name=alex group=oldboy groups=oldgirl" --- 指定用户所属组
ansible 172.16.1.41 -m user -a "name=alex uid=2000" --- 指定用户uid
ansible 172.16.1.41 -m user -a "name=alex state=absent" --- 删除用户
ansible 172.16.1.41 -m user -a 'name=oldbaby password="$6$oldgirl$kAUTXVC2z1agr1HlmpFe9abFhWKwJ1fNyg64F95U3rVumwQfqOuhV3YkyZU9.H79TChzIKn5epl5M18B199qV1"'
--- 给用户设置密码
===========================================
ps: 密码密文生成方式:
https://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module
方法一:
ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"
mypassword --- 明文密码信息
sha512 --- 明文转换为密文加密方法
mysecretsalt --- 用什么做算法依据生成密文信息
ansible all -i localhost, -m debug -a "msg={{ 'oldboy123' | password_hash('sha512', 'oldgirl') }}
$6$oldgirl$kAUTXVC2z1agr1HlmpFe9abFhWKwJ1fNyg64F95U3rVumwQfqOuhV3YkyZU9.H79TChzIKn5epl5M18B199qV1
实践操作:
[root@m01 ~]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy123') }}"
localhost | SUCCESS => {
"msg": "$6$oldboy123$W3jkmkkVTr.9UStm4S50RT2uIEjB/4GEtaAeVCSZ..uWVN1YGxHvluss9JVfAPV0gSJoGn1qAfxGyttIsTjcz0"
}
方法二: 在centos7中无法使用
mkpasswd --method=sha-512
方法三: 利用python模块功能
yum install python-pip
pip install passlib
优化pip源
~/.pip/pip.conf
中添加或修改:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
=================================================================================================
6) mount --- 批量挂载模块
src --- 需要挂载的存储设备
path --- 挂载点信息
fstype --- 挂载文件系统类型 nfs
state --- 挂载和卸载设置参数
mounted : 实现立即挂载 和 开机自动挂载 (推荐)
present : 只能实现开机自动挂载
unmounted : 立即卸载 但是 不会永久卸载
absent : 立即卸载 永久卸载 错误提示
mount -t nfs 172.16.1.31:/data /mnt
ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted"
ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=unmounted"
7) fetch --- 远程拉取数据信息
ansible 172.16.1.41 -m fetch -a "src=/etc/hosts dest=/tmp"
-
利用管理端ansible程序部署rsync
第一个历程: 检查环境
第二个历程: 配置ansible主机清单
vim /etc/ansible/hosts
[rsync:children]
rsync_server
rsync_client
[rsync_server]
172.16.1.41 ansible_user=root ansible_password=123456
[rsync_client]
172.16.1.31
172.16.1.7 ansible_user=root ansible_password=654321 ansible_port=52113
[root@m01 172.16.1.41]# ansible rsync -m ping
172.16.1.31 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.16.1.41 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.16.1.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}第三个历程: 利用模块功能部署rsync服务
服务端配置:- 安装软件程序
ansible rsync -m yum -a "name=rsync state=installed" - 编写配置文件
在管理端准备好服务配置文件
ansible rsync_server -m copy -a "src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/" - 创建虚拟用户
ansible rsync_server -m user -a "name=rsync create_home=no shell=/sbin/nologin" - 创建密码文件 (授权600)
ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600" - 创建备份目录 (授权 属主 属组)
ansible rsync_server -m file -a "path=/backup state=directory owner=rsync group=rsync" - 启动程序服务
ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"
客户端配置:
- 创建密钥文件 (授权600)
ansible rsync_client -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600" - 批量测试传输文件
ansible rsync_client -m shell -a "rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password"
PS: 使批量管理操作下载软件速度提升
部署yum本地仓库掌握剧本编写
将多个模块操作命令进行整合
模块 === 命令
剧本 === shell脚本作用说明:
- 可以实现服务自动部署
- 可以提高运维工作效率
- 可以减少运维工作问题
- 可以节省公司运维成本
剧本编写组成:
- 剧本角色信息: hosts --主机信息
- 剧本任务信息: tasks --任务信息
剧本编写规范: 遵循yaml语法规范 == python代码语法规范
缩进规范: 两个空格表示一个缩进关系
标题一
标题二
标题三
PS: 在缩进时只能用空格表示缩进,不能用tab键代替缩进字典规范: 冒号后面要有空格
oldboy: 123456 key: value
PS: 冒号结尾时不需要有空格/冒号信息出现在注释说明中不需要有空格-
列表规范: 横线后面要有空格
name:- rsync
- nfs
- rpcbind
- 安装软件程序
在剧本中设置变量信息
设置变量方法一: 在剧本中设置变量
- hosts: 172.16.1.41
vars:
data_dir: /oldboy
tasks:
- name: copy file 01
copy: src={{ data_dir }}/oldboy01.txt dest=/tmp
- name: copy file 02
copy: src={{ data_dir }}/oldboy02.txt dest=/opt
- name: copy file 03
copy: src={{ data_dir }}/oldboy03.txt dest=/usr/local/
设置变量方法二: 在命令中设置变量
ansible-playbook -e data_dir=/oldboy test_变量配置功能.yml
设置变量方法三: 在主机清单中设置
a: 单个主机设置变量
172.16.1.41 data_dir=/oldboy
b: 多个主机设置变量
[oldboy]
172.16.1.41
[oldboy:vars]
data_dir=/oldboy