Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/betrcode/ansible-append-list
How to append to a list in Ansible
https://github.com/betrcode/ansible-append-list
ansible ansible-playbook append
Last synced: about 1 month ago
JSON representation
How to append to a list in Ansible
- Host: GitHub
- URL: https://github.com/betrcode/ansible-append-list
- Owner: betrcode
- Created: 2016-10-19T19:40:11.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-21T13:55:15.000Z (almost 2 years ago)
- Last Synced: 2023-03-11T23:46:50.383Z (almost 2 years ago)
- Topics: ansible, ansible-playbook, append
- Homepage: https://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible
- Size: 6.84 KB
- Stars: 48
- Watchers: 2
- Forks: 22
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# How to append to a list in Ansible
In this repository you can find examples of how to append things to lists in Ansible.
The original blog post can be found at:
[https://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible](https://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible)The examples make use of the builtin [set_fact module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html).
## How to run demonstration
`ansible-playbook demo-append-list.yml`or (if the file is made executable)
`./demo-append-list.yml`
## Example output
```shellmax@max-x1:~/ansible-append-list$ ansible-playbook demo-append-list.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Append list to list, or merge two lists"
}TASK [Setup two lists to be merged] ************************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_one": [
1,
2,
3
]
}TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_two": [
4,
5,
6
]
}TASK [Merge the two lists] *********************************************************************************************************************************************************************************
ok: [localhost]TASK [Demonstrate merged lists] ****************************************************************************************************************************************************************************
ok: [localhost] => {
"lists_merged": [
1,
2,
3,
4,
5,
6
]
}TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Append/merge list of maps to list"
}TASK [Setup two lists to be merged] ************************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_one": [
{
"first_name": "John",
"last_name": "Doe"
},
{
"first_name": "Jane",
"last_name": "Doe"
}
]
}TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_two": [
{
"first_name": "Douglas",
"last_name": "Adams"
},
{
"first_name": "Frederic",
"last_name": "Bastiat"
}
]
}TASK [Merge the two lists of maps] *************************************************************************************************************************************************************************
ok: [localhost]TASK [Demonstrate merged lists] ****************************************************************************************************************************************************************************
ok: [localhost] => {
"lists_merged": [
{
"first_name": "John",
"last_name": "Doe"
},
{
"first_name": "Jane",
"last_name": "Doe"
},
{
"first_name": "Douglas",
"last_name": "Adams"
},
{
"first_name": "Frederic",
"last_name": "Bastiat"
}
]
}TASK [Initialize an empty list] ****************************************************************************************************************************************************************************
ok: [localhost]TASK [Setup a integer variable] ****************************************************************************************************************************************************************************
ok: [localhost]TASK [Append item to list] *********************************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"the_list": [
5
]
}TASK [Append another item to the list] *********************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"the_list": [
5,
6
]
}TASK [Initialize an empty list for our truths] *************************************************************************************************************************************************************
ok: [localhost]TASK [Setup a boolean variable] ****************************************************************************************************************************************************************************
ok: [localhost]TASK [Append boolean to list] ******************************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_thruths": [
true
]
}TASK [Append another boolean to the list] ******************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_thruths": [
true,
false
]
}TASK [Initialize an empty list for our strings] ************************************************************************************************************************************************************
ok: [localhost]TASK [Setup a string variable] *****************************************************************************************************************************************************************************
ok: [localhost]TASK [Append string to list] *******************************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_strings": [
"Max"
]
}TASK [Append another item to the list] *********************************************************************************************************************************************************************
ok: [localhost]TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_strings": [
"Max",
"Power"
]
}PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=31 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```## Ansible version
Demo built with Ansible version 2.6.5
Tested up to version 2.10.8