Generating sequences with coma seperated values - ansible

I'm trying to achieve something similar like cisco range commandos with ansible. in my case, I have tried the following:
- name: test playbook
hosts: all
connection: local
gather_facts: no
vars:
epg: "{{ lookup('env','EPG').split(',') }}"
task:
- debug:
var:(item|int)
with_sequence_ "{{ epg }}"
the input will be made by the user and can be something like: 10-13,20
in this case what I want to have just like the cisco IOS console : 10,11,12,13,20
instead, I get the following output:
TASK [debug] *******************************************************************
task path: /opt2/jenkins/TEST/TEST interface.yaml:42
ok: [tst] => (item=10) => {
"(item|int)": "10",
"ansible_loop_var": "item",
"item": "10"
}
ok: [tst] => (item=11) => {
"(item|int)": "11",
"ansible_loop_var": "item",
"item": "11"
}
ok: [tst] => (item=12) => {
"(item|int)": "12",
"ansible_loop_var": "item",
"item": "12"
}
ok: [tst] => (item=13) => {
"(item|int)": "13",
"ansible_loop_var": "item",
"item": "13"
}
ok: [tst] => (item=1) => {
"(item|int)": "1",
"ansible_loop_var": "item",
"item": "1"
}
ok: [tst] => (item=2) => {
"(item|int)": "2",
"ansible_loop_var": "item",
"item": "2"
}
ok: [tst] => (item=3) => {
"(item|int)": "3",
"ansible_loop_var": "item",
"item": "3"
... (so on up to 20)
How can I get only 10,11,12,13 and 20 if somebody can help me I will be glad. Thank you in advance.

you have to combine a list of jinja2 and Ansible filters. It's not easy but try this:
- name: test playbook
hosts: all
vars:
epg: "{{ lookup('env','EPG')}}"
task:
- debug:
msg: "{% set LIST_EPG=[] %}{% for it in (epg.split(',')) %}{% set int1=it.split('-')[0] | int() %}{% if it.split('-') |length > 1 %}{% set int2=it.split('-')[1] | int() %}{% for i in range(int1, int2+1) %}{{ LIST_EPG.append(i|int) }}{% endfor %}{% endif %}{{ LIST_EPG.append(int1| int) }}{% endfor %}{{LIST_EPG|flatten|unique|join(',') }}"

By splitting the comma separated list in EPG environment variable, you are creating an array/list in epg. So we need to iterate over this.
Consider environment variable as:
export EPG="10,11,12,13,20"
And reusing your epg variable:
vars:
epg: "{{ lookup('env', 'EPG').split(',') }}"
tasks:
- debug:
msg: "{{ item }}"
loop: "{{ epg }}"
We can get this result:
ok: [localhost] => (item=10) => {
"msg": "10"
}
ok: [localhost] => (item=11) => {
"msg": "11"
}
ok: [localhost] => (item=12) => {
"msg": "12"
}
ok: [localhost] => (item=13) => {
"msg": "13"
}
ok: [localhost] => (item=20) => {
"msg": "20"
}

Related

ansible: how to parse output of an Oracle script (JSON_OBJECT)

There is an Oracle script that runs this query:
select json_object (name, value) from v$parameter where lower(name) in ...
output of this script:
{"name":"processes","value":"1000"}
{"name":"sessions","value":"1522"}
{"name":"sga_target","value":"3221225472"}
{"name":"control_file_record_keep_time","value":"7"}
{"name":"db_block_size","value":"8192"}
{"name":"compatible","value":"19.0.0.0.0"}
...
I register stdout_lines during sql-script execution and then parse line by line:
- name: print fetched database parameters
debug:
msg: "{{ db_params_out.stdout_lines }}"
- name: create list of newdb parameters from fetched result
debug:
msg: "{{ item }}"
with_list: "{{ db_params_out.stdout_lines }}"
when: db_params_out.stdout_lines | length > 0
Output looks good to me:
TASK [db_standby_preinstall : print fetched database parameters] *********************************************************************************************
ok: [localhost -> ol79_1] => {
"msg": [
"",
"{\"name\":\"processes\",\"value\":\"1000\"}",
"{\"name\":\"sessions\",\"value\":\"1522\"}",
"{\"name\":\"sga_target\",\"value\":\"3221225472\"}",
"{\"name\":\"control_file_record_keep_time\",\"value\":\"7\"}",
"{\"name\":\"db_block_size\",\"value\":\"8192\"}",
"{\"name\":\"compatible\",\"value\":\"19.0.0.0.0\"}",
...
TASK [db_standby_preinstall : create list of newdb parameters from fetched result] ***************************************************************************
ok: [localhost -> ol79_1] => (item=) => {
"msg": ""
}
ok: [localhost -> ol79_1] => (item={'name': 'processes', 'value': '1000'}) => {
"msg": {
"name": "processes",
"value": "1000"
}
}
ok: [localhost -> ol79_1] => (item={'name': 'sessions', 'value': '1522'}) => {
"msg": {
"name": "sessions",
"value": "1522"
}
}
ok: [localhost -> ol79_1] => (item={'name': 'sga_target', 'value': '3221225472'}) => {
"msg": {
"name": "sga_target",
"value": "3221225472"
}
}
ok: [localhost -> ol79_1] => (item={'name': 'control_file_record_keep_time', 'value': '7'}) => {
"msg": {
"name": "control_file_record_keep_time",
"value": "7"
}
}
ok: [localhost -> ol79_1] => (item={'name': 'db_block_size', 'value': '8192'}) => {
"msg": {
"name": "db_block_size",
"value": "8192"
}
}
ok: [localhost -> ol79_1] => (item={'name': 'compatible', 'value': '19.0.0.0.0'}) => {
"msg": {
"name": "compatible",
"value": "19.0.0.0.0"
but I cannot address key/value of a fetched item, ansible fails with The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'name' (or 'value') if i try to use item.name or item.value instead of item.
P.S. I can modify Oracle script and provide output in any format. I need to parse output and assign values to corresponding variables:
newdb_processes:1000
newdb_sessions:1522
etc...
Issue is solved. The problem was in the first line of stdout_lines which is empty but still passes via filter
when: db_params_out.stdout_lines | length > 0
I've removed this line but now I use "reject" filter in loop:
with_list: "{{ db_params_out.stdout_lines | reject('match', '^$') }}"
And it works now, no more empty output lines.
Thanks a lot user larsks who pointed me to a test-case with correct data to prove that his solution works properly.
You can pass each line through the from_json filter to get a structured object, like this:
- hosts: localhost
gather_facts: false
vars:
db_params_out:
stdout_lines:
- '{"name":"processes","value":"1000"}'
- '{"name":"sessions","value":"1522"}'
- '{"name":"sga_target","value":"3221225472"}'
- '{"name":"control_file_record_keep_time","value":"7"}'
- '{"name":"db_block_size","value":"8192"}'
- '{"name":"compatible","value":"19.0.0.0.0"}'
tasks:
- name: create list of newdb parameters from fetched result
debug:
msg: "{{ (item|from_json).name }}"
with_list: "{{ db_params_out.stdout_lines }}"
when: db_params_out.stdout_lines | length > 0
The above playbook outputs:
TASK [create list of newdb parameters from fetched result] **********************************************
ok: [localhost] => (item={'name': 'processes', 'value': '1000'}) => {
"msg": "processes"
}
ok: [localhost] => (item={'name': 'sessions', 'value': '1522'}) => {
"msg": "sessions"
}
ok: [localhost] => (item={'name': 'sga_target', 'value': '3221225472'}) => {
"msg": "sga_target"
}
ok: [localhost] => (item={'name': 'control_file_record_keep_time', 'value': '7'}) => {
"msg": "control_file_record_keep_time"
}
ok: [localhost] => (item={'name': 'db_block_size', 'value': '8192'}) => {
"msg": "db_block_size"
}
ok: [localhost] => (item={'name': 'compatible', 'value': '19.0.0.0.0'}) => {
"msg": "compatible"
}

How to compare lists in Ansible?

I have created 2 lists in Ansible:
{{actual_event_list}}
task1
task2
task3
task4
task5
{{expected_event_list}}
completed
completed
completed
completed
completed
I want to compare both lists in a way that if "task1" == "completed" then it's a success, else it should fail. Same goes for all items (task2 == completed, task3 == completed and so on...).
Try map. There might be more options on the filter. For example replace
shell> cat map-replace.yml
- hosts: localhost
vars:
actual_event_list:
- "{{ task1|default('running') }}"
- "{{ task2|default('running') }}"
- "{{ task3|default('running') }}"
tasks:
- debug:
msg: "{{ actual_event_list|
map('replace', 'completed', 'true')|map('bool')|
list }}"
vars:
task1: completed
task3: completed
gives
msg:
- true
- false
- true
I would first combine the list to get a dict and then loop over it. Here is an example with an assert.
playbook.yml:
---
- name: Stackoverflow demo
hosts: localhost
tasks:
- name: Set facts
set_fact:
actual_event_list:
- task1
- task2
- task3
- task4
- task5
expected_event_list:
- completed
- completed
- completed
- completed
- completed
# https://docs.ansible.com/ansible/latest/user_guide/complex_data_manipulation.html#id10
- name: Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists
set_fact:
events: "{{ events | default({}) | combine({item[0]: item[1]}) }}"
loop: "{{ (actual_event_list | zip(expected_event_list)) | list }}"
- name: Assert
assert:
that: item.value == "completed"
msg: "{{ item.key }} has status `{{ item.value }}` instead of `completed`"
loop: "{{ events | dict2items }}"
Output:
PLAY [Stackoverflow demo] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Set facts] **********************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists] **************************************************************************************************************************************************************
ok: [localhost] => (item=['task1', 'completed'])
ok: [localhost] => (item=['task2', 'completed'])
ok: [localhost] => (item=['task3', 'completed'])
ok: [localhost] => (item=['task4', 'completed'])
ok: [localhost] => (item=['task5', 'completed'])
TASK [Assert] *************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'task1', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task1",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task2', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task2",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task3', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task3",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task4', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task4",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task5', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task5",
"value": "completed"
},
"msg": "All assertions passed"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
Output when something goes wrong:
PLAY [Stackoverflow demo] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Set facts] **********************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists] **************************************************************************************************************************************************************
ok: [localhost] => (item=['task1', 'completed'])
ok: [localhost] => (item=['task2', 'completed'])
ok: [localhost] => (item=['task3', 'completed'])
ok: [localhost] => (item=['task4', 'not_completed'])
ok: [localhost] => (item=['task5', 'completed'])
TASK [Assert] *************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'task1', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task1",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task2', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task2",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task3', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task3",
"value": "completed"
},
"msg": "All assertions passed"
}
failed: [localhost] (item={'key': 'task4', 'value': 'not_completed'}) => {
"assertion": "item.value == \"completed\"",
"changed": false,
"evaluated_to": false,
"item": {
"key": "task4",
"value": "not_completed"
},
"msg": "task4 has status `not_completed` instead of `completed`"
}
ok: [localhost] => (item={'key': 'task5', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task5",
"value": "completed"
},
"msg": "All assertions passed"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=1
You can later enhance that script making sure the list have the same size.

ansible 2.9.2 getting skip_reason: "Conditional result was False" even when meeting my conditional string

I am trying to install some RPMs, only when variables are defined.
but I am getting skip messages even if the variable was defined.
I have tried the three different when conditions and all were skipped.
here is my task:
- name: Install elasticsearch packages
package: name={{ package.item }} state=installed
when: ansible_distribution == item.when
with_items:
- { package: "logstash-{{ logstash_ver }}", when: (logstash_ver is defined) and (logstash_ver|length > 0) }
- { package: "kibana-{{ kibana_ver }}", when: ( inventory_hostname == kibana_ver ) }
- { package: "elasticsearch-{{ elasticsearch_ver }}", when: ( host_enviroment == elasticsearch_ver ) }
Here is the message I am getting:
skipping: [host1] => (item={u'when': u'(logstash_ver is defined) and (logstash_ver|length > 0)', u'package': u'logstash-7.9.0-1'}) => {"ansible_loop_var": "item", "changed": false, "item": {"package": "logstash-7.9.0-1", "when": "(logstash_ver is defined) and (logstash_ver|length > 0)"}, "skip_reason": "Conditional result was False"}
skipping: [host1] => (item={u'when': u'( inventory_hostname == kibana_ver )', u'package': u'kibana-7.9.1-1'}) => {"ansible_loop_var": "item", "changed": false, "item": {"package": "kibana-7.9.1-1", "when": "( inventory_hostname == kibana_ver )"}, "skip_reason": "Conditional result was False"}
skipping: [host1] => (item={u'when': u'( host_enviroment == elasticsearch_ver )', u'package': u'elasticsearch-7.8.1-1'}) => {"ansible_loop_var": "item", "changed": false, "item": {"package": "elasticsearch-7.8.1-1", "when": "( host_enviroment == elasticsearch_ver )"}, "skip_reason": "Conditional result was False"}
at the end, I should install about 15 rpms, so I really want to use with_items with the when condition.
Any Idea how to do that ?
Thanks

Use two with_items loops in one Task

I did quite some research on this question. Though i did not find the answer to solve my problem.
I want to delete a directorys content with ansibe with deleting the directory itself. I want to do this for multiple directorys.
In theory i want to do something like this:
- name: Delete dir on Prod/Stag
file:
state: "{{ item.1 }}"
path: "{{ /path/ }}{{ item.2 }}/"
with_items.1:
- absent
- directory
with_items.2:
- test1
- test2
- test3
- test4
Sadly this does not work.
This is what i have right now.
- name: Delete dir
file:
state: absent
path: "{{ path }}{{ item }}/"
with_items:
- test1
- test2
- test3
- test4
Is there a way to make this code shorter by creating two loops?
You want with_nested:
- debug:
msg: "state: {{ item.0 }}; path: {{ item.1 }}"
with_nested:
- [ absent, directory ]
- [ sys, wifi, reco-properties, threshold-prod ]
Results in:
TASK [debug] *******************************************************************
ok: [localhost] => (item=[u'absent', u'sys']) => {
"msg": "state: absent; path: sys"
}
ok: [localhost] => (item=[u'absent', u'wifi']) => {
"msg": "state: absent; path: wifi"
}
ok: [localhost] => (item=[u'absent', u'reco-properties']) => {
"msg": "state: absent; path: reco-properties"
}
ok: [localhost] => (item=[u'absent', u'threshold-prod']) => {
"msg": "state: absent; path: threshold-prod"
}
ok: [localhost] => (item=[u'directory', u'sys']) => {
"msg": "state: directory; path: sys"
}
ok: [localhost] => (item=[u'directory', u'wifi']) => {
"msg": "state: directory; path: wifi"
}
ok: [localhost] => (item=[u'directory', u'reco-properties']) => {
"msg": "state: directory; path: reco-properties"
}
ok: [localhost] => (item=[u'directory', u'threshold-prod']) => {
"msg": "state: directory; path: threshold-prod"
}
https://docs.ansible.com/ansible/2.4/playbooks_loops.html#nested-loops

Loops inside inventory files?

Is it possible to use loops in the inventory files? For example in one inv file I got:
---
ISPs:
- name: ISP1
- name: ISP2
networks:
- name: network1
- name: network2
- name: network3
and then I want to create something like this in this same inventory:
from ISP1 to network1 permit
from ISP1 to network2 permit
from ISP1 to network3 permit
from ISP2 to network1 permit
from ISP2 to network2 permit
from ISP2 to network3 permit
but I think that using loops I could do it faster instead of copy-paste and write down all of the possible combinations. Is it even possible?
example:
from {{ item[0] }} to {{ item[1] }} permit
with_nested:
- [ 'ISP1', 'ISP2' ]
- [ 'network1', 'network2', 'network3' ]
will create something like this:
from ISP1 to network1 permit
from ISP1 to network2 permit
from ISP1 to network3 permit
from ISP2 to network1 permit
from ISP2 to network2 permit
from ISP2 to network3 permit
Thanks for any answer!
here is 2 variants,
see https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-nested-with-cartesian
---
- hosts: localhost
connection: local
gather_facts: false
vars:
ISPs:
- name: ISP1
- name: ISP2
networks:
- name: network1
- name: network2
- name: network3
tasks:
- name: Using with_nested
debug:
msg: '{{ item.0.name }} - {{item.1.name }} '
with_nested:
- '{{ ISPs }}'
- '{{ networks }}'
- name: Using loop
debug:
msg: '{{ item.0.name }} - {{item.1.name }} '
loop: "{{ ISPs | product(networks) | list }}"
Output:
PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [Using with_nested] ***********************************************************************************************************************************************************************************
ok: [localhost] => (item=[{u'name': u'ISP1'}, {u'name': u'network1'}]) => {
"msg": "ISP1 - network1 "
}
ok: [localhost] => (item=[{u'name': u'ISP1'}, {u'name': u'network2'}]) => {
"msg": "ISP1 - network2 "
}
ok: [localhost] => (item=[{u'name': u'ISP1'}, {u'name': u'network3'}]) => {
"msg": "ISP1 - network3 "
}
ok: [localhost] => (item=[{u'name': u'ISP2'}, {u'name': u'network1'}]) => {
"msg": "ISP2 - network1 "
}
ok: [localhost] => (item=[{u'name': u'ISP2'}, {u'name': u'network2'}]) => {
"msg": "ISP2 - network2 "
}
ok: [localhost] => (item=[{u'name': u'ISP2'}, {u'name': u'network3'}]) => {
"msg": "ISP2 - network3 "
}
TASK [Using loop] ******************************************************************************************************************************************************************************************
ok: [localhost] => (item=[{u'name': u'ISP1'}, {u'name': u'network1'}]) => {
"msg": "ISP1 - network1 "
}
ok: [localhost] => (item=[{u'name': u'ISP1'}, {u'name': u'network2'}]) => {
"msg": "ISP1 - network2 "
}
ok: [localhost] => (item=[{u'name': u'ISP1'}, {u'name': u'network3'}]) => {
"msg": "ISP1 - network3 "
}
ok: [localhost] => (item=[{u'name': u'ISP2'}, {u'name': u'network1'}]) => {
"msg": "ISP2 - network1 "
}
ok: [localhost] => (item=[{u'name': u'ISP2'}, {u'name': u'network2'}]) => {
"msg": "ISP2 - network2 "
}
ok: [localhost] => (item=[{u'name': u'ISP2'}, {u'name': u'network3'}]) => {
"msg": "ISP2 - network3 "
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
It is possible to do, but applying login inside ansible code will be messy, So better idea is to invoke any bash or python script in ansible & pass these two list as an argument to that script & return the expected array from the script at the end & store this value to fact using set_fact
Example:
tasks:
- set_fact: isp_networks="{{ lookup('pipe','python script.py ' + {{ISPs}} + ' ' + {{networks}}) }}"
- debug: var={{isp_networks}}

Resources