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 ...
随机推荐
- django遇到的问题-系列1
django开发中遇到的问题以及解决方法: 1.You called this URL via POST, but the URL doesn't end in a slash and you hav ...
- c#计算器
代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴. using System;using System.Collections.Generic;using System. ...
- matlab程序设计
一.M文件 1.脚本文件 (1)英文字母开头命名 (2)所产生的所有变量驻留在base workspace,只要不用clear,就只有关闭MATLAB,才会被删除 2.函数文件 (1)function ...
- python super超类方法
super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重复调用( ...
- 入门项目 A6 lib common 登陆状态装饰
# 用户认证装饰器 def auth(func): from core import src def inner(*args, **kwargs): # 调用被装饰函数前需要做的操作 if src.u ...
- Verdi 看波形常用快捷操作
Verdi看波形的基本操作小结: 快捷键:(大写字母=Shift+小写) g get, signlas添加信号,显示波形n next, Search Forward选定信号按指定的值(上升 ...
- Qt设置创建部分半透明,上面控件不透明
//头文件#pragma once #include <QWidget> #include "ui_widgetFullAD.h" class widgetFullAD ...
- Classnotfoundexception 与 noClassDelfaultError的区别
ClassNotFoundException 这个异常特别常见,就是class找不到异常,一般的问题就是: 1 调用class的forName方法时,找不到指定的类 2 ClassLoader 中的 ...
- redis命令Set类型(七)
集合类型 集合类型:无序.不可重复 列表类型:有序.可重复 命令: 1>增加/删除元素 语法:SADD key member [member -] 127.0.0.1:6379> sadd ...
- 2019 Power BI最Top50面试题,助你面试脱颖而出系列<上>
距离4月还剩11天, 你是否还在投简历找工作而机会寥寥? 你是否还在四处奔波疲于面试而结果不意? ....... 知否知否, 天下武功唯快不破, 传说江湖有本Power BI 面试真香秘籍, 能助你快 ...