Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Last active Dec 31, 2021
Embed
What would you like to do?
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