Ansible Unable to assign variable the value to another variable - variables

If the user does not pass dest_path parameter or if dest_path is EMPTY i.e contains only whitespaces then dest_path should be the same as source_file value.
Below is my playbook:
---
- name: "Play 1"
hosts: localhost
any_errors_fatal: false
gather_facts: false
tasks:
- set_fact:
dest_path: "{{ dest_path | default(source_file) }}"
- set_fact:
dest_path: "{{ source_file }}"
when: dest_path | length == 0
- debug:
msg: "DESTINATION PATH IS: {{ dest_path }} and the SOURCE PATH is: {{ source_file }}"
This is how you run this playbook:
ansible-playbook -i /web/playbooks/allmwhosts.hosts /web/playbooks/test.yml -e '{ source_file: /web/bea_apps/CURRENT }' -e dest_path=
In the above example when the user does not specify any value for dest_path I m expecting dest_path should be source_file i.e /web/bea_apps/CURRENT
However, as you can see in the output below that is not the case:
Output:
PLAY [Play 1] *******************
TASK [set_fact] **************************************************************************************
ok: [localhost]
TASK [set_fact] **************************************************************************************
ok: [localhost]
TASK [debug] *****************************************************************************************
ok: [localhost] => {
"msg": "DESTINATION PATH IS: and the SOURCE PATH is: /web/bea_apps/CURRENT"
}
PLAY RECAP *******************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
Can you please suggest?

The extra variable that you pass as parameter is overriding the variable in set_fact variable dest_path. Below is the working code. In set_fact instead of dest_path I replaced with path.
---
- name: "Play 1"
hosts: localhost
any_errors_fatal: false
gather_facts: false
tasks:
- set_fact:
path: "{{ source_file }}"
when: (dest_path is not defined) or (dest_path | length == 0)
- set_fact:
path: "{{ dest_path }}"
when: (dest_path is defined) or (dest_path | length != 0)
- debug:
msg: "DESTINATION PATH IS: {{ path }} and the SOURCE PATH is: {{ source_file }}"

Related

extract password as variable

new to ansible ..
Trying to get this result:
variable tgt_wls_pwd = the result of env1.wls_pwd = 1234
variable tgt_apps_pwd = the result of env1.apps_pwd = 5678
The unencrypted password file vault.yml
env1:
wls_pwd: 1234
apps_pwd: 5678
Playbook
ansible-playbook tgt-app-stop.yml --extra-vars="target_clone=env1"
- name: Stop Application Tier(s) process
hosts: " {{ target_clone }}-app01"
any_errors_fatal: true
remote_user: ansible
become: yes
become_user: install
roles:
- oraapp-stop
vars_files:
- vault.yml
tasks:
- set_fact:
target_clone: "{{ target_clone }}"
vars:
# tgt_wls_pwd: "{{ target_clone }}||{{ wls_pwd }}"
# tgt_apps_pwd: "{{ target_clone }}||{{ apps_pwd }}"
# tgt_wls_pwd: "{{ target_clone ['{{ wls_pwd }}'] }}"
# tgt_apps_pwd: "{{ target_clone ['{{ apps_pwd }}'] }}"
tgt_wls_pwd: "{{ target_clone.wls_pwd }}"
tgt_apps_pwd: "{{ target_clone.apps_pwd }}"
I've tried quite a few permutations
target_clone is an extra variable passed to the playbook when running.
Thanks.
You'll need the vars lookup plugin. See
shell> ansible-doc -t lookup vars
For example, given the file
shell> cat vault.yml
env1:
wls_pwd: 1234
apps_pwd: 5678
and the inventory
shell> cat hosts
env1-app01
The playbook
shell> cat tgt-app-stop.yml
- hosts: "{{ target_clone }}-app01"
gather_facts: false
vars_files:
- vault.yml
vars:
tgt_wls_pwd: "{{ lookup('vars', target_clone).wls_pwd }}"
tgt_apps_pwd: "{{ lookup('vars', target_clone).apps_pwd }}"
tasks:
- debug:
msg: |
tgt_wls_pwd: {{ tgt_wls_pwd }}
tgt_apps_pwd: {{ tgt_apps_pwd }}
gives
shell> ansible-playbook tgt-app-stop.yml -e "target_clone=env1"
PLAY [env1-app01] ****************************************************************************
TASK [debug] *********************************************************************************
ok: [env1-app01] =>
msg: |-
tgt_wls_pwd: 1234
tgt_apps_pwd: 5678
PLAY RECAP ***********************************************************************************
env1-app01: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

How can I print out the actual values of all the variables used by an Ansible playbook?

