Skip to content

Ansible filter to have variable substitution in keys and values inside dictionaries

Notifications You must be signed in to change notification settings

cytopia/ansible-filter-get_attr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Ansible filter: get_attr

This filter overcomes Ansible's lack of variable substitution within dict keys.

Usage

# Just to set some variables
- set_fact:
    key1: "Name"
    val1: "my-name"
    key2: "tier"
    val2: "dev"

# Here we actually use variables
- set_fact:
    key_val:
      - key: "{{ key1 }}"
        val: "{{ val1 }}"
      - key: "{{ key2 }}"
        val: "{{ val2 }}"

# The filter will actually reorder the above array into a dictionary.
- debug:
    msg: "{{ key_val | get_attr('key', 'val') }}"

This will result in the following output:

{
  "key_val": {
    "Name": "my-name",
    "tier": "dev"
  }
}

Integration

Copy get_attr.py into your roles filter_plugins/ directory.

About

Ansible filter to have variable substitution in keys and values inside dictionaries

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages