Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Last active June 3, 2023 17:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebuildy/17454e32c357c6e8f4f86cded9b4dafe to your computer and use it in GitHub Desktop.
Save ebuildy/17454e32c357c6e8f4f86cded9b4dafe to your computer and use it in GitHub Desktop.
Ansible + argoCD API recipes
---
- tags: ["always"]
set_fact:
argocd_url: http://localhost:2369
api_headers: &api_headers
Authorization: "Bearer {{ lookup('env', 'ARGO_TOKEN') }}"
- name: refresh all applications
block:
- register: _result
uri:
url: "{{ argocd_url }}/api/v1/applications?refresh=true&project={{ argocd_project }}"
method: GET
headers: "{{ api_headers }}"
- debug: var="{{ _result.json['items'] }}"
- vars:
buffer: |
{% for item in _result.json['items'] %}
{{ item.metadata.name }}:
url: {{ argocd_url }}/applications/{{ item.metadata.name }}
sync: {{ item.status.sync.status }}
health: {{ item.status.health.status }}
{% endfor %}
set_fact:
buffer: "{{ buffer | from_yaml }}"
- debug:
var: buffer
- name: "sync argocd application {{ async_item }}"
tags: ["never", "sync"]
register: async_results
uri:
url: "{{ argocd_url }}/api/v1/applications/{{ async_item }}/sync"
method: POST
headers: "{{ api_headers }}"
async: 45
poll: 0
loop:
- app1
- app2
- app3
loop_control:
loop_var: "async_item"
- name: get results
tags: ["never", "sync"]
block:
- name: Check sync status
async_status:
jid: "{{ async_result_item.ansible_job_id }}"
loop: "{{ async_results.results }}"
loop_control:
loop_var: "async_result_item"
register: async_poll_results
until: async_poll_results.finished
retries: 30
- vars:
buffer: |
{% for result in async_poll_results.results %}
{{ result.json.metadata.name }}:
url: {{ argocd_url }}/applications/{{ result.json.metadata.name }}
sync: {{ result.json.status.sync.status }}
{% endfor %}
set_fact:
stdout : "{{ buffer | from_yaml }}"
- debug: var=stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment