Learning Ansible for multi-server prod deploy on DO - dependencies questions

Hi all, I have a few basic questions while working on a deployment to DigitalOcean droplet(s) with Ansible.

  1. If I want to use the DigitalOcean Cloud collection, does the collection need to be in the same directory as my playbooks vs. in my global config Ansible files? If so, can I use a requirements.yml file including this collection to ensure it’s installed in the right place, or do this manually?

  2. How does the built in lookup plugin find environment variables - is it just what I set in my shell environment during development? If so, how does it find anything if on a cloud hosted server? (Where should I put my environment variable keys and values so they can be looked up?)

Thanks for any input!

Also - would a vars.yml playbook addition be how to use the lookup plugin? Or do those not go together?

Edit: Not great to be putting these keys as plaintext in YAML files I think, so I suppose vars isn’t the answer.

Update: After more endless docs reading, I think this answers my second question.

For collections, use ansible-galaxy to install the collections.
You can either install it manually with ansible-galaxy collection install digitalocean.cloud, but it’s best to specify requirements file to it’s checked into source control.

collections:
  - name: digitalocean.cloud

Then ansible-galaxy install -r requirements.yml


Lookup plugins will always execute on the controller node, hence the env lookup plugin will have same environment you run ansible from.

To get remote environment variables, you can use ansible facts:

- hosts: localhost
  gather_facts: true
  tasks:
    - ansible.builtin.debug:
        msg: "{{ ansible_facts['env'] }}"

Can you clarify what do you mean about the vars.yml?

For reference, heres the docs about different ways to set variables:

1 Like