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 ...
随机推荐
- 精进之路之AQS及相关组件
AQS ( AbstractQueuedSynchronizer)是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Sem ...
- Mysql 设置远程连接
一.问题分析 有时候使用数据库远程连接工具连接MySQL的时候总是连接不上,确认过账号密码正确,端口telnet端口又是通的. Navicat Premium报错如下: 1130 - Host '19 ...
- Qt终结者之QML动画
前言 使用QML差不多2年了,在使用过程中深深的感受到QML的强大与便捷,让我深陷其中,不能自拔.其中QML相比传统的界面最大的优势就是便捷的动画效果与炫酷的粒子效果,让QML做出来的界面能媲美WPF ...
- Sharepoint 2016 配置FBA(四)添加用户到Membership数据库
现在还不能用FBA登录,因为数据库还没有用户. 有一些方法来管理membership数据库,有可以用IIS来管理.推荐使用 SharePoint 2016 FBA Pack(https://share ...
- (原创)Verilog三段式状态机
下面以上图一个简单的FSM说明三段式Verilog状态机范式: `timescale 1ns / 1ps module FSM( clk,rst_n, in1,in2, out1,out2, CS,N ...
- 201771010141 周强《面向对象程序设计(java)》第十三周学习总结
实验目的与要求 (1) 掌握事件处理的基本原理,理解其用途: (2) 掌握AWT事件模型的工作机制: (3) 掌握事件处理的基本编程模型: (4) 了解GUI界面组件观感设置方法: (5) 掌握Win ...
- linux服务器ssh免密登录
环境:两台服务器,Park01.Park02,配置ssh免密登录 在Park01执行:ssh-keygen 然后一直回车 生成节点的公钥和私钥,生成的文件会自动放在/root/.ssh目录下 然后 ...
- 基本的java加密算法MD5等等
简单的java加密算法有: BASE64 严格地说,属于编码格式,而非加密算法 MD5 (Message Digest algorithm 5,信息摘要算法) SH ...
- Servlet抽取的问题-method传递问题+表单提交的问题
隐藏域解决该问题: 其中,hidden就是隐形域. 表单提交的问题: 1.通过按钮实现: 2.通过function中,获取页面元素.submit方法
- s21day22 python笔记
s21day22 python笔记 一.内容回顾及补充 模块补充 importlib.import_module:通过字符串的形式导入模块 #示例一: import importlib # 用字符串的 ...