Git Product home page Git Product logo

ansible-append-list's Introduction

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

The examples make use of the builtin set_fact module.

How to run demonstration

ansible-playbook demo-append-list.yml

or (if the file is made executable)

./demo-append-list.yml

Example output

max@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

ansible-append-list's People

Contributors

betrcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.