Coder Social home page Coder Social logo

Comments (2)

Hickory420 avatar Hickory420 commented on July 17, 2024 1

in vultr we implemented 2 types: inventory plugin and module (task plugin).

🤯

Thank you very much for pointing me in the right direction. I would like to avoid setting the environment variable for the VULTR_API_KEY as I would like to keep it encrypted within ansible.

This is what I ended up with (for now)

.
├── roles
│   └── common
│       ├── tasks
│       │   └── main.yaml
│       └── vars
│           └── main.yaml
└── site.yaml

roles/common/tasks/main.yaml

---
- name: Setting Vars
  ansible.builtin.include_vars: main.yaml

- name: Gather Vultr regions information
  vultr.cloud.region_info:
    api_key: "{{ api_key }}"
  register: region

- name: Print syd region information
  ansible.builtin.debug:
    var: "{{ region }}"
  loop: "{{ region.vultr_region_info }}"
  when:
    - item.country == "AU"
    - item.id == "syd"

roles/common/vars/main.yaml

---
plugin: vultr.cloud.vultr
api_key: abc123
compose:
  ansible_host: vultr_main_ip

site.yaml

- name: Main Play
  hosts: localhost
  roles:
    - { role: common, tags: always }

And I call it with

ansible-playbook site.yaml

I can confirm the api_key variable gets carried across multiple roles.

Thank you once again for you help.

from ansible-collection-vultr.

resmo avatar resmo commented on July 17, 2024

Hi @Hickory420

Ansible has a few plugin types, in vultr we implemented 2 types: inventory plugin and module (task plugin).

The config in ./vultr.yaml, e.g.

plugin: vultr.cloud.vultr
api_key: your key

is only for the inventory plugin, further you would need to enable this plugin in your ansible.cfg in your project roots direcotory:

[inventory]
enable_plugins = host_list, script, auto, yaml, ini, toml, vultr.cloud.vultr

To test it, you could run:

ansible-inventory -i vultr.yml --list

and you should get a list of all vultr instances, to test connection, you have to tell the plugin how to connect to the instances if you don't want to use the label name as a DNS target (this is in the docs of the plugin ansible-doc -t inventory vultr.cloud.vultr)

plugin: vultr.cloud.vultr
api_key: your key
compose:
  ansible_host: vultr_main_ip

after that you will be able to "ping" (ssh into your VMs and test connection) using:

ansible -m ping -i vultr.yml -u root all 

If you want to use the modules to create instances, see plans, regions and so on, the API key must be provided again (https://docs.ansible.com/ansible/latest/collections/vultr/cloud/region_info_module.html#parameter-api_key) to this plugin type. This could be achieved in few different ways (not vultr specific) but one way would be to put it into a group_vars variable and pass it in the tasks as an argument:

---
- name: View the regions
  hosts: localhost
  vars:
    # this would go into group_vars/all.yml
     vultr_api_key: my key
  tasks:
    - name: Gather Vultr regions information
      vultr.cloud.region_info:
         api_key: "{{ vultr_api_key }}"
      register: region

    - name: Print the gathered information
        ansible.builtin.debug:
        var: result.vultr_region_info

NOTE: Another way to provide the vultr api key to all plugin types is using an environment variable:

$ export VULTR_API_KEY=my key
ansible-inventory -i vultr.yml --list

I hope my explanation solved the issue.

from ansible-collection-vultr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.