Apologies for this simple question, but I tried various approach without success.
This is my vars file
---
preprod:
name: nginx
prod:
name: apache
I am trying to pass the value of name
based on the environment name user provides (preprod, prod etc).
This is my template
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: {{ env.name }}
name: {{ env.name }}
namespace: default
spec:
selector:
matchLabels:
app: {{ env.name }}
template:
metadata:
labels:
app: {{ env.name }}
spec:
containers:
- image: {{ env.name }}
imagePullPolicy: Always
name: {{ env.name }}
resources: {}
However, when I try with this using following command:
ansible-playbook playbook.yaml -e env=preprod
I am getting the following error.
fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'str object' has no attribute 'name'"}
My expectation is the {{ env.name }}
should have been replaced with the value of preprod.name
as in nginx
in this case.
I want users to provide the value for env
via -e
on the command line, it seems if I do like preprod.name
directly on the template, it seems to work, but I don’t want that.
I hope this clarifies what I am trying to do, but it didn’t work.
May I know what I am missing?
2
Answers
This error message indicates that the extra var passed on command line as
-e
is a string, and not the key (we expect) of the dict we are loading from the vars file.I’m making up an example playbook as you have not shown how you load your vars file. I’m using
include_vars
as we can name the variable to load dict into.With this approach, the
prod
andpreprod
keys will be available undertestvars
, and can be referenced with a variable such asenv
.Then the template should use
_name
variable, like:Given the variables in a place where the play can find it, e.g. group_vars/all. Optionally add default_env
Use vars lookup plugin to
"Retrieve the value of an Ansible variable".
SeeFor example, the playbook
by default displays
Now you can select the environment by declaring the variable my_env, .e.g
and