Getting correct value of variable - ansible

I do loop over results of get_url task which is registered in a variable, lets call it var. When I try to get get value from within the loop the content of an item looks like:
['u(/tmp/path'),'Undefined']
The question is - how to get the correct value of an item? The value is /tmp/path. Why is an item in loop looking like an array?

According How to get the first element of a list from the output? you may use just
- name: Show first list element only
debug:
msg: "{{ var[0] }}"
To remove the u'' from the list output is an other question for which you can find answers here in StackOverflow already.

Related

Ansible regex_replace replace multiple characters in URI

Long story short -
Task 1:
Through URI module I am querying Website to obtain json data. Output is saved to variable.
Task 2:
Json_search is used to filter data.
But result value comes with square brackets which I need to get rid off as it needs to be inserted into url address.
I am using not elegant replace module twice to solve this problem. This works but I more than sure you can engage regex_replace (or json_query in more intelligent way) to achieve same result.
Expected output: https://site1/api/host/1
Received output before using replace 2x:
https://site1/api/host/[1]
Snippet from playbook:
…
- name: display url
debug:
msg: {{ json_output_var.json|json_search('result[].id')|replace('[','')|replace(']','')

How to remove every part, except the end from elements in a list in ansible?

I have a problem to edit remove every part, except the end, of every elements in a list.
The list looks like this at the beginning:
['/dev/sda1', '/dev/sdb1', '/dev/sdc1', '/mnt/xxx/yyy']
Now i want to edit it, so i only have the last part of every element left.
So the list should be look like this at the end:
['/sda1', '/sdb1', '/sdc1', '/yyy']
I wanted to loop through this list and edit every element but i don't know how i can regex this.
Does anybody has an idea?
Thanks for everybody who look at this!
The task below does the job
- debug:
msg: "{{ my_list|map('regex_replace', my_regex, my_replace)|list }}"
vars:
my_list: ['/dev/sda1', '/dev/sdb1', '/dev/sdc1', '/mnt/xxx/yyy']
my_regex: '^.*/(.*)$'
my_replace: '/\1'
gives
msg:
- /sda1
- /sdb1
- /sdc1
- /yyy

Ansible split returning multiple indexes

I'm trying to use split with Ansible to return 2 different indexes, in the example below (which doesn't work) let's say I want to set my_split to 'ad':
my_string: "a-b-c-d"
my_split: "{{ my_string.split('-')[0,3]|join() }}"
All documentation I can find only shows examples returning 1 index and I can't find any references to what I'm trying to achieve
Q: Set my_split to 'ad'
A: The tasks
- set_fact:
my_split: "{{ [0,3]|map('extract',my_string.split('-'))|join() }}"
- debug:
var: my_split
give
"my_split": "ad"
The problem is the selection of the first and fourth elements of the sequence. The expression below
my_string.split('-')[0,3]
fails
The error was: list object has no element (0, 3)
Instead, it's possible to use map and extract. See Extracting values from containers.

ansible. Run handler on specific host

I need to collect some information during run of ansible and print this information in the end.
I've tried to define empty list variable in role. I added in playbook handler which add new value to list ant print value of this variable in the end of playbook.
set_fact:
manual_tasks: "{{ manual_tasks + ['restart apache'] }}"
I miss that set_fact host related. So this solution stop working as soon as I start using different hosts. Delegate_to is not solve problem as well. Is there way to make this list global? Or any other solution exist?
Q: Add new value to list and print it at the end of the playbook. Is there way to make this list global?
A: No. It is not. "Global scope is set by config, environment variables and the command line." A host can't set_fact in the play or global scope.
You might want to take a look at ansible-runner. See Sending Runner Status and Events to External Systems.

Ansible YAML attribute reading [duplicate]

I am trying to make sense of a variable reference I found in an incomplete Ansible role. The role references a value using
dest: “{{params['box'].t1}}”
In a separate yaml file I have
box:
t1: "Albany"
t2: "Albuquerque"
params isn't defined, so obviously this isn't going to work, but I can't figure out the correct way to define it. Can someone tell me where (or how) params must be defined for this variable reference to work in Ansible?
Related questions. Does the use of square brackets in dest: “{{params['box'].t1}}” indicate that it is a dictionary? If yes, could I also write this as dest: “{{params['box']['t1']}” or dest: “{{params.box.t1}”?
params['box'].t1 refers to Albany in:
params:
box:
t1: "Albany"
t2: "Albuquerque"
It is the same as params.box.t1 and params['box']['t1'].
Brackets refer to a key name, so they imply it is a dictionary.
You typically use square bracket-notation when you want to refer to a key via a variable:
vars:
wanted_key: box
params:
box:
t1: Albany
other:
t1: Albuquerque
Then params[wanted_key].t1 refers to Albany.
In your example the value inside square brackets is a string (quoted), so all above examples are equivalent.

Resources