Ansible运维自动化
Ansible运维自动化
一、Ansible-playbook的初步使用
playbook的使用,playbook可以把ansible的模块进行组合
ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/

1、playbook的简单shell模块使用
- [root@ansible scripts]# cat test_shell.yaml #playbook的执行模板
- --- #开头三个小-开头
- - hosts: webB
- tasks:
- - name: test
- shell: echo "welcome to yunjisaun" >> /tmp/username
- - name: test2
- shell: echo "welcome to yunjisuan" >> /tmp/username
- 模板说明:
- --- #开头必须有三个小-,顶格写
- - hosts: #正文配置代码的第一级,必须有两个空格(-占一个空格位)
- - host: webB #webB是host参数的值,值和hosts:之间要有一个空格
- tasks: #tasks:表示接下来要执行的具体任务
- - name: #相对于tasks再多缩进两个格(-占一个空格位),表示属于tasks的下一级
- - name: test #test只是要执行的具体命令的名字可以随便写。name:后还是有一个空格要注意
- shell: #表示调用shell模块执行命令相对于tasks仍旧要多缩进两个空格
- shell: echo "xxx" >> xxx #shell:后边还是要有个空格,需要注意。
执行playbook配置文件
- [root@ansible scripts]# ansible-playbook test_shell.yaml #执行playbook配置文件
- PLAY [webB] ********************************************************************************************************
- TASK [Gathering Facts] *********************************************************************************************
- ok: [webB]
- TASK [test] ********************************************************************************************************
- changed: [webB]
- TASK [test2] *******************************************************************************************************
- changed: [webB]
- PLAY RECAP *********************************************************************************************************
- webB : ok=3 changed=2 unreachable=0 failed=0
2、playbook的简单copy模块的使用
- [root@ansible scripts]# echo "welcom to yunjisuan" >> /tmp/test_copy
- [root@ansible scripts]# cat test_copy.yaml
- ---
- - hosts: all
- tasks:
- - name: test copy
- copy: src=/tmp/copy_test dest=/tmp/
- [root@ansible scripts]# ansible-playbook /service/scripts/test_copy.yaml
- PLAY [all] *********************************************************************************************************
- TASK [Gathering Facts] *********************************************************************************************
- ok: [webA]
- ok: [webB]
- TASK [test copy] ***************************************************************************************************
- changed: [webA]
- changed: [webB]
- PLAY RECAP *********************************************************************************************************
- webA : ok=2 changed=1 unreachable=0 failed=0
- webB : ok=2 changed=1 unreachable=0 failed=0
3、playbook使用register输出命令运行结果
我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了。
我们可以通过register模块追加输出命令的执行结果。
- [root@ansible scripts]# cat test_register.yaml
- ---
- - hosts: all
- tasks:
- - name: test register
- shell: echo "welcome to yunjisuan"
- register: print_result #将之前命令的输出结果保存在变量print_result里
- - debug: var=print_result #将变量的值作为debug输出出来。
- [root@ansible scripts]# ansible-playbook test_register.yaml
- PLAY [all] *********************************************************************************************************
- TASK [Gathering Facts] *********************************************************************************************
- ok: [webA]
- ok: [webB]
- TASK [test register] ***********************************************************************************************
- changed: [webA]
- changed: [webB]
- TASK [debug] *******************************************************************************************************
- ok: [webA] => { #命令的执行结果有输出了
- "print_result": {
- "changed": true,
- "cmd": "echo \"welcome to yunjisuan\"",
- "delta": "0:00:00.002269",
- "end": "2018-06-15 10:28:14.693883",
- "failed": false,
- "rc": 0,
- "start": "2018-06-15 10:28:14.691614",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "welcome to yunjisuan",
- "stdout_lines": [
- "welcome to yunjisuan"
- ]
- }
- }
- ok: [webB] => {
- "print_result": {
- "changed": true,
- "cmd": "echo \"welcome to yunjisuan\"",
- "delta": "0:00:00.002633",
- "end": "2018-06-15 10:28:14.710242",
- "failed": false,
- "rc": 0,
- "start": "2018-06-15 10:28:14.707609",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "welcome to yunjisuan",
- "stdout_lines": [
- "welcome to yunjisuan"
- ]
- }
- }
- PLAY RECAP *********************************************************************************************************
- webA : ok=3 changed=1 unreachable=0 failed=0
- webB : ok=3 changed=1 unreachable=0 failed=0
4、nginx配置下发并检测
- [root@ansible scripts]# cat test_nginx_conf.yaml
- ---
- - hosts: all
- tasks:
- - name: copy nginx.conf
- copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
- - name:
- shell: /usr/local/nginx/sbin/nginx -t
- register: nginx_result
- - debug: var=nginx_result
二、playbook的自定义变量和内置变量
1、在playbook中使用自定义变量
- [root@localhost scripts]# cat test_vars.yaml
- ---
- - hosts: all
- vars: #定义变量
- - name: "yunjisuan" #第一个name变量
- age: "3" #第二个age变量
- tasks:
- - name: "{{ name }}" #{{}}两对大括号引用变量,变量名两头空格
- shell: echo "myname {{ name }},myage {{ age }}"
- register: var_result
- - debug: var=var_result
- 特别提示:
- 引用变量需要在双引号中引用。
- [root@localhost scripts]# ansible-playbook /service/scripts/test_vars.yaml
- [WARNING]: Found variable using reserved name: name #这里提示,name是一个保留的内置变量,我们在自定义时不能用
- PLAY [all] *********************************************************************************************************
- TASK [Gathering Facts] *********************************************************************************************
- ok: [webA]
- ok: [webB]
- TASK [yunjisuan] ***************************************************************************************************
- changed: [webA]
- changed: [webB]
- TASK [debug] *******************************************************************************************************
- ok: [webA] => {
- "var_result": {
- "changed": true,
- "cmd": "echo \"myname yunjisuan,myage 3\"",
- "delta": "0:00:00.002320",
- "end": "2018-06-19 10:45:16.175728",
- "failed": false,
- "rc": 0,
- "start": "2018-06-19 10:45:16.173408",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "myname yunjisuan,myage 3",
- "stdout_lines": [
- "myname yunjisuan,myage 3"
- ]
- }
- }
- ok: [webB] => {
- "var_result": {
- "changed": true,
- "cmd": "echo \"myname yunjisuan,myage 3\"",
- "delta": "0:00:00.002518",
- "end": "2018-06-19 10:45:10.552331",
- "failed": false,
- "rc": 0,
- "start": "2018-06-19 10:45:10.549813",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "myname yunjisuan,myage 3",
- "stdout_lines": [
- "myname yunjisuan,myage 3"
- ]
- }
- }
- PLAY RECAP *********************************************************************************************************
- webA : ok=3 changed=1 unreachable=0 failed=0
- webB : ok=3 changed=1 unreachable=0 failed=0
- #我们修改一下name这个变量再发送,就不会出警告了
- [root@localhost scripts]# cat test_vars.yaml
- ---
- - hosts: all
- vars:
- - names: "yunjisuan"
- age: "3"
- tasks:
- - name: "{{ names }}"
- shell: echo "myname {{ names }},myage {{ age }}"
- register: var_result
- - debug: var=var_result
在使用自定义变量时,我们要特别注意不要和系统的内置保留变量同名,容易引发问题
2、在playbook中使用Ansible内置变量
我们可以使用ansible all -m setup | less查看ansible内置变量
- [root@localhost scripts]# cat test_setupvars.yaml
- ---
- - hosts: all
- gather_facts: True #使用ansible内置变量
- tasks:
- - name: setup var
- shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}"
- register: var_result
- - debug: var=var_result
- [root@localhost scripts]# ansible-playbook test_setupvars.yaml
- PLAY [all] *********************************************************************************************************
- TASK [Gathering Facts] *********************************************************************************************
- ok: [webA]
- ok: [webB]
- TASK [setup var] ***************************************************************************************************
- changed: [webA]
- changed: [webB]
- TASK [debug] *******************************************************************************************************
- ok: [webA] => {
- "var_result": {
- "changed": true,
- "cmd": "echo \"ip 192.168.200.132 cpu 1\"",
- "delta": "0:00:00.002408",
- "end": "2018-06-19 11:32:44.540658",
- "failed": false,
- "rc": 0,
- "start": "2018-06-19 11:32:44.538250",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "ip 192.168.200.132 cpu 1",
- "stdout_lines": [
- "ip 192.168.200.132 cpu 1"
- ]
- }
- }
- ok: [webB] => {
- "var_result": {
- "changed": true,
- "cmd": "echo \"ip 192.168.200.138 cpu 1\"",
- "delta": "0:00:00.002102",
- "end": "2018-06-19 11:32:44.526875",
- "failed": false,
- "rc": 0,
- "start": "2018-06-19 11:32:44.524773",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "ip 192.168.200.138 cpu 1",
- "stdout_lines": [
- "ip 192.168.200.138 cpu 1"
- ]
- }
- }
- PLAY RECAP *********************************************************************************************************
- webA : ok=3 changed=1 unreachable=0 failed=0
- webB : ok=3 changed=1 unreachable=0 failed=0
简单演示一下ansible内置变量的取用方法ansible all -m setup | less
- [root@localhost scripts]# cat test_setupvars.yaml
- ---
- - hosts: all
- gather_facts: True
- tasks:
- - name: setup var
- shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test
- - name: setup var2
- shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
- register: var_result
- - debug: var=var_result
三、Playbook下发可变配置文件
配置文件如果使用copy模块去下发的话,那配置都是一样的;
如果下发的配置文件里有可变的配置,需要用到template模块。
1、利用template模块下发可变的配置文件
- [root@localhost scripts]# cat /tmp/test
- my name is {{ myname }} #自定义变量
- my name is {{ ansible_all_ipv4_addresses[0] }} #系统变量
- [root@localhost scripts]# cat test_filevars.yaml
- ---
- - hosts: all
- gather_facts: True #开启系统变量
- vars:
- - myname: "yunjisuan" #自定义变量
- tasks:
- - name: template test
- template: src=/tmp/test dest=/root/test #使用template下发可变配置文件
- [root@localhost scripts]# ansible-playbook test_filevars.yaml
2、下发配置文件里面使用判断语法
- [root@localhost scripts]# cat /tmp/if.j2
- {% if PORT %} #if PORT存在
- ip=0.0.0.0:{{ PORT }}
- {% else %} #否则的话
- ip=0.0.0.0:80
- {% endif %} #结尾
- [root@localhost scripts]# cat test_ifvars.yaml
- ---
- - hosts: all
- gather_facts: True #开启系统内置变量
- vars:
- - PORT: 90 #自定义变量
- tasks:
- - name: jinja2 if test
- template: src=/tmp/if.j2 dest=/root/test
- [root@localhost scripts]# ansible-playbook test_ifvars.yaml
如果我们将变量PORT值为空的话,就会是另外的结果
- [root@localhost scripts]# cat test_ifvars.yaml
- ---
- - hosts: all
- gather_facts: True
- vars:
- - PORT: #置空
- tasks:
- - name: jinja2 if test
- template: src=/tmp/if.j2 dest=/root/test
- [root@localhost scripts]# ansible-playbook test_ifvars.yaml
四、Playbook的notify通知和下发nginx配置
- #实战下发可执行动作的可变的nginx配置文件
- [root@localhost scripts]# head -1 /tmp/nginx.j2
- worker_processes {{ ansible_processor_count }}; #可变的参数
- [root@localhost scripts]# cat test_nginxvars.yaml
- ---
- - hosts: all
- gather_facts: True #开启系统内置变量
- tasks:
- - name: nginx conf
- template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
- notify:
- - reload nginx #下发通知给handlers模块执行名字叫做reload nginx的动作
- handlers: #定义动作
- - name: reload nginx #动作的名字
- shell: /usr/local/nginx/sbin/nginx -s reload
- [root@localhost scripts]# ansible-playbook test_nginxvars.yaml
Ansible运维自动化的更多相关文章
- Ansible运维自动化工具19个常用模块使用实例【转】
一.模块列表 1.setup 2.ping 3.file 4.copy 5.command 6.shell 7.script 8.cron 9.yum 10.service 11.group 12.u ...
- Ansible 运维自动化 ( 配置管理工具 )
背景 出差背景,要搞项目的自动化部署.因为只直接对接生产分发,机器又非常多,这样以往使用的bat只能作为应急方案了,还是得考虑使用专业化的工具来做这个事情! 当下有许多的运维自动化工具( 配置管理 ) ...
- Ansible运维自动化工具
1>Ansible 1>ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabri ...
- Centos7安装配置ansible运维自动化工具
准备至少两台机器 Centos7,这两台机器都关闭 selinux IP:106.13.118.132 服务端(ansible) masterIP:148.70.60.244 节点 slaver 服务 ...
- Centos7搭建ansible运维自动化工具
1)设置主机名和hosts文件 2)配置阿里云repo源 Wget -O /etc/yum.repos.d/aliyun.repo https://mirrors.aliyun.com/repo/Ce ...
- 运维自动化神器ansible之user模块
运维自动化神器ansible之user模块 一.概述 user模块 可管理远程主机上的 用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 二.参数介绍 name: 用于指定操作 ...
- 运维自动化之ansible的安装与使用 转
运维自动化之ansible的安装与使用 随着服务器数量的增长,我们需要一个批量工具去提高工作效率,之前用的是puppet,ansible的简单,适用让我眼前一亮,决定写一篇ansible从安装到基本配 ...
- 运维自动化之1 - ansible 批量主机管理
2000 - 2016 年,维护的小型机.linux刚开始的2台增加到上千台,手工检查.日常版本升级需要管理太多设备,必须通过运维自动化实现 特别是版本升级,需要到同类机器部署代码.起停设备,必须在一 ...
- 运维自动化之salt笔记
1:saltstack的基本介绍 2:salt的安装 1:服务端1:安装2:配置文件3:运行4:注意事项2:客户端1:安装2:配置文件3:运行4:注意事项 3:salt的使用: 1:基础知识1:tar ...
随机推荐
- mysql无法安装,报 The older version of MySQL Installer - Community cannot be removed. Contact your technical support group
解决办法: C:\Users\Administrator>msiexec /i C:\Users\Administrator\Downloads\mysql-installer-web-comm ...
- JAVA服务cpu占用高排查
最近线上机器偶尔有台cpu达到100%,还居高不下.同样负载的其他机器却正常,我想肯定是代码哪里有问题了 首先我们top看下 可定位到对应占用高的PID 然后=>ps -mp PID -o TH ...
- Maven Gradle
场景:随着项目越来越规范,对构建工具的要求越来越高,我们从Maven转到了Gradle. 转自:http://www.infoq.com/cn/news/2011/04/xxb-maven-6-gra ...
- 爬取QQ音乐歌手的歌单
import requests# 引用requests库res_music = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search ...
- java中字符串“不可变性”的破坏,使用反射破坏final属性。以及涉及到字符串常量池的问题。
大家都清楚java中String类是不可变的,它的定义中包含final关键字.一旦被创建,值就不能被改变(引用是可以改变的). 但这种“不可变性”不是完全可靠的,可以通过反射机制破坏.参考一下代码: ...
- 网页中,鼠标点击与javascript的click事件怎么区分处理
就下面问题发现另一个方式: js代码: <script> //IE if(document.all) { document.getElementById("clickme&quo ...
- CSS效果:3D卡片
HTML: <html lang="en"> <head> <meta charset="UTF-8"> <meta ...
- linux安装postgresql
第一步在编译安装postgresql源码的时候,需要用到以下依赖,若本机没有的话,需要提前安装依赖环境,执行以下命令:yum install gcc gcc-c++yum install zlib-d ...
- Spring Boot的第一个程序
Spring boot是由Pivotal团队提供的全新框架,在设计之初,其目的是为了简化Spring应用的创建.运行.测试.调试.部署的过程.Spring Boot框架不仅简化了Spring的搭建过程 ...
- Linux常用文件操作命令
一.进入文件夹 格式:cd [目录名称] 常用选项: cd / 进入当前目录 cd .. 返回上一级目录. cd ../.. 将当前目录向上移动两级. cd - 返回最近访问目录. 二.显示 ...