第五个模块:file
模块说明:修改已有文件属性信息(属主 属组 权限)
ansible oldboy -m file -a "path=/bbb/abc.txt mode=600 owner=oldboy group=oldboy"
ansible oldboy -m file -a "path=/oldboy/ mode=600 owner=oldboy group=oldboy"
问题: 如何实现批量递归修改目录属性信息 chmod -R / chown -R 用于创建新的数据信息/删除数据信息
创建普通文件: ansible oldboy -m file -a "path=/oldboy/alex100.txt state=touch
创建目录文件: ansible oldboy -m file -a "path=/oldboy/alex_dir state=directory"
创建链接文件: (软连接) ansible oldboy -m file -a "src=/oldboy/alex100.txt path=/oldboy/alex_soft.txt state=link"
创建链接文件: (硬链接) ansible oldboy -m file -a "src=/oldboy/alex100.txt path=/oldboy/alex_hard.txt state=hard"
删除文件数据: ansible oldboy -m file -a "path=/oldboy/alex100.txt state=absent"
ansible oldboy -m file -a "path=/oldboy/alex_dir state=absent"
第六个模块:fetch
将被管理主机上的数据进行拉取
ansible oldboy -m fetch -a "path=/oldboy/awk.txt dest=/tmp/"
第七个模块: yum
模块说明: 批量安装软件包(并行)
ansible oldboy -m yum -a "name=htop state=installed"
ansible oldboy -m yum -a "name=htop state=removed"
批量安装和卸载软件
第八个模块:service
第八个模块: service == systemctl
模块说明: 管理服务运行状态
ansible oldboy -m service -a "name=crond state=stopped" 停止服务
ansible oldboy -m service -a "name=crond state=started" 开启服务
ansible oldboy -m service -a "name=crond state=restarted"重启服务
ansible oldboy -m service -a "name=crond state=restarted enabled=yes"让一个服务是否开机自启
PS: 确认服务已经被systemctl命令管理
第九个模块:cron
模块说明: 实现批量设置定时任务
模块参数:
name : 设置定时任务注释信息 (用于判断定时任务是否相同)
minute : 设置时间分钟信息 (0-59 * / -)
hour : 设置时间小时信息 (0-23 * / -)
day : 设置时间日期信息 (1-31 * / -)
month : 设置时间月份信息 (1-12 * / -)
weekday : 设置时间星期信息 (0-6 * / -)
jobs : 设置任务信息
模块实践:
创建定时任务: ansible oldboy -m cron -a "name='exec shell scripts03' minute=0 hour=2 job='/bin/sh /server/scripts/
oldboy.sh'"
删除定时任务: ansible oldboy -m cron -a "name='exec shell scripts02' state=absent"
注释定时任务:
ansible oldboy -m cron -a "name='exec shell scripts' minute=0 hour=2 day=15 month=12 weekday=5 job='/bin/sh /server/scripts/
oldboy.sh' disabled=yes"
ansible oldboy -m cron -a "name='exec shell scripts' minute=0 hour=2 day=15 month=12 weekday=5 job='/bin/sh /server/scripts/
oldboy.sh' disabled=no"
正常设置定时任务:
分 时 日 月 周 任务信息
第十个模块:user /group
模块说明:批量创建用户信息
comment 给用户添加设置注释信息
create_home 指定是否创建家目录
group 指定用户所属主要组信息
groups 指定用户所属附加组信息
home 指定创建用户家目录位置
name 指定创建用户名称
password 设置用户密码信息
shell 指定用户登录系统方式
state 指定用户信息(absent)
uid 指定用户uid数值信息
如何将用户以及家目录彻底删除
如何给密码铭文信息加密?
方法一:
ansible all -i localhost,-m debug -a "msg={{'123456' | password_hash('sha512','oldboy') }}"
方法二:
yum install -y python -pip
优化pip源 -->
https://developer.aliyun.com/mirror/pypi?spm=a2c6h.13651102.0.0.53322f70kU73Ag
pip install passlib
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
======================================================
第十一个模块:unarchive
模块说明:解压模块
ansible oldboy -m unarchive -a "src=/oldboy/foo.tgz dest=/oldboy/" 将本地压缩数据解压被管理主机上
ansible oldboy -m unarchive -a "src=/oldboy/foo.tgz dest=/oldboy/ remote_src=yes"
ansible oldboy -m unarchive -a "src=/oldboy/sersync-master.zip dest=/oldboy/"
批量管理服务剧本编写
利用剧本可以实现自动化部署服务功能
剧本编写规范:
两部分内容:角色信息 角色任务
-host;
task
具体编写规范:pyyaml语法结构
满足三个要求:
满足信息缩进规范:以两个空格作为一个缩进关系
一级信息
二级信息
三级信息
二级信息
一级信息
满足字典规范:在使用冒号时后面需要跟上空格 使用在结尾不需要有空格
key value oldboy=123456 oldboy:123456
满足信息列表规范:短横线加空格构成列表信息
- 一级信息:开发课程体系
- 一级信息:运维
- 一级信息:网络
利用剧本批量安装软件
编写第一个剧本: - hosts: oldboy
remote_user: root
tasks:
yum: name=htop state=installed
ansible颜色提示说明
绿色信息:没有对远程主机做任何修改 查看操作
黄色信息:对远程主机做数据修改调整
红色信息:远程管理出错
紫色信息:警告提示信息
蓝色信息:命令和剧本执行过程信息
gather_facts :no加快显示执行结果(后面会讲)
剧本执行操作:
ansible-playbook /etc/ansible/ansible_playbook/test01.yaml
剧本语法检查:
ansibe-playbook --syntax-check /etc/ansible/ansible_playbook/rsync_auto.yaml
ansible-playbook -C /etc/ansible/ansible_playbook/rsync_auto.yaml 模拟执行剧本 类似彩排过程
编写备份服务剧本:
第一个历程:修改hosts文件
[rsync_server]
172.16.1.41
[rsync_client]
172.16.1.31
第二历程编写剧本信息
-
hosts: rsync_server
gather_facts: no
tasks- name: install software
yum:name=rsync state=installed
- name: edit rsync conf file
copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/ - name: create user
user: name=rsync create_home=no shell=/sbin/nologin - name: create password file
copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600 - name: create backup dir
file: path=/backup state=directory owner=rsync group=rsync - name: boot rsyncd server
service: name=rsyncd state=started enabled=yes
- name: edit rsync conf file
-
hosts: rsync_client
gather_facts: no
tasks:- name: install software
yum: name=rsync state=installed
- name: create password
copy: content='oldboy123' dest=/etc/rsync.password mode=600
- name: create password