An answer on StackOverflow suggests using - debug: var=vars or - debug: var=hostvars to print out all the variables used by an Ansible playbook.
Using var=hostvars did not print out all of the variables. But I did get all of the variables printed out when I added the following lines to the top of the main.yml file of the role executed by my playbook:
- name: print all variables
debug:
var=vars
The problem is that the values of the variables printed out are not fully evaluated if they are dependent on the values of other variables. For example, here is a portion of what gets printed out:
"env": "dev",
"rpm_repo": "project-subproject-rpm-{{env}}",
"index_prefix": "project{{ ('') if (env=='prod') else ('_' + env) }}",
"our_server": "{{ ('0.0.0.0') if (env=='dev') else ('192.168.100.200:9997') }}",
How can I get Ansible to print out the variables fully evaluated like this?
"env": "dev",
"rpm_repo": "project-subproject-rpm-dev",
"index_prefix": "project_dev",
"our_server": "0.0.0.0",
EDIT:
After incorporating the tasks section in the answer into my playbook file and removing the roles section, my playbook file looks like the following (where install-vars.yml contains some variable definitions):
- hosts: all
become: true
vars_files:
- install-vars.yml
tasks:
- debug:
msg: |-
{% for k in _my_vars %}
{{ k }}: {{ lookup('vars', k) }}
{% endfor %}
vars:
_special_vars:
- ansible_dependent_role_names
- ansible_play_batch
- ansible_play_hosts
- ansible_play_hosts_all
- ansible_play_name
- ansible_play_role_names
- ansible_role_names
- environment
- hostvars
- play_hosts
- role_names
_hostvars: "{{ hostvars[inventory_hostname].keys() }}"
_my_vars: "{{ vars.keys()|
difference(_hostvars)|
difference(_special_vars)|
reject('match', '^_.*$')|
list|
sort }}"
When I try to run the playbook, I get this failure:
shell> ansible-playbook playbook.yml
SSH password:
SUDO password[defaults to SSH password]:
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [192.168.100.111]
TASK [debug] *******************************************************************
fatal: [192.168.100.111]: FAILED! => {"failed": true, "msg": "lookup plugin (vars) not found"}
to retry, use: --limit #/usr/local/project-directory/installer-1.0.0.0/playbook.retry
PLAY RECAP *********************************************************************
192.168.100.111 : ok=1 changed=0 unreachable=0 failed=1
The minimal playbook below
shell> cat pb.yml
- hosts: localhost
gather_facts: false
vars:
test_var1: A
test_var2: "{{ test_var1 }}"
tasks:
- debug:
var: vars
reproduces the problem you described. For example,
shell> ansible-playbook pb.yml | grep test_var
test_var1: A
test_var2: '{{ test_var1 }}'
Q: How can I print out the actual values of all the variables used by an Ansible playbook?
A: You can get the actual values of the variables when you evaluate them. For example,
shell> cat pb.yml
- hosts: localhost
gather_facts: false
vars:
test_var1: A
test_var2: "{{ test_var1 }}"
tasks:
- debug:
msg: |-
{% for k in _my_vars %}
{{ k }}: {{ lookup('vars', k) }}
{% endfor %}
vars:
_special_vars:
- ansible_dependent_role_names
- ansible_play_batch
- ansible_play_hosts
- ansible_play_hosts_all
- ansible_play_name
- ansible_play_role_names
- ansible_role_names
- environment
- hostvars
- play_hosts
- role_names
_hostvars: "{{ hostvars[inventory_hostname].keys() }}"
_my_vars: "{{ vars.keys()|
difference(_hostvars)|
difference(_special_vars)|
reject('match', '^_.*$')|
list|
sort }}"
gives the evaluated playbook's vars
msg: |-
test_var1: A
test_var2: A
Looking for an answer to the same question, I found the following solution from this link:
- name: Display all variables/facts known for a host
debug:
var: hostvars[inventory_hostname]
tags: debug_info

Retrieve variable values from var file in ansible playbook

Need some help to retrieve child values from var file.
Below is my var file
api_user: root
api_password: !vault |
$ANSIBLE_VAULT;1.2;AES256;isi33835326238346338613761366531376662613865376263656262
6138
Huston:
onefs_host: 10.88.55.00
Phoenix:
onefs_host: 10.76.52.01
Below is my playbook
---
- name: isi_increase
hosts: localhost
connection: local
vars_files:
- isilonvars.yml
tasks:
- name: Print
debug:
msg:
- "{{ Huston.onefs_host }}"
- "{{ api_user }}"
- "{{ api_password }}"
This code works perfectly
TASK [Print] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"10.88.55.00",
"root",
"Sh00tm3!"
]
}
But as per my requirement i have to retrieve onefs_host IP as per location in my playbook. I am using extra vars here -e "location=Huston"
- name: Print
debug:
msg:
# - "{{ wcc.onefs_host }}"
- "{{ {{location}}.onefs_host }}"
- "{{ api_user }}"
- "{{ api_password }}"
I am getting the below error.
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: {{ {{isilon_location}}.onefs_host }}"}
Can you try this way
- name: Print
debug:
msg:
- "{{ vars[location]['onefs_host'] }}"
- "{{ api_user }}"
- "{{ api_password }}"

