Ansible: how to convert list into integer

ansible convert string to int
ansible map
ansible convert ansibleunsafetext to int
ansible flatten
ansible convert string to numeric
ansible regex_findall
ansible ternary operator
ansible int filter not working

i have the following json output

"lc_facts": {
        "changed": false, 
        "failed": false, 
        "launch_configurations": [
            {
                "block_device_mappings": [
                    {
                        "device_name": "/dev/sda1", 
                        "ebs": {
                            "delete_on_termination": true, 
                            "volume_size": 40, 
                            "volume_type": "gp2"
                        }
                    }
                ]
            }
       ]
  }

The query below

- debug:
    msg: "{{ lc_facts.launch_configurations|json_query('[*].block_device_mappings[0].ebs.[volume_size]') | flatten }}"

gives

    "msg": [
    [40]
    ]

i want to convert into an integer so i can use this value. i tried everything, but all i want is to get number only without brackets. i also added |int in the end, but it gives me 0 value, which is weird.

please help, i am really in trouble.

Brackets create a list. "[[40]]" is a list of lists with one element "40". The play below

vars:
  var1: [[40]]
tasks:
  - debug: var=var1
  - debug: var=var1[0]
  - debug: var=var1[0][0]

gives:

"var1": [
    [
        40
    ]
]

"var1[0]": [
    40
]

"var1[0][0]": "40"

Using the Jinja2 filter "| int" to convert a string to an integer in PB , ISSUE TYPE Bug Report COMPONENT NAME Ansible Filters (based off of Jinja2 Using the Jinja2 filter "| int" to convert a string to an integer in PB doesn't work. The non_working_main.yml is the main.yml file in the tasks of the role name: define consul server list set_fact: CONSUL_LOCAL_CONFIG:� Use the vlan_parser filter to manipulate an unsorted list of VLAN integers into a sorted string list of integers according to IOS-like VLAN list rules. This list has the following properties: Vlans are listed in ascending order. Three or more consecutive VLANs are listed with a dash. The first line of the list can be first_line_len characters long.

try taking the first element of the list with index [0]. Do it twice if the list is inside another list:

for msg = [40] use msg[0]

for msg = [[40]] use msg[0][0]

Mapping strings into numbers in Ansible, Our range is a very special — it's a list of… So, our goal is to create a hash function from arbitrary string into fixed range of integers. Moreover� Our range is a very special — it’s a list of non-privileged non-dynamic TCP ports [1024–49152]. So, our goal is to create a hash function from arbitrary string into fixed range of integers.

def dict_to_obj(obj, data):
    for d in data:
        if isinstance(data[d], dict):
            setattr(obj, str(d), obj.__class__())
            dict_to_obj(getattr(obj, str(d)), data[d])
        else:
            setattr(obj, str(d), data[d])
    return obj

class Empty:pass
obj = Empty()
obj = dict_to_obj(obj,Your_JSON_object)
#Now here you get all json attributes as objects e.g.
#to get volume_size : 
print(obj.lc_facts.launch_configurations.block_device_mappings.ebs.volume_size)
# but for this to work, you need to restructure your json nested key-value pairs like 
# this :-
"launch_configuration" : {....}
# instead of this :-
"launch configuration" : [ {....} ]

I hope this helps.

sequence – generate a list based on a number sequence — Ansible , Use Jinja2 filters to convert items to preferred type, e.g. {{ 1 + item|int }} . See also number of elements in the sequence, this is not to be used with end. end. Convert list variable to comma separated list in ansible. To convert list variable such as: arguments: [ "value1", "value2", "value3" ] to a command separate value in a template file use:

Filters, To turn a dictionary into a list of items, suitable for looping, use dict2items : Use the vlan_parser filter to manipulate an unsorted list of VLAN integers into a� Filters in Jinja2 are a way of transforming template expressions from one kind of data into another. Jinja2 ships with many of these. See builtin filters in the official Jinja2 template documentation. In addition to those, Ansible supplies many more.

Ansible Generate an array of numbers, But what you really wanted is a way of generating a list of integers in a Jinja2 template: - set_fact: mysequence: "{{ range(1, 22 + 1) | list }}". For this you need to convert the integer to string first. converting from potential text or bytes at the beginning of a Ways to provide body payload in Ansible's URI module [Update] Out of all Google searched I did, yours nailed down my issue. Note that you can convert a List of any object type into a string this way. facts.

Add quotes to elemens of the list in jinja2 (ansible), Ansible convert list to string Use the vlan_parser filter to manipulate an unsorted list of VLAN integers into a sorted string list of integers according to IOS- like� Returned Facts ¶. Facts returned by this module are added/updated in the hostvars host facts and can be referenced by name just like any other host fact. They do not need to be registered in order to use them.

Comments
  • what does this number represent ? is this the volume size ? and is your json structure always the same ?
  • it is the volume size. for this query, it will be same. but it can be changed
  • what can be changed?
  • huge respect for you. i have no words to say thank you. excellent explanation as well.
  • Thank you for code. this will be helpful when i will try some python.