ansible入门01
1.批量操作
1.安装在实体机上:批量安装:PXE(预引导执行环境):需要网卡上有DHCP客户端去加载bootloaddercobbler:kickstack:2.安装在虚拟机上:克隆、虚拟机镜像模板、支持批量部署操作3.安装在云主机上:
puppet(ruby)saltstack(python)ansible(python)chef:。。。
批量命令执行工具:
fabric:python语言func:
3.程序发布:
手动发布:脚本执行:不够灵活公司内部专用运维程序框架:集成运维工具,需要运维研发要求:1.不能影响用户体验2.系统不能停机3.不能导致系统故障或造成系统完全不可用要基于灰度进行发布:1.基于主机:发布时,先对部分主机进行发布2.基于用户:在进行发布的时候,先对部分用户进行测试发布路径:在调度器上下线一批主机(标记为维护模式)--》关闭服务--》部署新版本--》启动服务--》在调度器上启动这一批主机按需启动或停止:弹性计算运维的三个阶段:稳定、批量、自动化
4.ansible:可以提供系统配置和命令批量执行(在主机数量相对较小时)
1.无agent:大部分基于ssh协议,例ansible、fabric2.有agent:例func、puppet
需要认证、需要确保连接的安全性
2.ansible
anible的特性:
1.模块化:调用特定的模块、完成特定的任务2.基于python语言实现,由paramiko、PyYAML和Jinja2三个关键模块3.部署简单、无agent4.支持自定义模块5.支持palybook6.支持幂等性:即一个命令执行一次和执行n次的效果是相同的
ansible需要在不同的系统上分别执行不同的命令,需要提前进行判断。
ansible:ansible-doc -l:列出所有可支持的模块
编辑主机组
2.1 预先进行ssh认证
ssh-keygen
ssh-copy-id
2.2 使用ansible
# ansible all -m ping
192.168.61.144 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.61.142 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.61.143 | SUCCESS => {
"changed": false,
"ping": "pong"
}
2.3 ansible常用模块
2.3.1 command模块:
ansible all -m command -a "date 051320422016" #直接修改hosts列表中主机的时间
ansible webservers -m command -a date #显示组中的主机的系统时间
ansible all -m command -a "ls /var"
ansible all -a "ls /var" #命令可省
ansible webservers -a 'useradd user1'
ansible webserver -m command -a "echo test1234|passwd --stdin test"
192.168.61.142 | SUCCESS | rc=0 >>
test1234|passwd --stdin test 192.168.61.143 | SUCCESS | rc=0 >>
test1234|passwd --stdin test
2.3.2 shell模块:
ansible all -m shell -a 'cat /tmp/test' #显示出列表中主机/tmp/test文件中的内容
ansible all -m shell -a "date" #显示组中的主机的系统时间
ansible all -m shell -a "systemctl status httpd"
ansible all -m shell -a "echo mageedu|passwd --stdin yser1" #支持管道
2.3.3 copy模块:
ansible all -m copy -a "src=./abc/test dest=/tmp owner=root group=root mode=600"
#将当前主机的文件复制到远程主机
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=zhangpf"
ansible all -m copy -a "content='this is just a test' dest=/tmp/test.txt" #直接生成内容
# ansible dbserver -m copy -a "src=/etc/hosts dest=/tmp owner=root group=root mode=600"
192.168.61.144 | SUCCESS => {
"changed": true,
"checksum": "2142bf6a810d1fbca7759e922f2c806301923b2c",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "a92245f0d6e4a4a2f8d7a2741d367dff",
"mode": "",
"owner": "root",
"size": 266,
"src": "/root/.ansible/tmp/ansible-tmp-1503209054.65-1091359116985/source",
"state": "file",
"uid": 0
}
192.168.61.143 | SUCCESS => {
"changed": true,
"checksum": "2142bf6a810d1fbca7759e922f2c806301923b2c",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "a92245f0d6e4a4a2f8d7a2741d367dff",
"mode": "",
"owner": "root",
"size": 266,
"src": "/root/.ansible/tmp/ansible-tmp-1503209056.66-183234739097730/source",
"state": "file",
"uid": 0
}
2.3.4 cron 模块:
present:创建absent:删除
ansible all -m cron -a "minute=*/5 job='/sbin/ntpdate 172.18.0.1 &>/dev/null' name=Synctime" #每五分钟通过主机172.18.0.1同步一次时间
ansible all -m cron -a "state=absent name=Synctime" #删除Synctime这个任务
2.3.5 fetch模块:
# ansible webserver -m fetch -a "src=/etc/passwd dest=/home/test"
192.168.61.143 | SUCCESS => {
"changed": true,
"checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3",
"dest": "/home/test/192.168.61.143/etc/passwd",
"md5sum": "bd75ad746a725360b719fc544f98ce3c",
"remote_checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3",
"remote_md5sum": null
}
192.168.61.142 | SUCCESS => {
"changed": true,
"checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3",
"dest": "/home/test/192.168.61.142/etc/passwd",
"md5sum": "bd75ad746a725360b719fc544f98ce3c",
"remote_checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3",
"remote_md5sum": null
}
[root@node003 ansible]# ll /home/test
总用量 8
drwxr-xr-x 3 root root 4096 8月 20 14:19 192.168.61.142
drwxr-xr-x 3 root root 4096 8月 20 14:19 192.168.61.143
2.3.6 file模块:
ansible webservers -m file -a "dest=/src/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/src/foo/b.txt mode=600 owner=test group=test" #类似于chmod
ansible webservers -m file -a "dest=/tmp/test/test.sh mode=755 owner=zhangpf group=root state=directory"
#报错,/tmp下没有/test目录,不能递归创建文件
ansible webservers -m file -a "dest=/tmp/test/b.txt state=absent" #类似于删除文件或目录
ansible all -m file -a "src=/tmp/fstab path=/tmp/fstab.link state=link"
#创建链接文件,链接到的是服务器主机的/etc/fsatb文件而非远程主机的/etc/fsatb文件
ansible all -m file -a "path=/tmp/testc state=link"
2.3.7 hostname模块:
ansible webserver -m hostname -a "name=zhangpf" #直接修改主机名
2.3.8 pip模块:
2.3.9 yum模块:
present(安装)latest(升级)absent(卸载)
ansible all -m yum -a "name=httpd state=present"
2.3.10 service模块:管理服务
runningstartedstoppedrestartedreloaded
enabled=:是否设置此服务开机启动
ansible all -m service -a "name=httpd state=started enabled=1"
2.3.11 user模块:管理用户账号
ansible all -m user -a "name=test uid=1200 groups=zhangpf comment='this is a test' shell=/bin/sh" #创建用户
ansible all -m user -a "name=test state=absent" #删除
ansible all -m user -a "name=test passwd=123456" #添加密码
2.3.12 setup模块:获取变量
ansible 172.18.47.48 -m setup
ansible all -m setup
ansible all -m setup --tree /tmp/facts #搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录
ansible all -m setup -a 'filter=ansible_*_mb' #只显示内存相关信息
ansible all -m setup -a 'filter=ansible_enp0s3' #只显示网卡相关信息
3.YAML语法
3.1 palybook的核心元素
3.2 playbook的基础组件:
- hosts: all #定义使用的主机组,我们可以定义多个hosts
remote_user: root #远程执行的用户
tasks:
- name: create a user test1 #第一个任务
user: name=test1 system=true uid=666 #调用user模块执行任务
- name: create a user test2
user: name=test2 system=true uid=667
# ansible-playbook --check first.yaml PLAY [all] ********************************************************************* TASK [Gathering Facts] *********************************************************
ok: [192.168.61.142]
ok: [192.168.61.144]
ok: [192.168.61.143] TASK [create a user test1] *****************************************************
changed: [192.168.61.144]
changed: [192.168.61.143]
changed: [192.168.61.142] TASK [create a user test2] *****************************************************
changed: [192.168.61.142]
changed: [192.168.61.143]
changed: [192.168.61.144] PLAY RECAP **********************************************************************
192.168.61.142 : ok=3 changed=2 unreachable=0 failed=0
192.168.61.143 : ok=3 changed=2 unreachable=0 failed=0
192.168.61.144 : ok=3 changed=2 unreachable=0 failed=0
3.3 使用ansible-playbook:
# ansible-playbook --list-hosts first.yaml
#显示如下 playbook: first.yaml play #1 (all): all TAGS: []
pattern: [u'all']
hosts (3):
192.168.61.143
192.168.61.142
192.168.61.144
#在/etc/ansible下创建文件web.yaml
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: install httpd conf
copy: src=./web/httpd.conf.v1 dest=/etc/httpd/conf/httpd.conf
- name: start httpd
service: name=httpd state=reloaded
- name: execute ss command
shell: ss -tnl | grep :80
3.4 处理器handlers
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: install httpd conf
copy: src=./web/httpd.conf.v1 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
3.5 tags:任务打标签
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
tags: install
- name: install httpd conf
copy: src=./web/httpd.conf.v1 dest=/etc/httpd/conf/httpd.conf
tags: install
notify: restart httpd
- name: start httpd
service: name=httpd state=started
tags: starthttpd
handlers:
- name: restart httpd
service: name=httpd state=restarted
ansible-playbook -t install web.yaml #一次执行install标签的两个任务
ansible-playbook --check -t install,start web.yaml #一次执行两个标签install、start任务
3.6 variables:变量
ansible 172.18.47.48 -m setup
ansible all -m setup
ansible all -m setup --tree /tmp/facts
#搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录 ansible all -m setup -a 'filter=ansible_*_mb' #只显示内存相关信息
ansible all -m setup -a 'filter=ansible_enp0s3' #只显示网卡相关信息
(a) 向不同的主机传递不同的变量;IP/HOSTNAME varaiable=value var2=value2(b) 向组中的主机传递相同的变量;[groupname:vars]variable=value例:#修改hosts文件
[webserver:vars]
http_port=8080
- hosts: all
remote_user: root
tasks:
- name: install {{ pkname }}
yum: name={{ pkname }} state=present
ansible-playbook --check -e pkname=tree testavr.yaml
ansible-playbook -e pkname=tree testavr.yaml
[webserver]
192.168.61.142 hname=www1
192.168.61.143 hname=www2
- hosts: webserver
remote_user: root
tasks:
- name: set hostname
hostname: name={{ hname }}
3.7 不使用密钥连接

ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansbile_sudo_pass
...
ansible入门01的更多相关文章
- 不用搭环境的10分钟AngularJS指令简易入门01(含例子)
不用搭环境的10分钟AngularJS指令简易入门01(含例子) `#不用搭环境系列AngularJS教程01,前端新手也可以轻松入坑~阅读本文大概需要10分钟~` AngularJS的指令是一大特色 ...
- 【爬虫入门01】我第一只由Reuests和BeautifulSoup4供养的Spider
[爬虫入门01]我第一只由Reuests和BeautifulSoup4供养的Spider 广东职业技术学院 欧浩源 1.引言 网络爬虫可以完成传统搜索引擎不能做的事情,利用爬虫程序在网络上取得数据 ...
- JavaScript基础入门 - 01
JavaScript入门 - 01 准备工作 在正式的学习JavaScript之前,我们先来学习一些小工具,帮助我们更好的学习和理解后面的内容. js代码位置 首先是如何编写JavaScript代码, ...
- [转帖]Ansible 入门秘诀
Ansible 入门秘诀 作者: Jose Delarosa 译者: LCTT jdh8383 | 2019-03-08 09:24 收藏: 2 用 Ansible 自动化你的数据中心的关键点. ...
- CSS3基础入门01
CSS3 基础入门 01 前言 相对于css2来说,css3更新了很多的内容,其中包括选择器.颜色.阴影.背景.文本.边框.新的布局方案.2d.3d.动画等等. 而如果想要学习css3的诸多部分,不妨 ...
- Java基础语法入门01
Java基础语法入门01 学习java你要先进行去了解JDK,JRE,JVM JDK Java开发工具包 JRE Java语言开发的运行环境 JVM Java虚拟机,用于Java语言的跨平台所用. 当 ...
- Ansible 入门指南 - 学习总结
概述 这周在工作中需要去修改 nginx 的配置,发现了同事在使用 ansible 管理者系统几乎所有的配置,从数据库的安装.nginx 的安装及配置.于是这周研究起了 ansible 的基础用法.回 ...
- Ansible 入门指南 - ansible-playbook 命令
上篇文章Ansible 入门指南 - 安装及 Ad-Hoc 命令使用介绍的额是 Ad-Hoc 命令方式,本文将介绍 Playbook 方式. Playbook 译为「剧本」,觉得还挺恰当的. play ...
- 【网络爬虫入门01】应用Requests和BeautifulSoup联手打造的第一条网络爬虫
[网络爬虫入门01]应用Requests和BeautifulSoup联手打造的第一条网络爬虫 广东职业技术学院 欧浩源 2017-10-14 1.引言 在数据量爆发式增长的大数据时代,网络与用户的沟 ...
随机推荐
- Mysql学习笔记—concat以及group_concat的用法(转载)
本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字 ...
- PAT 1061 Dating[简单]
1061 Dating(20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4 ...
- HTML5开发——轻量级JSON存储解决方案Lawnchair.js
Lawnchair是一个轻量级的移动应用程序数据持久化存储方案,同时也是客户端JSON文档存储方法,优点是短小,语法简洁,扩展性比较好. 现在做HTML5移动应用除了LocalStorage的兼容性比 ...
- OpenResty--mysql,redis 项目中的应用
最近刚刚接手同事的OpenResty的项目,发现对mysql,redis的操作没有用连接池,故对此进行了改造. MYSQL 主要是通过mysql_pool.lua 和 dbutil.lua 来封装对数 ...
- spring-data-jpa 介绍 复杂查询,包括多表关联,分页,排序等
本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...
- (10)场景转换(Transitions)
Cocos2d-x最爽的一个特性之一就是提供了在两个不同场景之间直接转换的能力.例如:淡入淡出,放大缩小,旋转,跳动等.从技术上来说,一个场景转换就是在展示并控制一个新场景之前执行一个转换效果. 场景 ...
- macbook 蓝牙关闭按钮灰色的
PRAM重置:1.关机,拔掉所有外设,只连接电源线2.按开机键,接着马上同时按住 command+option+p+r 键,按住后听到三次dang的开机音后放开3.电脑会自行启动,进到登录页面后点关机 ...
- And Design:拓荒笔记——Form表单
And Design:拓荒笔记——Form表单 Form.create(options) Form.create()可以对包含Form表单的组件进行改造升级,会返回一个新的react组件. 经 For ...
- DNS服务器配置实践
实验背景:在Linux系统上配置主要DNS服务器和辅助DNS服务器,所在域名为example.com,子网为192.168.X.0. 启动已安装LINUX系统,进行DNS服务器配置. 一.配置主要DN ...
- 使用老毛桃安装Windows操作系统
首先必须知道什么是PE系统? 当电脑出现问题而不能正常进入系统时候的一种“紧急备用”系统,通常放在U盘中,设置启动项优先级,使得电脑启动的时候加载PE系统. 如何在U盘中安装老毛桃(PE工具箱)? h ...