Ansible运维自动化

时间:2022-10-23 11:43:57

Ansible运维自动化

一、Ansible-playbook的初步使用

playbook的使用,playbook可以把ansible的模块进行组合

ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/

Ansible运维自动化

1、playbook的简单shell模块使用

  1. [root@ansible scripts]# cat test_shell.yaml  #playbook的执行模板
  2. ---         #开头三个小-开头
  3. - hosts: webB
  4. tasks:
  5. - name: test
  6. shell: echo "welcome to yunjisaun" >> /tmp/username
  7. - name: test2
  8. shell: echo "welcome to yunjisuan" >> /tmp/username
  9. 模板说明:
  10. ---  #开头必须有三个小-,顶格写
  11. - hosts:   #正文配置代码的第一级,必须有两个空格(-占一个空格位)
  12. - host: webB   #webB是host参数的值,值和hosts:之间要有一个空格
  13. tasks:        #tasks:表示接下来要执行的具体任务
  14. - name:     #相对于tasks再多缩进两个格(-占一个空格位),表示属于tasks的下一级
  15. - name: test  #test只是要执行的具体命令的名字可以随便写。name:后还是有一个空格要注意
  16. shell:  #表示调用shell模块执行命令相对于tasks仍旧要多缩进两个空格
  17. shell: echo "xxx" >> xxx     #shell:后边还是要有个空格,需要注意。

执行playbook配置文件

  1. [root@ansible scripts]# ansible-playbook test_shell.yaml #执行playbook配置文件
  2. PLAY [webB] ********************************************************************************************************
  3. TASK [Gathering Facts] *********************************************************************************************
  4. ok: [webB]
  5. TASK [test] ********************************************************************************************************
  6. changed: [webB]
  7. TASK [test2] *******************************************************************************************************
  8. changed: [webB]
  9. PLAY RECAP *********************************************************************************************************
  10. webB                       : ok=3    changed=2    unreachable=0    failed=0

2、playbook的简单copy模块的使用

  1. [root@ansible scripts]# echo "welcom to yunjisuan" >> /tmp/test_copy
  2. [root@ansible scripts]# cat test_copy.yaml
  3. ---
  4. - hosts: all
  5. tasks:
  6. - name: test copy
  7. copy: src=/tmp/copy_test dest=/tmp/
  8. [root@ansible scripts]# ansible-playbook /service/scripts/test_copy.yaml
  9. PLAY [all] *********************************************************************************************************
  10. TASK [Gathering Facts] *********************************************************************************************
  11. ok: [webA]
  12. ok: [webB]
  13. TASK [test copy] ***************************************************************************************************
  14. changed: [webA]
  15. changed: [webB]
  16. PLAY RECAP *********************************************************************************************************
  17. webA                       : ok=2    changed=1    unreachable=0    failed=0
  18. webB                       : ok=2    changed=1    unreachable=0    failed=0

3、playbook使用register输出命令运行结果

我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了。 
我们可以通过register模块追加输出命令的执行结果。

  1. [root@ansible scripts]# cat test_register.yaml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: test register
  6. shell: echo "welcome to yunjisuan"
  7. register: print_result          #将之前命令的输出结果保存在变量print_result里
  8. - debug: var=print_result         #将变量的值作为debug输出出来。
  9. [root@ansible scripts]# ansible-playbook test_register.yaml
  10. PLAY [all] *********************************************************************************************************
  11. TASK [Gathering Facts] *********************************************************************************************
  12. ok: [webA]
  13. ok: [webB]
  14. TASK [test register] ***********************************************************************************************
  15. changed: [webA]
  16. changed: [webB]
  17. TASK [debug] *******************************************************************************************************
  18. ok: [webA] => {                                             #命令的执行结果有输出了
  19. "print_result": {
  20. "changed": true,
  21. "cmd": "echo \"welcome to yunjisuan\"",
  22. "delta": "0:00:00.002269",
  23. "end": "2018-06-15 10:28:14.693883",
  24. "failed": false,
  25. "rc": 0,
  26. "start": "2018-06-15 10:28:14.691614",
  27. "stderr": "",
  28. "stderr_lines": [],
  29. "stdout": "welcome to yunjisuan",
  30. "stdout_lines": [
  31. "welcome to yunjisuan"
  32. ]
  33. }
  34. }
  35. ok: [webB] => {
  36. "print_result": {
  37. "changed": true,
  38. "cmd": "echo \"welcome to yunjisuan\"",
  39. "delta": "0:00:00.002633",
  40. "end": "2018-06-15 10:28:14.710242",
  41. "failed": false,
  42. "rc": 0,
  43. "start": "2018-06-15 10:28:14.707609",
  44. "stderr": "",
  45. "stderr_lines": [],
  46. "stdout": "welcome to yunjisuan",
  47. "stdout_lines": [
  48. "welcome to yunjisuan"
  49. ]
  50. }
  51. }
  52. PLAY RECAP *********************************************************************************************************
  53. webA                       : ok=3    changed=1    unreachable=0    failed=0
  54. webB                       : ok=3    changed=1    unreachable=0    failed=0

