0

尝试仅在 with_items 中的第一项上在 Ansible 中运行此任务。我无法更改 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)}}"
4

1 回答 1

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 }}"
于 2020-02-25T20:54:30.227 回答