0

I have a playbook for some hosts which have group variables and host variables that use a custom lookup plugin to extract values from an API endpoint.

ansible_user: "{{ lookup(get_value_from_endpoint) }}"

One important information in the group variables pulled from this API endpoint is ansible_user, as shown in the example. The username is secured in the API endpoint for security purposes, so that the operator (person using ansible-playbook or Ansible Tower or AWX) would only need to access the API endpoint and so that the username is not committed to git.

There are also some more information being pulled from the API.

The problem is that the lookup plugin is executed on each task. So if I have 25 tasks in the play, there will be at least 25 API calls.

Is there any way to "cache" this variable so that it will persist through the execution of the play?

Cheers

2
0

In case you arent able to store that information in your inventory, but you want informations from one run of your playbook be available in the next run of it days later and maybe on different machines, you have the following options.

  • get the facts on the first run of you playbook and store it on your hosts directly (for example in a JSON file). Next run, you can check, if that file exists, read it and restore the information into a fact for this run.

If you only want to get the facts once per playbook run - create a task, that registeres the API output and just use that register, like

- uri:
    ..
  register: api_facts

- uri:
    api: "{{ api_facts.stdout }}"

Of course, you can reformat the output in api_facts and use set_fact to format it. So you only call the API once per playbook run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.