4、nginx配置下发并检测

  1. [root@ansible scripts]# cat test_nginx_conf.yaml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: copy nginx.conf
  6. copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
  7. - name:
  8. shell: /usr/local/nginx/sbin/nginx -t
  9. register: nginx_result
  10. - debug: var=nginx_result

二、playbook的自定义变量和内置变量

1、在playbook中使用自定义变量

  1. [root@localhost scripts]# cat test_vars.yaml
  2. ---
  3. - hosts: all
  4. vars:         #定义变量
  5. - name: "yunjisuan"   #第一个name变量
  6. age: "3"            #第二个age变量
  7. tasks:
  8. - name: "{{ name }}"      #{{}}两对大括号引用变量,变量名两头空格
  9. shell: echo "myname {{ name }},myage {{ age }}"
  10. register: var_result
  11. - debug: var=var_result
  12. 特别提示:
  13. 引用变量需要在双引号中引用。
  14. [root@localhost scripts]# ansible-playbook /service/scripts/test_vars.yaml
  15. [WARNING]: Found variable using reserved name: name        #这里提示,name是一个保留的内置变量,我们在自定义时不能用
  16. PLAY [all] *********************************************************************************************************
  17. TASK [Gathering Facts] *********************************************************************************************
  18. ok: [webA]
  19. ok: [webB]
  20. TASK [yunjisuan] ***************************************************************************************************
  21. changed: [webA]
  22. changed: [webB]
  23. TASK [debug] *******************************************************************************************************
  24. ok: [webA] => {
  25. "var_result": {
  26. "changed": true,
  27. "cmd": "echo \"myname yunjisuan,myage 3\"",
  28. "delta": "0:00:00.002320",
  29. "end": "2018-06-19 10:45:16.175728",
  30. "failed": false,
  31. "rc": 0,
  32. "start": "2018-06-19 10:45:16.173408",
  33. "stderr": "",
  34. "stderr_lines": [],
  35. "stdout": "myname yunjisuan,myage 3",
  36. "stdout_lines": [
  37. "myname yunjisuan,myage 3"
  38. ]
  39. }
  40. }
  41. ok: [webB] => {
  42. "var_result": {
  43. "changed": true,
  44. "cmd": "echo \"myname yunjisuan,myage 3\"",
  45. "delta": "0:00:00.002518",
  46. "end": "2018-06-19 10:45:10.552331",
  47. "failed": false,
  48. "rc": 0,
  49. "start": "2018-06-19 10:45:10.549813",
  50. "stderr": "",
  51. "stderr_lines": [],
  52. "stdout": "myname yunjisuan,myage 3",
  53. "stdout_lines": [
  54. "myname yunjisuan,myage 3"
  55. ]
  56. }
  57. }
  58. PLAY RECAP *********************************************************************************************************
  59. webA                       : ok=3    changed=1    unreachable=0    failed=0
  60. webB                       : ok=3    changed=1    unreachable=0    failed=0
  61. #我们修改一下name这个变量再发送,就不会出警告了
  62. [root@localhost scripts]# cat test_vars.yaml
  63. ---
  64. - hosts: all
  65. vars:
  66. - names: "yunjisuan"
  67. age: "3"
  68. tasks:
  69. - name: "{{ names }}"
  70. shell: echo "myname {{ names }},myage {{ age }}"
  71. register: var_result
  72. - debug: var=var_result

在使用自定义变量时,我们要特别注意不要和系统的内置保留变量同名,容易引发问题

2、在playbook中使用Ansible内置变量

