当uri模块的条件时,ansible-playbook

时间:2018-03-19 15:34:30

标签: ansible

我有ansbile剧本代码:

- name: Send file to server
  uri:
    url: "{{ url }}:{{ port }}/application/{{ item | basename }}"
    method: POST
    body: "{{ lookup('file','{{ item }}') }}"
    force_basic_auth: yes
    status_code: 201
    body_format: json
  with_fileglob: "{{ tmp_dir)_for_files }}/configs/*"
  register: code_result
  until: code_result.status == 201
  retries: "{{ uri_retries }}"
  delay: "{{ uri_delays }}"

我需要实现的是,只有当文件尚未存在时才会上传到服务器。

1 个答案:

答案 0 :(得分:0)

以下是解决此问题的方法

- name: Get file from server
  uri:
    url: "{{ url }}:{{ port }}/application/{{ item | basename }}"
    method: GET
    body: "{{ lookup('file','{{ item }}') }}"
    force_basic_auth: yes
    status_code: 404
    body_format: json
  with_fileglob: "{{ tmp_dir }}/configs/*"
  register: get_code_result
  ignore_errors: true

- name: Send file to server
  uri:
    url: "{{ url }}:{{ port }}/application/{{ item.item | basename }}"
    method: POST
    body: "{{ lookup('file','{{ item.item }}') }}"
    force_basic_auth: yes
    status_code: 201
    body_format: json
  with_items:
      - "{{  get_code_result.results }}"
  when: item.status == 404
  register: code_result
  until: code_result.status == 201
  retries: "{{ uri_retries }}"
  delay: "{{ uri_delays }}"

似乎正在运作