Optimizing anssible code
Some ansible code can take a long time to run, this is in most cases due to a design choice in the playbook. Mostly when using loops where they are not needed, or loops to parse large datasets, this takes lots of time.
For a few of these optimizations there are things that can be done to improve performance:
Avoid large loops on registered vars
Create lists and dicts with jinja2
Avoid jinja spacing errors in linting
Creating complex loops using jinja2
Avoid host_key error when redeploying a host
When you use your infrastructure playbook to remove- and create a host with the same name (redeploy), the host key changes. Sometimes the play will fail after (re)creating the host with a host key error. To prevent this, use the following in the deployment play only:
- name: Configure the host on infrastructure
hosts: "{{ instances | default('localhost') }}"
gather_facts: false
vars:
ansible_ssh_host_key_checking: false
WARNING
Do not use this in your inventory, it reduces security for all hosts in the inventory.
If you just use this in your deployment play, you will be safe and the play won't fail
on this error.