Ansible条件模块参数

时间:2016-04-26 08:41:15

标签: ansible ansible-playbook ansible-2.x

当某个条件有效时,是否可以只包含模块参数,而不重复播放?

示例:

我有一个看起来如下的游戏:

  - name: Start Container
    docker:
      name: "{{containerName}}"
      state: reloaded
      command: "java -jar {{containerImage}}-{{containerJarVersion}}.jar"

我想根据条件是否为false来更改命令参数的值?目前,我必须复制整个游戏并将其包装在一个条件中,这是可怕的,因为只有一个参数不同。

目前的解决方案:

  - name: Start Container Debug
    docker:
      name: "{{containerName}}"
      state: reloaded
      command: "java  -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address={{debugPort}} -jar {{containerImage}}-{{containerJarVersion}}.jar"
    when: ({{enableDebug}} == true)

  - name: Start Container
    docker:
      name: "{{containerName}}"
      state: reloaded
      command: "java -jar {{containerImage}}-{{containerJarVersion}}.jar"
    when: ({{enableDebug}} == false)

3 个答案:

答案 0 :(得分:2)

我认为你仍然需要在某个地方使用条件(你想根据最终的条件做不同的事情.. :)。

我唯一能想到的就是避免你两次写任务(导致所有烦人的skipped),你可以做到以下几点:

     # file: play.yml

     - include_vars: debug_vars.yml
        when: enableDebug == True

     - include_vars: prod_vars.yml
        when: enableDebug == False


     - name: Start Container
       docker:
          name: "{{containerName}}"
          state: reloaded
          command: "{{ start_container_command }}"



      # file: debug_vars.yml
        start_container_command: "java  -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address={{debugPort}} -jar {{containerImage}}-{{containerJarVersion}}.jar"

      # file: prod_vars.yml
        start_container_command: "java -jar {{containerImage}}-{{containerJarVersion}}.jar"

答案 1 :(得分:1)

你可以像这样使用它:

  - name: Start Container Debug
    docker:
      name: "{{containerName}}"
      state: reloaded
      command: "java  -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address={{debugPort}} -jar {{containerImage}}-{{containerJarVersion}}.jar"
    when: enableDebug # Will run when the value of  eanbleDebug is True

  - name: Start Container
    docker:
      name: "{{containerName}}"
      state: reloaded
      command: "java  -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address={{debugPort}} -jar {{containerImage}}-{{containerJarVersion}}.jar"
    when: not enableDebug # Will run when the value of  eanbleDebug is False

我认为eanbleDebug将是这样的:

eanbleDebug: True
or 
eanbleDebug: False

希望这会对你有所帮助

答案 2 :(得分:1)

(现在)您可以使用此内联条件机制

start_container_command: "java {{'-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address={{debugPort}}' if not enableDebug else '' }} -jar {{containerImage}}-{{containerJarVersion}}.jar"