ovh / the-bastion-ansible-wrapper

Using Ansible through The Bastion
https://ovh.github.io/the-bastion/
Apache License 2.0
35 stars 12 forks source link

feat: Add Ansible inventory cache #7

Closed jouir closed 2 years ago

jouir commented 2 years ago

Hello folks!

When using this SSH and SCP wrapper, even if BASTION_HOST, BASTION_PORT and BASTION_USER environment variables are defined, the ansible-inventory binary is called and takes 3 seconds each time. With a simple copy module, it can be called multiple times. It's not performant at all. The real Ansible inventory cache, defined by cache in the inventory section of the Ansible configuration, is ignored by the wrapper.

With this commit, there are two new optional environment variables:

If the BASTION_ANSIBLE_INV_CACHE_FILE environment variable is defined, the wrapper will save the inventory content to this file with an updated_at timestamp. The BASTION_ANSIBLE_INV_CACHE_TIMEOUT (default to 60 seconds) is used to remove the file based on the current time and the updated_at information (cache invalidation).

On our infrastructure, with the following playbook:

---
- hosts:
    - server_type_patroni
    - '&role_node'

  any_errors_fatal: true
  gather_facts: false

  tasks:
    - copy:
        content: "{{ ansible_managed | comment }}"
        dest: /tmp/jriou.txt
      become: true
      become_user: postgres

    - file:
        path: /tmp/jriou.txt
        state: absent

We have reduced the global execution time:

I wish you to have a nice day