Ansible playbook 根据条件动态设置变量


  1. 首先新建 inventory,主机列表如下:

    node-01 ansible_host=192.168.64.30
    node-02 ansible_host=192.168.64.31 [app]
    node-01 [db]
    node-02
  2. 然后新建一个 test.yml,新增如下内容:

    - hosts: db[0]:app[0]   # 支持索引取值
    vars:
    - Role: "{{ 'db' if 'app' in group_names else 'app' }}" # 动态设置变量
    - Ports:
    app: 4503
    db: 4502 pre_tasks: # 默认tasks只能在roles执行完后才执行,而pre-tasks可以在roles之前执行
    - name: Debug
    debug:
    msg: This is a test message. roles:
    - testrole
  3. 新建 roles/testrole/defaults/main.yml

    ---
    RESTLink: 'http://{{ inventory_hostname }}:{{ Ports[Role] }}'
  4. 新建 roles/testrole/tasks/main.yml

    ---
    - debug:
    msg: "Final URL is {{ RESTLink }}"
  5. 执行该 playbook

    ansible-playbook -i inventory test.yml

    输出结果是:


    PLAY [db[0]:app[0]] ******************************************************************************************************************************************* TASK [Gathering Facts] ****************************************************************************************************************************************
    Tuesday 27 August 2019 18:22:30 +0800 (0:00:00.121) 0:00:00.121 ********
    ok: [node-01]
    ok: [node-02] TASK [debug] **************************************************************************************************************************************************
    Tuesday 27 August 2019 18:22:32 +0800 (0:00:02.242) 0:00:02.363 ********
    ok: [node-02] => {
    "msg": "This is a test message."
    }
    ok: [node-01] => {
    "msg": "This is a test message."
    } TASK [testrole : Debug] ***************************************************************************************************************************************
    Tuesday 27 August 2019 18:22:33 +0800 (0:00:00.255) 0:00:02.620 ********
    ok: [node-02] => {
    "msg": "Final URL is http://node-02:4503"
    }
    ok: [node-01] => {
    "msg": "Final URL is http://node-01:4502"
    } PLAY RECAP ****************************************************************************************************************************************************
    node-01 : ok=3 changed=0 unreachable=0 failed=0
    node-02 : ok=3 changed=0 unreachable=0 failed=0

总结:

1. When used in this manner, the order of execution for your playbook is as follows:

  • Any pre_tasks defined in the play.
  • Any handlers triggered so far will be run.
  • Each role listed in roles will execute in turn. Any role dependencies defined in the roles meta/main.yml will be run first, subject to tag filtering and conditionals.
  • Any tasks defined in the play.
  • Any handlers triggered so far will be run.
  • Any post_tasks defined in the play.
  • Any handlers triggered so far will be run.

Ansible实践总结的更多相关文章

  1. Nebula Graph 的 Ansible 实践

    本文首发于 Nebula Graph 公众号 NebulaGraphCommunity,Follow & 看大厂图数据库技术实践 背景 在 Nebula-Graph 的日常测试中,我们会经常在 ...

  2. ansible实践4- 管理配置文件

    生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下.下面我们来写个管理nginx配置文件的playbook   mkdir  -p /etc/ansible/nginx_c ...

  3. ansible实践3-playbook条件判断

    --- - hosts: testhost   user: root   gather_facts: True   tasks:     - name: use when       shell: t ...

  4. ansible实践2-拷贝文件或目录

      ansible testhost -m copy -a "src=/etc/ansible  dest=/tmp/ansibletest owner=root group=root mo ...

  5. ansible实践-1

      不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook 支持sudo 有提供UI(浏览器图形化)www.ans ...

  6. ansible实践

    ansible常用module ansible-doc -l List available modules -s Show playbook snippet for specified module( ...

  7. Ansible - 简介和应用自动化基础实践

    installAnsible简介和应用自动化基础实践 一.引入: 1.1  如官方定义,Ansible is The simplest way to automate apps and IT infr ...

  8. 《Ansible自动化运维:技术与佳实践》第二章读书笔记

    Ansible 安装与配置 本章主要讲的是 Ansible 安装与基本配置,主要包含以下内容: Ansible 环境准备 安装 Ansible 配置运行环境 Ansible 环境准备 从 GitHub ...

  9. ansible服务部署与使用

    第1章 ssh+key实现基于密钥连接(ansible使用前提) 说明:    ansible其功能实现基于SSH远程连接服务    使用ansible需要首先实现ssh密钥连接 1.1 部署ssh ...

随机推荐

  1. 关键字static、final

    final final能修饰类.修饰方法.能修饰属性. 修饰类:该类不能被继承. 修饰方法:该方法不能被重写.所以abstract和final不能同时用 修饰属性/变量:该属性/变量为常量,该值不能再 ...

  2. Anacodna之conda的使用

    yum install -y bunzip2 wget https://repo.continuum.io/archive/Anaconda2-5.0.1-Linux-x86_64.sh chmod ...

  3. Http接口调用示例教程

    介绍HttpClient库的使用前,先介绍jdk里HttpURLConnection,因为HttpClient是开源的第三方库,使用方便,不过jdk里的都是比较基本的,有时候没有HttpClient的 ...

  4. spring解析配置文件(三)

    一.从XmlBeanDefinitionReader的registerBeanDefinitions(doc,resource)开始 protected int doLoadBeanDefinitio ...

  5. @Validated和@Valid的区别?校验级联属性(内部类)

    每篇一句 NBA里有两大笑话:一是科比没天赋,二是詹姆斯没技术 相关阅读 [小家Java]深入了解数据校验:Java Bean Validation 2.0(JSR303.JSR349.JSR380) ...

  6. Shiro权限框架与SpringMVC集成

    1.Shiro整合SpringMVC 我们学习Shiro框架肯定是要应用到Web项目上的,所以我们需要整合Shiro和SpringMVC 整合步骤: 第一步:SpringMVC框架的配置 spring ...

  7. Tomcat发布War包或者Maven项目

    在tomcat的conf目录下面的server.xml中修改如下: Host name="localhost"  appBase="webapps" unpac ...

  8. 基于python的Elasticsearch索引的建立和数据的上传

    这是我的第一篇博客,还请大家多多指点 Thanks ♪(・ω・)ノ         今天我想讲一讲关于Elasticsearch的索引建立,当然提前是你已经安装部署好Elasticsearch. ok ...

  9. LR有的JMeter也有之一“参数化”

    酝酿了几天,一直想写点JMeter的东西,算是对学习东西的一个整理.:) 恩,一直觉得自己领悟能力不强,别人写的东西总要看老半天也不懂.好吧!一惯的傻瓜的方式(大量的截图+参数说明)嘻嘻. 参数化:简 ...

  10. Samba:基于公网 IP 的服务访问

    写在前面的话 由于使用过程中,发现如果 Samba 只用于内网访问,同事在外面甚至其它不是一个网段的同事就无法访问了.这显然不符合我们最终的需求,最后没法,只能把访问部署到云服务器上面去,此时问题来了 ...