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 ...
随机推荐
- Linux 网络编程(一)--Linux操作系统概述
一.Linux的内核版本 Linux内核的编号采用如下编号形式: 主版本号.此版本号.主补丁号.次补丁号 例如:2.6.26.3 第一个数字”2”是主版本号,表示第2大版本. 第二个数字”6”是此版本 ...
- mybatics 与jpa
mybatics,dao层接口,mapper: public interface UserMapper { @Select("SELECT * FROM T_ROLE WHERE ROLE_ ...
- Sql Prompt---Unable to connect to the Redgate Client Service
今天在使用SQLPrompt时,突然发现所有的提示都不能用了,在百度上搜索无果,刚刚在Google搜索,果然找到了自己想要的答案,今天把搜索的结果发布在此,希望能够帮助到有需要的朋友. 客户端服务停止 ...
- dos3章
FOR命令中有一些变量,他们的用法许多新手朋友还不太了解,今天给大家讲解他们的用法! 先把FOR的变量全部列出来: ~I - 删除任何引号("),扩展 %I %~f ...
- python基础一之课后作业:编写登录接口
1 # Author : Mamba 2 3 #python基础一之课后作业:编写登录接口 4 5 # 输入用户名密码 6 # 认证成功后显示欢迎信息 7 # 用户名3次输入错误后,退出程序 8 # ...
- Linux 驱动——Button驱动4(fasync)异步通知
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- element UI 下拉菜单滚动监听(vue指令)
直接看代码吧,可以直接粘贴此代码到你的编辑器中看效果. <template> <div class="page-component"> <div cl ...
- TowSum
- QEMU模拟器Windows版本模拟ARMX86CPU指令
http://qemu.weilnetz.de/ QEMU Binaries for Windows
- crontab下git命令无效
原因 crontab默认的 path 设置和系统自身的有区别 git 命令放在 /usr/local/bin/ 目录 whereis git 而crontab 却在 /sbin:/bin:/usr ...