架构一、Ansible基础及常用模块

Ansible-playbook
Playbook 常规使用,handiers,vars,template,with_items迭代

Handiers任务处理器
介绍playbook时提到handlers是任务处理器,和前面使用到的task一样都是用来定义任务,区别在于handlers需要满足某些条件时才会触发任务操作,所以需要一个任务通知者notify对应的handler后,任务才会被执行。不管有多少个通知者notify了handler,都需要等到playbook中所有task执行完成后,最后才执行对应的handlers,而且只会被执行一次。handler的使用也很简单,只需要在task中notify对应的handler即可,和tasks一样也可以创建多个handlers,一个task可以notify多个handlers,多个task也可以notify同一个handler,handler的定义和task的定义是一样的,定义handler的名称,使用的模块,以及给模块传递的参数;在task中只需要notify想要运行的handlser名称即可。

程序自动化的常用软件

Ansible:常用 基于python开发支持自己开发模块 基于ssh协议 轻量级 (300台以内)
Saltstacj:基于python开发 有客户端也有服务器 支持统一管理 轻量级 (300~1000内)
Puppet: 基于ruby开发 软件比较复杂 支持1000+台主机的管理 重量级 (谷歌国内少用)

Ansible 程序自动化

1.Ansible理论部分
  特点、特性:模块化(模块多(3000),功能不同,用什么功能调用什么模块)
        安全:基于openssh开发,管理远程的ssh开发
  可定制模块:
三个核心模块:paramiko(远程管理ssh开发)  PyYAML(支持剧本开发)  Jinja2(支持模板语言)
幂等性:每次的操作结果都一样(名称一样内容不一样覆盖,同名同内容不操作同样成功)
2.基本命令
   Ansible 操作对象 -m 模块 -a ‘参数’
   操作对象(webserver   dbserver   all’全部’)
3.常用模块15个
  绿色:执行成功(本次操作执行成功,但是文件内容未修改)
  黄色:执行成功且成功修改
  红色:失败

查询用法 ansible-doc -l | wc -l多少个模块 ansible-doc -s 模块名 查看模块用法

常用模块

Ping       用于检测被控端是否在线
ansible dbserver -m ping
Command  只用于简单的命令
ansible all -m command -a 'ls /home'  参数可换 ‘date’
Shell      功能与Command相同但可执行更复杂的命令  * / 等等
ansible dbserver -m shell -a 'ls -l /tmp/test'                       查询详情
ansible webserver -m shell -a 'rpm -qa | grep httpd'               查看是否安装过
ansible webserver -m shell -a 'systemctl is-enabled httpd'           启动服务
Cron      计时任务
ansible webserver -m cron -a 'minute=*/10 job="echo hello world" name="hello world"'    添加任务并起个任务名称
ansible webserver -m cron -a 'name="hello world" state=absent' 删除任务     日  day
User      创建用户
ansible webserver -m user -a 'name=nginx create_home=no system =yes'       创建用户不指定家目录  并添加到系统
ansible webserver -m user -a 'name=nginx uid=777 group=sun shell=/bin/bash home=/tmp/nginx'   家目录先要自己创建
ansible webserver -m user -a 'name=nginx state=absent'           删除用户
ansible webserver -m user -a 'name=szd remove=yes state=absent'   删除用户并删除家目录
Group     创建组
ansible webserver -m group -a 'name=my state=absent'
ansible webserverq -m group -a 'name=my gid=770 system=yes'
Copy      拷贝文件
ansible webserver -m copy -a 'src=/usr/local/etc dest=/tmp/ backup=yes'  拷贝并备份
ansible dbserver -m file  -a 'path=/etc/sun.conf state=touch'            创建文件
ansible dbserver -m copy -a 'src=/etc/sun.conf dest=/root remote_src=yes' 被控端的文件拷贝至被控端的其他目录
File   更改文件属性 创建目录  
(0644四位权限?特殊权限位,拥有者位,同组用户位,其余用户位
ansible dbserver -m file -a 'path=/tmp/test owner=root group=root mode=0644 state=touch'  创建文件
ansible dbserver -m file -a 'path=/tmp/test owner=sun group=sun mode=0644 state=file'     修改文件属性
ansible dbserver -m file -a 'path=/tmp/java src=/usr/bin/java state=link'                   创建软链接
ansible dbserver -m file -a 'path=/tmp/test.conf src=/etc/sysctl.conf state=hard'            创建硬链接
ansible dbserver -m file -a 'path=/test owner=sun group=sun recurse=yes'                 递归修改
ansible dbserver -m file -a 'path=/tmp/test state=absent'                               删除文件  写路径
Fetch   拷贝被控端的文件至主控端(只能获取文件,不能获取目录)
ansible all -m fetch -a 'src=/var/log/messages dest=/date'
yum -y install tree   tree是用一种结构来看
tree /date/

ansible all -m replace -a "path=/root/aa.conf regexp='sunzhendong' replace='short_open_tag = On'"   替换
Yum    被控端yum安装
ansible webserver -m yum -a 'name=httpd state=absent'                  删除下载软件
ansible webserver  -m yum -a 'name=httpd,vsftpd  state=installed'        下载多个软件用逗号分隔
Service  用于被控端各种服务管理(启动、停止、重启、重新加载等‘服务启动级别’runlevel’’)
ansible webserver -m service -a 'name=httpd enabled=yes state=started'     启动服务,开机自启
ansible webserver -m service -a 'name=httpd   state=stopped'             停止服务
ansible webserver -m service -a 'name=httpd   state=restarted'            重启服务
Replace  替换(类似sed命令)
ansible webserver -m replace -a 'dest=/tmp/nginx regexp=starting replace=789'   更换文本内容 
Lineinfile   添加一行
ansible all -m lineinfile -a 'dest=/tmp/test line="192.168.11.sdasd"'                    添加一个新行
ansible all -m lineinfile -a 'dest=/tmp/test  line=szd=nihaoma state=absent'             删除一个行
Hostname   临时修改主机名称
ansible dbserver -m hostname -a 'name=dbserver'                            修改主机名
Unarchive   主控端直接解压到被控端
ansible webserver -m unarchive -a 'src=/root/nginx-1.15.4.tar.gz dest=/usr/src'     解压
有时间做远程?
Setup       获取被控端的值(查看版本、CPU、主机名、地址)
ansible dbserver -m setup -a 'filter=*version*'   查看版本
*cpu*   *hostname*   address             也可查看
Script       执行脚本
ansible dbserver -m script -a 'test.sh'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值