Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cytopia/ansible-filter-get_attr
Ansible filter to have variable substitution in keys and values inside dictionaries
https://github.com/cytopia/ansible-filter-get_attr
ansible ansible-filters
Last synced: 12 days ago
JSON representation
Ansible filter to have variable substitution in keys and values inside dictionaries
- Host: GitHub
- URL: https://github.com/cytopia/ansible-filter-get_attr
- Owner: cytopia
- Created: 2018-06-26T15:27:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-27T07:48:12.000Z (over 6 years ago)
- Last Synced: 2024-12-27T13:03:22.454Z (15 days ago)
- Topics: ansible, ansible-filters
- Language: Python
- Size: 3.91 KB
- Stars: 6
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ansible filter: get_attr
This filter overcomes Ansible's lack of variable substitution within dict keys.
* Issue: https://github.com/ansible/ansible/issues/17324
## Usage
```yaml
# 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:
```json
{
"key_val": {
"Name": "my-name",
"tier": "dev"
}
}
```## Integration
Copy `get_attr.py` into your roles `filter_plugins/` directory.