inventory文件

[cassandra:children]
shcp-01
shcp-06
bjyt-03
[cassandra:vars]
snmp_path=/etc/snmp/snmpd.conf [shcp-01]
w-tri_db0[1:5].og.shcp.qihoo.net [shcp-06]
w-tri_db[06:10].og.shcp.qihoo.net ansible_ssh_passwd=123456 [bjyt-03]
w-tri_db[03:07].og.bjtp.qihoo.net ansible_ssh_port=60022

命令行执行

ansible -i cassandra-hosts all -m shell -a "touch /root/test.yxy"  -s --ask-pass --ask-sudo-pass
-s 是使用sudo
--ask-pass 是询问登录密码
--ask-sudo-pass 是询问sudo到指定用户的密码

设置变量

[yangxiaoyi1@w38v inventory]$ ls
cassandra-hosts group_vars host_vars
#
[yangxiaoyi1@w38v group_vars]$ ls
shcp-01 shcp-06
#
[yangxiaoyi1@w38v shcp-01]$ ls
login_vars
# 查看组变量
[yangxiaoyi1@w38v shcp-01]$ cat login_vars
---
sshport: 22
httpport: 80
#执行查看效果
ansible -i cassandra-hosts shcp-01 -m shell -a "echo {{sshport}}" --ask-pass

patterns

关于主机名有几种正则表达式

1. 多个组名和主机名可以用:分隔
ansible -i cassandra-hosts shcp-01:shcp06 -m ping
#在shcp-01不再shcp-06
ansible -i cassandra-hosts shcp-01:\!shcp06 -m ping
#既在shcp-01 又在shcp-06
ansible -i cassandra-hosts shcp-01:\&shcp06 -m ping
2. 通配符*的使用
ansible -i cassandra-hosts w-tri_db*.og.shcp.qihoo.net -m ping
3. 应用正则表达式 以~开头
ansible -i cassandra-hosts ~^b -m ping
4. 逻辑!的应用
ansible -i cassandra-hosts w-tri_db*.og.bjtp.qihoo.net:\!w-tri_db03.og.bjtp.qihoo.net -m ping

模块

  1. shell 在目标主机执行shell命令

    1. ansible all -m shell -a "echo hello"
  2. copy 模块
#复制文件
ansible all -m copy -a "src=/tmp/1.log dest=/tmp"
#复制文件夹中的内容
ansible tmp -m copy -a "src=/tmp/tmp/ dest=/tmp"
#复制整个文件夹
ansible tmp -m copy -a "src=/tmp/tmp dest=/tmp"
  1. file模块
#修改文件权限
ansible tmp -m file -a "dest=/tmp/1.txt owner=yangxiaoyi group=yangxiaoyi mode=600"
#删除目录和文件
ansible tmp -m file -a "dest=/tmp/tmp/1.txt state=absent"
ansible tmp -m file -a "dest=/tmp/tmp/ state=absent"
#创建目录
ansible tmp -m file -a "dest=/tmp/tmp/ state=directory"
#创建文件
ansible tmp -m file -a "dest=/tmp/tmp/file state=touch"
  1. yum模块
# 安装包
ansible tmp -m yum -a "name=httpd state=latest"
删除包
ansible tmp -m yum -a "name=httpd state=absent"
  1. User模块
ansible all -m user -a "name=foo password=<crypted password here>"
#删除用户
ansible tmp -m user -a "name=foo state=absent"
  1. git模块
ansible tmp -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
  1. service模块
#重启服务
ansible tmp -m service -a "name=sshd state=restarted"
value of state must be one of: reloaded, restarted, started, stopped, got: restart
  1. 获取facts

    ansible tmp -m setup

ansible配置文件

用户可以修改一下配置文件来修改设置,他们的被读取的顺序如下:

  • ANSIBLE_CONFIG (一个环境变量)
  • ansible.cfg (位于当前目录中)
  • .ansible.cfg (位于家目录中)
  • /etc/ansible/ansible.cfg

PlayBook使用

#我们想忽略某一错误,通过执行成功与否来做决定,我们可以像这样:
tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result|failed
- command: /bin/something_else
when: result|success
- command: /bin/still/something_else
when: result|skipped

今后所有操作都使用playbook,增加熟练度

模拟场景一

  1. 两台线上服务器需要部署nginx、php-fpmd、

    一台服务器做Mysql
  2. 创建虚拟主机,修改服务配置文件
  3. 日志切割,数据库备份,优化配置,系统优化
  4. 启动服务

