zoukankan      html  css  js  c++  java
  • 3. playbook基础组件

    Playbook
    playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。
    从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏。

    playbook基础组件
    Hosts和Users
    playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。
    hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;
    remote_user则用于指定远程主机上的执行任务的用户。
    如上面示例中的
    1
    2
        -hosts: webnodes
         remote_user:root
    不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;
     
    此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。
    1
    2
    3
    4
    5
    6
    7
    - hosts: webnodes
        remote_user: mageedu
        tasks:
        - name: test connection
          ping:
          remote_user: mageedu
          sudo: yes

    任务列表和action
    play的主体部分是task list。
    task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。
    在运行自下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。

    task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。

    每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出

    定义task的可以使用“action:module options”或“module: options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也中使用在行首使用几个空白字符进行换行。
    tasks:
    1
    2
        - name: make sure apache is running
          service:name=httpd state=running


    在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:
    1
    2
    3
        tasks:
         -name: disable selinux
          command: /sbin/setenforce 0


    如果命令或脚本的退出码不为零,可以使用如下方式替代:
    1
    2
    3
      tasks:
       - name: run this command and ignore the result
         shell: /usr/bin/somecommand || /bin/true


    或者使用ignore_errors来忽略错误信息:
    1
    2
    3
    4
    tasks:
      - name: run this command and ignore the result
        shell: /usr/bin/somecommand
        ignore_errors: True


    Inventory
    ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名。默认的inventory file为/etc/ansible/hosts。
    inventory文件格式:
    inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;
    此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
        ntp.xmfb.com

        [webservers]
        www1.xmfb.com:2222
        www2.xmfb.com

        [dbservers]
        db1.xmfb.com
        db2.xmfb.com
        db3.xmfb.com


    如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机,例如:
    1
    2
    3
    4
    5
    [webservers]
    www[01:50].example.com

    [databases]
    db-[a:f].example.com


    组嵌套
    inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。不过,这些变量只能在ansible-playbook中使用,而ansible不支持。例如:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [apache]
    httpd1.xmfb.com
    httpd2.xmfb.com

    [nginx]
    ngx1.xmfb.com
    ngx2.xmfb.com

    [webservers:children]
    apache
    nginx

    [webservers:vars]
    ntp_server=ntp.xmfb.com


    Handlers示例:定义配置文件发生修改之后,在执行yml文件,会重启httpd服务。
    定义的内容:
    <ignore_js_op style="word-wrap: break-word; margin: 0px; padding: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; widows: 1; background-color: rgb(255, 255, 255);"> 
    修改配置文件的监听端口为80
    1
    2
    [root@node1 ~]# vim conf/httpd.conf
    Listen 80

    执行结果
     
    验证:webserver组中的主机httpd端口更改与否
    1
    2
    [root@node2 ~]# netstat -lntp | grep httpd
    tcp       0      0 :::80                       :::*                        LISTEN      41848/httpd





  • 相关阅读:
    1451. Rearrange Words in a Sentence
    1450. Number of Students Doing Homework at a Given Time
    1452. People Whose List of Favorite Companies Is Not a Subset of Another List
    1447. Simplified Fractions
    1446. Consecutive Characters
    1448. Count Good Nodes in Binary Tree
    709. To Lower Case
    211. Add and Search Word
    918. Maximum Sum Circular Subarray
    lua 时间戳和时间互转
  • 原文地址:https://www.cnblogs.com/51runsky/p/4665888.html
Copyright © 2011-2022 走看看