Build a variable in a playbook with the hostname and call it in a task

I have a two main variable with subvariables. The main variable VELABx and VELABy match the ansible_hostnames. Following an example of the variables mentioned:
VELABx
location: "text1.1"
initialism: "text1.2"
VELABy
location: "text2.1"
initialism: "text2.2"
Now I'd like to run a task which replaces a regexp with the location variable based on the current ansible_hostname. So, I tried this code, but it leads into an error.
- name: Replace location for VELABx
replace:
dest: "/path/to/file"
regexp: 'location'
replace: "{{ {{ansible_hostname}}.location }}"
remote_user: rssuser
become: no
when: ansible_hostname == "VELABx"
What am I doing wrong? How can I solve this? Can anyone bring up a solution?
Things are always easier when you manage to create a proper structure of data. For example, given the dictionaries in a file
shell> cat velab.yml
VELABx:
location: "text1.1"
initialism: "text1.2"
VELABy:
location: "text2.1"
initialism: "text2.2"
you can include the data from the file into a dictionary, e.g.
- hosts: VELABx,VELABy,VELABz
tasks:
- include_vars:
file: velab.yml
name: velab
run_once: true
- debug:
var: velab
run_once: true
gives
velab:
VELABx:
initialism: text1.2
location: text1.1
VELABy:
initialism: text2.2
location: text2.1
The data you provided in the question are not YAML dictionaries. The colon is missing behind the keys. Either fix it, if you can or parse the file. Anyway, create the dictionary whatever is the source.
Then, given the files
shell> ssh admin#10.1.0.61 cat /tmp/velab.txt
location
shell> ssh admin#10.1.0.62 cat /tmp/velab.txt
location
shell> ssh admin#10.1.0.63 cat /tmp/velab.txt
location
the task to replace the location is simple
- name: Replace location for VELAB*
replace:
dest: /tmp/velab.txt
regexp: location
replace: "{{ velab[inventory_hostname]['location'] }}"
when: velab[inventory_hostname] is defined
gives
shell> ansible-playbook playbook.yml -CD
PLAY [VELABx,VELABy,VELABz] ******************************************
...
TASK [Replace location for VELAB*] ***********************************
skipping: [VELABz]
--- before: /tmp/velab.txt
+++ after: /tmp/velab.txt
## -1 +1 ##
-location
+text1.1
changed: [VELABx]
--- before: /tmp/velab.txt
+++ after: /tmp/velab.txt
## -1 +1 ##
-location
+text2.1
changed: [VELABy]
The next option is to remove the condition and use the default value, instead of skipping the host, when the host is missing in the dictionary
- name: Replace location for VELAB*
replace:
dest: /tmp/velab.txt
regexp: location
replace: "{{ velab[inventory_hostname]['location']|
default(default_location) }}"
vars:
default_location: "text99.1"
you must use loopkup, {{}} inside other {{}} is incorrect
---
- hosts: localhost
gather_facts: false
vars:
ansible_hostname: VELABx
VELABx:
location: "text1.1"
initialism: "text1.2"
VELABy:
location: "text2.1"
initialism: "text2.2"
tasks:
- name: DEBUG
debug:
msg: "{{ lookup('vars', ansible_hostname ).location }}"
- name: Replace location for VELABx
replace:
dest: "/path/to/file"
regexp: 'location'
replace: "{{ lookup('vars', ansible_hostname ).location }}"
when: ansible_hostname == "VELABx"

Using extra-var to call a defined variable in ansible

Is this possible? I have a playbook looking like this:
vars:
BDNAME: ""
- name: Add a tenant using a JSON string
aci_bd:
tenant: "common"
bd: "{{ BDNAME }}"
vrf: "PIGGE"
hostname: '1.1.1.1'
username: "x"
password: "x"
use_ssl: yes
validate_certs: false
It works if i provide an extra variable in the commandline:
ansible-playbook apic.yml -i server.yml --extra-vars BDNAME='pooh'
Then BDNAME gets the value pooh.
But is there any way that i can define pooh as a variable. So if i run the playbook like i just did, BDNAME get the value of that variable.
So something like
vars:
BDNAME: ""
POOH: nisse
Then BDNAME should be nisse.
Define BDNAME in playbook directly from the extra variable POOH. That should do what you want. But it would be easier to use POOH instead of BDNAME.
Here is a example playbook:
---
- hosts: localhost
vars:
BDNAME: "{{ POOH }}"
tasks:
- name: print BDNAME
debug:
msg: "{{ BDNAME }}"
if you call it with:
ansible-playbook playbook.yml -e '{"POOH": "Oliver"}'
you will see:
TASK [print BDNAME] **********************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": "Oliver"
}