zoukankan      html  css  js  c++  java
  • ansible使用playbook的简单例子(ansible2.9.7)

    一,ansible使用playbook的优点

    1,用ansible执行一些简单的任务,使用ad-hoc命令就可以解决问题

      如果执行复杂的功能,需要大量的操作,执行的ad-hoc命令会不够方便,这时我们选择使用playbook。

     使用playbook你可以方便的重用代码,可以移植到不同的机器上面

       可以像函数一样,最大化的复用代码。

      如果把常见的操作都编写成playbook,之后管理服务器会变得十分简单

    2,playbook使用YMAL语言编写

          YMAL语言的格式:

       文件的第一行以 "---" (三个连字符)开始,表示是YMAL文件的开始。
      在同一行中,#之后的内容表示注释
      YMAL中的列表元素以”-”开头然后紧跟着一个空格,后面为元素内容
      同一个列表中的元素应该保持相同的缩进,否则会被当做错误处理
      play中hosts/variables/roles/tasks等对象的表示方法都是键值中间以": "分隔表示(":"后面还有一个空格)

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/

    说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,例子:编写一个简单的playbook

    功能:安装screen

    #hosts:指定要操作的hosts,如果是所有机器,可以用all

    #remote_user: 登录用的用户

    #become:是否要切换用户

    #become_user:要切换到的用户

    #tasks:要执行的任务

    #command: 用command模块执行命令

    #说明:用dnf安装软件其实应该用dnf模块,我们这里只是做一个演示

    [root@centos8 playbook]# vi installscreen.yml 

    代码:

    ---
    - hosts: yujian
      remote_user: webop            
      become: yes
      become_user: root
      tasks:
       - name: "安装screen"
         command: dnf install --quiet -y screen

    测试执行:

    [liuhongdi@centos8 playbook]$ ansible-playbook installscreen.yml 
    
    PLAY [yujian] ******************************************************************************************
    TASK [Gathering Facts] *********************************************************************************
    
    TASK [安装screen] ***************************************************************************************
    [WARNING]: Consider using the dnf module rather than running 'dnf'.  
    If you need to use command because dnf is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. changed: [121.122.123.47] PLAY RECAP ********************************************************************************************* 121.122.123.47 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    说明:用dnf安装软件应该使用dnf模块

             ansible给出了一个warning,

             我们这里只做为演示,不用管它

    服务器端检查是否安装成功?

    [root@blog ~]$ whereis screen
    screen: /usr/bin/screen /usr/share/screen /usr/share/man/man1/screen.1.gz /usr/share/info/screen.info.gz

    安装成功了

    三,playbook参数的使用例子:

    1,语法检查 

    [root@centos8 installscreen]# ansible-playbook  --syntax-check installscreen.yml

    2,列出涉及到的host

    [root@centos8 installscreen]# ansible-playbook  --list-host installscreen.yml 

    3,检查会发生的改变,但不是真的执行命令

    [root@centos8 installscreen]# ansible-playbook --check installscreen.yml 

    4,列出tasks

    [root@centos8 playbook]# ansible-playbook  --list-tasks installscreen.yml

    四,例子: 使用playbook执行script并查看执行结果

    脚本的功能是发布git分支

    1,脚本:

    [liuhongdi@centos8 script]$ vi gitpubwww.sh 

    内容

    cd /data/web/think_www;
    echo "---------------------------------------git status:
    ";
    git status;
    echo "---------------------------------------git pull:
    ";
    git pull origin master;
    echo "---------------------------------------git status:
    ";
    git status;

    2,发布git分支的playbook

    #register: 注册一个变量,用来存储返回的结果

    #debug:启用debug模块,把输出结果显示出来

    [root@centos8 playbook]# vi gitpubwww.yml 

    内容

    ---
    - hosts: yujian
      remote_user: webop            
      become: yes
      become_user: root
      tasks:
       - name: "www项目发布git分支"
         script: /data/ansible/script/gitpubwww.sh
         register: gitpub_out
       - name: "查看结果"
         debug: var=gitpub_out verbosity=0

    3,执行playbook

    [liuhongdi@centos8 playbook]$ ansible-playbook gitpubwww.yml

    五,查看ansible的版本

    [liuhongdi@centos8 ~]$ ansible --version
    ansible 2.9.7
      config file = /etc/ansible/ansible.cfg
      configured module search path = ['/home/liuhongdi/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python3.6/site-packages/ansible
      executable location = /usr/bin/ansible
      python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]
  • 相关阅读:
    controller中返回值是string
    String jsonstr =new Gson().toJson(object) 什么意思
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path jsp开始页面有红叉
    ubuntu 安装配置JDK
    检查元素状态
    检查页面元素是否存在
    智能等待页面元素(显示的等待同步测试)
    implicitly_wait()隐式等待
    多选框处理
    单选按钮处理
  • 原文地址:https://www.cnblogs.com/architectforest/p/12888246.html
Copyright © 2011-2022 走看看