简体   繁体   中英

Ansible run_once with_items

Trying to run this task in Ansible only on the first item in with_items. I am not able to change the cluster_server_names variable as it is used elsewhere. Do I need a separate task to register a new variable containing only the server name I want or is there another way possible?

   - 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)}}"

I'm afraid I do not have a way to test this, but try the first filter:

- 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 }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM