Register a list of variables from file in ansible - bash

I have a file /tmp/components_list with a content like this:
ComponentA: '1263'
ComponentB: '989'
ComponentC: '1354'
I want to register variables in ansible (without quotes), according to the content of file and use them in the yml code.
So, as a result I need something like that:
- name: Get variables from file
Some actions with a file /tmp/components_list
- name: Using these variables
shell: docker run --name component artifactory:5100/radware/Component:{{ComponentA}}
So it should be a number in the variable ComponentA.
How can I do it by using ansible? Thanks!

You can use include_vars statement, see also How to include vars file in a vars file with ansible?

Related

How can I parse part of a URL from an variable?

I'm trying to write a playbook that will go and download the version of ombi I supply on the command line as a variable, then parse part of it so I can rename the file and keep a local copy of it. Then gunzip then untar then stop the service overwrite the existing app, then restart the service.
I've written several other playbooks but parsing this part out has me stumped.
So if say this was the URL
https://github.com/Ombi-app/Ombi/releases/download/v4.32.0/linux-x64.tar.gz
I want to extract the 4.32.0 out of that url. So my playbook run line might be something like:
ansible-playbook updateombi.yml --extra-vars "ombi_release=https://github.com/Ombi-app/Ombi/releases/download/v4.32.0/linux-x64.tar.gz"
I'm assuming I would declare a var like:
ombi_version: "{{ ombi_release | urlsplit('path') }}"
but the urlsplit is what's got me stumped. Anyone able to throw me a bone?
I'm trying to write a playbook that will go and download the version of Ombi I supply on the command line as a variable ...
To do so you could simply provide the version number only
ansible-playbook updateombi.yml --extra-vars "ombi_release=4.32.0"
and construct the URL and filename afterwards within your playbook
url: "https://github.com/Ombi-app/Ombi/releases/download/v{{ ombi_release }}/linux-x64.tar.gz"
dest: /tmp/linux-x64-v{{ ombi_release }}.tar.gz
since they don't have a variable part except the version number. By doing this there would be no need for
... then parse part of it so I can rename the file ...

Best way to migrate the ansible group_var file to a simple environment variables in shell script

I am trying to migrate from Ansible to a simple GitLab Helm installation. As a part of the ansible playbook, there was a group_var file containing a bunch of template variables which were replaced in the template files with ansible task. I am trying to find a better way to handle this in the shell script. Simple key values are easy to replace but it has some lists too.
Has anyone done anything similar ? I am leaning towards exporting them to environment variables and running envsubst command on the helm values files.
Sample file:
redis-url: url
search-host: host_name
search-port: 9090
rate_limit_external_gateway_max: 6
rate_limit_external_gateway_headers: true
rate_limit_external_gateway_test_mode: true
rate_limit_external_gateway_black_list_headers: [
X-RateLimit-Limit,
X-RateLimit-Remaining,
X-RateLimit-Reset
]
hosts:
- x.domain.com
- y.domain.com
- z.domain.com

How to detect an inventory environment in Ansible?

I have the requirement to skip some steps in my scripts when I run a deployment against production.
When a playbook is started, it always requires an environment (-i option), so there would be information I could query to distinguish which steps to take.
This leads me to ask:
How can I query the environment I am running a playbook in?
As an alternative, I could provide an extra variable as a parameter like -e "env=prod". But this would be redundant, since I have specified the environment already with -i...
Another option would be to set up a group environment, put all hosts of this environment in there, and define a group_var called env: prod. But putting all hosts in this group is overkill.
Bottom line: can I query the environment? Is there another option I'm not considering?
From Magic Variables in the Ansible documentation:
Also available, inventory_dir is the pathname of the directory holding Ansible’s inventory host file, inventory_file is the pathname and the filename pointing to the Ansible’s inventory host file.
Use string manipulation to extract the information you want from the above variable (e.g., the last segment from the path).
A filter exists to extract the last part of a pathname/filename :
managing-file-names-and-path-names
So you can use inventory_file | basename

Ansible: host_variables grouping in one file

I have a key with different value on each server(host), how can I save all those values in a single file, so that when my playbook is executed it reads from that file.
From Ansible documentation: I found under host_vars/hostname I have to create a file for each server and add the variable. It would be cumbersome if I have like 100 servers
You can set variables by host in your inventory file like this: https://docs.ansible.com/ansible/intro_inventory.html#host-variables
Groups and group_vars are another solution that may be fit your requirements.

Can 'extra_vars' receive multiple files?

According the Ansible documentation defining variables at runtime, it says I can load variables from a file.
ansible-playbook release.yml --extra-vars "#some_file"
However, in my case I have two files containing extra variables for my playbook invocation.
Concatenating them together is not an option because one is a secret file created and keyed using Vault. The other file is generated from an upstream process.
I have tried:
ansible-playbook release.yml --extra-vars "#some_file #some_other_file"
... but it didn't work. Upon invocation I get
ERROR: file could not read: some_file #some_other_file
so my guess is it takes everything after the first # symbols as the path of the file.
My questions is, can extra-vars accept multiple files?
It turns out I can use:
ansible-playbook release.yml --extra-vars=#some_file --extra-vars=#some_other_file
This does work for me. Please let me know if there is a better answer. Thanks.

Resources