我们可以使用ansible all -m setup | less查看ansible内置变量

  1. [root@localhost scripts]# cat test_setupvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True    #使用ansible内置变量
  5. tasks:
  6. - name: setup var
  7. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}"
  8. register: var_result
  9. - debug: var=var_result
  10. [root@localhost scripts]# ansible-playbook test_setupvars.yaml
  11. PLAY [all] *********************************************************************************************************
  12. TASK [Gathering Facts] *********************************************************************************************
  13. ok: [webA]
  14. ok: [webB]
  15. TASK [setup var] ***************************************************************************************************
  16. changed: [webA]
  17. changed: [webB]
  18. TASK [debug] *******************************************************************************************************
  19. ok: [webA] => {
  20. "var_result": {
  21. "changed": true,
  22. "cmd": "echo \"ip 192.168.200.132 cpu 1\"",
  23. "delta": "0:00:00.002408",
  24. "end": "2018-06-19 11:32:44.540658",
  25. "failed": false,
  26. "rc": 0,
  27. "start": "2018-06-19 11:32:44.538250",
  28. "stderr": "",
  29. "stderr_lines": [],
  30. "stdout": "ip 192.168.200.132 cpu 1",
  31. "stdout_lines": [
  32. "ip 192.168.200.132 cpu 1"
  33. ]
  34. }
  35. }
  36. ok: [webB] => {
  37. "var_result": {
  38. "changed": true,
  39. "cmd": "echo \"ip 192.168.200.138 cpu 1\"",
  40. "delta": "0:00:00.002102",
  41. "end": "2018-06-19 11:32:44.526875",
  42. "failed": false,
  43. "rc": 0,
  44. "start": "2018-06-19 11:32:44.524773",
  45. "stderr": "",
  46. "stderr_lines": [],
  47. "stdout": "ip 192.168.200.138 cpu 1",
  48. "stdout_lines": [
  49. "ip 192.168.200.138 cpu 1"
  50. ]
  51. }
  52. }
  53. PLAY RECAP *********************************************************************************************************
  54. webA                       : ok=3    changed=1    unreachable=0    failed=0
  55. webB                       : ok=3    changed=1    unreachable=0    failed=0

简单演示一下ansible内置变量的取用方法ansible all -m setup | less

  1. [root@localhost scripts]# cat test_setupvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True
  5. tasks:
  6. - name: setup var
  7. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test
  8. - name: setup var2
  9. shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
  10. register: var_result
  11. - debug: var=var_result

三、Playbook下发可变配置文件

配置文件如果使用copy模块去下发的话,那配置都是一样的; 
如果下发的配置文件里有可变的配置,需要用到template模块。

1、利用template模块下发可变的配置文件

  1. [root@localhost scripts]# cat /tmp/test
  2. my name is {{ myname }} #自定义变量
  3. my name is {{ ansible_all_ipv4_addresses[0] }}  #系统变量
  4. [root@localhost scripts]# cat test_filevars.yaml
  5. ---
  6. - hosts: all
  7. gather_facts: True    #开启系统变量
  8. vars:
  9. - myname: "yunjisuan" #自定义变量
  10. tasks:
  11. - name: template test
  12. template: src=/tmp/test dest=/root/test #使用template下发可变配置文件
  13. [root@localhost scripts]# ansible-playbook test_filevars.yaml

2、下发配置文件里面使用判断语法

  1. [root@localhost scripts]# cat /tmp/if.j2
  2. {% if PORT %}       #if PORT存在
  3. ip=0.0.0.0:{{ PORT }}
  4. {% else %}          #否则的话
  5. ip=0.0.0.0:80
  6. {% endif %}         #结尾
  7. [root@localhost scripts]# cat test_ifvars.yaml
  8. ---
  9. - hosts: all
  10. gather_facts: True    #开启系统内置变量
  11. vars:
  12. - PORT: 90        #自定义变量
  13. tasks:
  14. - name: jinja2 if test
  15. template: src=/tmp/if.j2 dest=/root/test
  16. [root@localhost scripts]# ansible-playbook test_ifvars.yaml

如果我们将变量PORT值为空的话,就会是另外的结果

  1. [root@localhost scripts]# cat test_ifvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True
  5. vars:
  6. - PORT:       #置空
  7. tasks:
  8. - name: jinja2 if test
  9. template: src=/tmp/if.j2 dest=/root/test
  10. [root@localhost scripts]# ansible-playbook test_ifvars.yaml

四、Playbook的notify通知和下发nginx配置

  1. #实战下发可执行动作的可变的nginx配置文件
  2. [root@localhost scripts]# head -1 /tmp/nginx.j2
  3. worker_processes  {{ ansible_processor_count }};    #可变的参数
  4. [root@localhost scripts]# cat test_nginxvars.yaml
  5. ---
  6. - hosts: all
  7. gather_facts: True    #开启系统内置变量
  8. tasks:
  9. - name: nginx conf
  10. template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
  11. notify:
  12. - reload nginx  #下发通知给handlers模块执行名字叫做reload nginx的动作
  13. handlers: #定义动作
  14. - name: reload nginx  #动作的名字
  15. shell: /usr/local/nginx/sbin/nginx -s reload
  16. [root@localhost scripts]# ansible-playbook test_nginxvars.yaml