具体可按ansible文件夹中roles内容

ansible理解的更多相关文章

  1. 进击的 Ansible(二):如何快速搞定生产环境 Ansible 项目布局?

    Tips:与前文 <进击的 Ansible(一):Ansible 快速入门> 一样,本文使用的 Ansible 版本 2.5.4,项目演示环境 MacOS.由于 Ansible 项目开发活 ...

  2. Ansible用于网络设备管理 part 2 对Jinja2 YAML 和 module的理解

    虽然很不想用“应该”这个词,但是还是写上了,的确我自己目前就是这么理解的. 那么这个理解就是,Ansible的一个key point 就是总的一个playbook是去依赖很多元素的,就像一开始那个图里 ...

  3. ansible基础-理解篇

    1. 介绍 要说现在的部署工具,ansible可以说家喻户晓了. ansible是一个开源软件,用于软件供应.配置管理.应用部署.ansible可以通过SSH.remote PowerShell.其他 ...

  4. ansible role 理解

    1.roles意为角色,主要用于封装playbook实现复用性.在ansible中,roles通过文件的组织结构来展现.

  5. 《Ansible权威指南》笔记(1)——安装,ssh密钥登陆,命令

    2016-12-23 读这本<Ansible权威指南>学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记.另,这本书内容很好,但印刷错误比较多,作者说第二版会 ...

  6. 用Vagrant和Ansible搭建持续交付平台

    这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...

  7. 初探ansible安装

    一.ansible介绍常用的自动化运维工具 Puppet —基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱SaltStack —基于 Python 开发,采用 ...

  8. ansible代码分析第一篇--主文件—ansible分析

    2016年2月23日,学习,分析ansible代码 ansible是一种运维中使用的批量部署的工具,它本身是一种框架,具体的部署和架构分析,下面这篇文章讲的不错. http://os.51cto.co ...

  9. 翻译-Salt与Ansible全方位比较

    原文链接:http://jensrantil.github.io/salt-vs-ansible.html 作者: Jens Rantil 之前某些时候我需要评估配置管理系统.结合从他人得到的意见,我 ...

随机推荐

  1. python之路day10-命名空间和作用域、函数嵌套,作用域链、闭包

    楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return mbigger = my_max(10,20)print ...

  2. python之路day08--文件的操作

    文件的操作 hanfei的博客.txt 1/文件的路径2.编码方式3.操作方式:只读,只写,追加,读写,写读... 只读 f=open('hanfei的博客',mode='r',encoding='u ...

  3. (十五)qt-tcp

    基本流程 QT += core gui network #include "tcp.h" #include "ui_tcp.h" #include <QD ...

  4. 退役之战- SDOI

    嘻嘻, 从文化课中逃脱出来, 很痛苦啊, 英语已经近半年没学了,语文水平水的一批,在其他班里受虐待. 百废待兴. 因为曾经学了一段时间的省选,所以被老师拉回来送人头考试啦. 听说4.5 SDOI一轮哎 ...

  5. 2018-2019-2 20165232《网络对抗技术》Exp1 缓冲区溢出实验

    2018-2019-2 20165232<网络对抗技术>Exp1 缓冲区溢出实验 实验点1:逆向及Bof基础实践 实践任务 用一个pwn1文件. 该程序正常执行流程是:main调用foo函 ...

  6. google搜索指南

    常用搜索技巧 搜索社交媒体@ @twitter 搜索特定价格$ $400 搜素标签# #tag 排除特定词,在词前加减号- -except 搜索完全匹配词,加双引号"" " ...

  7. #2 numpy pandas初步学习记录

    对numpy中的array进行了了解,array方法的取值arr_2d[0:2, 0:2] pandas 1,read_CSV方法 2,head方法 3,loc方法,取值前开后开, 4,replace ...

  8. Java基础14-缓冲区字节流;File类

    作业解析 阐述BufferedReader和BufferedWriter的工作原理, 是否缓冲区读写器的性能恒大于非缓冲区读写器的性能,为什么,请举例说明? 答: BufferedReader对Rea ...

  9. Spring Cloud Commons模块

    上一篇介绍了 Spring Cloud Context模块 ,本文介绍SpringCloud的另一个基础模块 SpringCloud Commons模块 .只要在项目的pom文件中引入了spring- ...

  10. python操作redis命令

    Python操作redis from redis import StrictRedis, ConnectionPoolredis_url="redis://:xxxx@112.27.10.1 ...