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.引言 在数据量爆发式增长的大数据时代,网络与用户的沟 ...
随机推荐
- Java中参数传递时值传递的机制分析
参数传递是什么? 在C的函数或是JAVA的方法中,向一个函数或方法内部传递一个参数,比如: void fun( int num ){ num+=2 ; } int a = 3 ...
- (2.2)DDL增强功能-自定义函数与存储过程
1.存储过程 精华总结: 通过对比@@ERROR一般和if判断结合使用,@@TRANCOUNT和try catch块结合使用,xact_abort为on可以单独使用Xact_abort为off时,如果 ...
- php源码编译常见错误解决方案大全
php源码编译常见错误解决方案大全http://www.cnlvzi.com/index.php/Index/article/id/143 在CentOS编译PHP5的时候有时会遇到以下的一些错误信息 ...
- 006-markdown基础语法
1.标题 # 这是一级标题 ## 这是二级标题 ### 这是三级标题 #### 这是四级标题 ##### 这是五级标题 ###### 这是六级标题 2.字体 *这是倾斜的文字* **这是加粗的文字** ...
- EasyUI Progressbar 进度条
通过 $.fn.progressbar.defaults 重写默认的 defaults. 进度条(progressbar)提供了一种显示长时间操作进度的反馈.进度可被更新以便让用户知道当前正在执行的操 ...
- #C++初学记录(并查集)
并查集 题目 今天是伊格那丢的生日.他邀请了很多朋友.现在该吃晚饭了.伊格那丢想知道他至少需要多少张桌子.你必须注意到并不是所有的朋友都认识对方,而且所有的朋友都不想和陌生人待在一起.这个问题的一个重 ...
- 20145335郝昊《网络对抗》逆向及Bof基础实践
20145335郝昊<网络对抗>逆向及Bof基础实践 1.实践说明 本次实践是对一个名为pwn1的可执行Linux文件进行操作.程序的执行流程是:主函数main调用foo函数,foo将函数 ...
- Linux内核分析06
进程的描述和进程的创建 一,进程的描述 进程控制块PCB——task_struct (进程描述符),为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struc ...
- 20162314 Experiment 4 - Graph
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...
- G-Sensor 8452驱动及相关
8452是一款G-Sensor芯片,采用I2C跟主芯片通讯,采用中断方式跟操作系统协作.通过内部检测XYZ三个方向的加速度,实现各种应用. (1)原理框图如下: 现在来实现在WINCE中的I2C驱动, ...