Ansible运维自动化的更多相关文章

  1. 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 ...

  2. Ansible 运维自动化 ( 配置管理工具 )

    背景 出差背景,要搞项目的自动化部署.因为只直接对接生产分发,机器又非常多,这样以往使用的bat只能作为应急方案了,还是得考虑使用专业化的工具来做这个事情! 当下有许多的运维自动化工具( 配置管理 ) ...

  3. Ansible运维自动化工具

    1>Ansible 1>ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabri ...

  4. Centos7安装配置ansible运维自动化工具

    准备至少两台机器 Centos7,这两台机器都关闭 selinux IP:106.13.118.132 服务端(ansible) masterIP:148.70.60.244 节点 slaver 服务 ...

  5. Centos7搭建ansible运维自动化工具

    1)设置主机名和hosts文件 2)配置阿里云repo源 Wget -O /etc/yum.repos.d/aliyun.repo https://mirrors.aliyun.com/repo/Ce ...

  6. 运维自动化神器ansible之user模块

    运维自动化神器ansible之user模块 一.概述   user模块 可管理远程主机上的 用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 二.参数介绍   name: 用于指定操作 ...

  7. 运维自动化之ansible的安装与使用 转

    运维自动化之ansible的安装与使用 随着服务器数量的增长,我们需要一个批量工具去提高工作效率,之前用的是puppet,ansible的简单,适用让我眼前一亮,决定写一篇ansible从安装到基本配 ...

  8. 运维自动化之1 - ansible 批量主机管理

    2000 - 2016 年,维护的小型机.linux刚开始的2台增加到上千台,手工检查.日常版本升级需要管理太多设备,必须通过运维自动化实现 特别是版本升级,需要到同类机器部署代码.起停设备,必须在一 ...

  9. 运维自动化之salt笔记

    1:saltstack的基本介绍 2:salt的安装 1:服务端1:安装2:配置文件3:运行4:注意事项2:客户端1:安装2:配置文件3:运行4:注意事项 3:salt的使用: 1:基础知识1:tar ...

随机推荐

  1. RabbitMQ操作

    注意:在rabbitmq中,可以存在多个exchange,exchange只是负责接收消息,然后消息必须发送到给queue中,如果没有queue,消息就丢失了,exchange就相当于交换机,不负责存 ...

  2. 【Django】如何按天 小时等查询统计?

    代码: from django.db import connection from django.db.models import Sum,Count #alarm_sum_group_items = ...

  3. [ZZ] HD7970GE vs GTX770

    AMD/NV烽烟再起!HD7970GE大战GTX770 泡泡网显卡频道7月8日 高端市场肩负展示厂商实力,树立品牌形象的任务,历来是兵家必争之地.从GTX680 VS HD7970,HD7970GE ...

  4. PHP使用1个crontab管理多个crontab任务

    http://www.binarytides.com/php-manage-multiple-cronjobs-with-a-single-crontab-entry/ In many php app ...

  5. 得到python某个模块的路径

    #-*-coding:utf-8-*- # 导入imp模块 import imp # 打印出MySQLdb模块 print imp.find_module("MySQLdb")

  6. MySQL Connector Net连接vs2012问题

    最近做一.NET项目,数据库用到MySQL,可是在VS2012连接数据库是遇到问题,提示:Authentication with old password no longer supported, u ...

  7. cf591A Wizards' Duel

    A. Wizards' Duel time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. 单应性(homography)变换的推导

    矩阵的一个重要作用是将空间中的点变换到另一个空间中.这个作用在国内的<线性代数>教学中基本没有介绍.要能形像地理解这一作用,比较直观的方法就是图像变换,图像变换的方法很多,单应性变换是其中 ...

  9. maven 不能使用 snapshot 的解决方式

    最近项目需要用到snapshot的包来进行构建过程,但是怎么都下不了构建的snapshot包.查询了相关资料,发现网上的资料不全,特总结下: 我使用的是nexus来作为代理*库proxy. 检查步骤 ...

  10. LoadRuner12&period;53教程&lpar;二&rpar;

    使用HP Web访问示例应用程序 shǐ使   yòng用   H   P   W   e   b   fǎng访   wèn问   shì示   lì例   yìng应   yòng用   chén ...