【问题标题】:Ansible run_once with_itemsAnsible run_once with_items
【发布时间】:2020-02-25 20:44:46
【问题描述】:

尝试在 Ansible 中仅在 with_items 中的第一项上运行此任务。我无法更改 cluster_server_names 变量,因为它在其他地方使用。我是否需要一个单独的任务来注册一个只包含我想要的服务器名称的新变量,还是有其他可能的方法?

   - name: "Provision public IP in dev"
     uri:
       url: "{{ api_url }}/servers/{{ item }}/publicIPAddresses"
       headers:
         Authorization: "REMOVED"
         Content-Type: "application/json"
         Accept: "application/json"
       method: POST
       body_format: json
       status_code:
         - 200
         - 201
         - 202
       body:
         ports: [{"protocol":"TCP","port":"80"}]
     no_log: false
     register: blueprint
     run_once: true
     with_items:
       - "{{cluster_server_names |json_query(get_server_names)}}"

【问题讨论】:

    标签: post ansible runonce


    【解决方案1】:

    恐怕我没有办法对此进行测试,但请尝试使用first 过滤器:

    - name: "Provision public IP in dev"
      uri:
        url: "{{ api_url }}/servers/{{ server }}/publicIPAddresses"
        headers:
          Authorization: "REMOVED"
          Content-Type: "application/json"
          Accept: "application/json"
        method: POST
        body_format: json
        status_code:
          - 200
          - 201
          - 202
        body:
          ports: [{"protocol":"TCP","port":"80"}]
      no_log: false
      register: blueprint
      run_once: true
      vars:
        server: "{{cluster_server_names | json_query(get_server_names) | first }}"
    

    【讨论】:

    • @Tevor 太棒了!请为我点击接受答案! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多