代码之家  ›  专栏  ›  技术社区  ›  yoav.str

Ansible-在JSON和shell中使用外部变量

  •  -1
  • yoav.str  · 技术社区  · 8 年前

    我有一本ansible中的键值字典。 我想使用从用户那里得到的参数(开或关),并想在字典中记下他的值。

    我如何运行它:

     ansible-playbook -i $INVENTORY -s --extra-vars "switch='on' PLAYBOOKS/add_or_rm.yml 
    

    代码:

     - hosts: docker-first-manager
     vars:
    operationMap:
      on: add
      off: rm
    
    
     tasks:
      - name: open_kibana_debug_port
        shell:
           docker service update --publish-{{operationMap[switch]}} 5603:5603 my_service
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Phil M    8 年前

    我的第一反应是使用when条件。接下来的维护似乎更容易:

      - name: Publish kibana debug port
        shell: docker service update --publish-add 5603:5603 my_service
        when: switch is defined and switch == "on"
      - name: Un-publish kibana debug port
        shell: docker service update --publish-rm 5603:5603 my_service
        when: switch is not defined or switch != "on"