Get up to 50% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

Ansible Convert List to String

Ansible Convert List to String

I noticed many users are using different and wrong methods to convert a list data to string format. You can use existing filters and functions in Ansible to achieve the same.

Here is our list

vars:
    my_list:
      - "apple"
      - "mango"
      - "orange"

Now we need to convert this to a single string with comma separated (or separated by any other character). I found users are using loops and adding to a string value but there is a simple way to do this as shown below.

- name: Concatenate a list to string
      set_fact:
        my_string: "{{ my_list | join(',') }}"

Find the full playbook below.

---
- name: List to String Conversion
  hosts: localhost
  vars:
    my_list:
      - "apple"
      - "mango"
      - "orange"
  tasks:
    - name: Print the List
      debug:
        msg: "{{ my_list }}"

    - name: Concatenate a list to string
      set_fact:
        my_string: "{{ my_list | join(',') }}"

    - name: Print the String
      debug:
        msg: "{{ my_string }}"

Sample execution

$ ansible-playbook site.yml

PLAY [List to String Conversion] *********************************************************************************

TASK [Gathering Facts] *******************************************************************************************

ok: [localhost]

TASK [Print the List] ********************************************************************************************
ok: [localhost] => {
    "msg": [
        "apple",
        "mango",
        "orange"
    ]
}

TASK [Concatenate a list to string] ******************************************************************************
ok: [localhost]

TASK [Print the String] ******************************************************************************************
ok: [localhost] => {
    "msg": "apple,mango,orange"
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Check Ansible Real Life GitHub repository for all use cases.

Want to learn Ansible from scratch ? Check you YouTube Playlist.

Gineesh Madapparambath

Gineesh Madapparambath

Gineesh Madapparambath is the founder of techbeatly. He is the co-author of The Kubernetes Bible, Second Edition and the author of Ansible for Real Life Automation. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (Read more: iamgini.com)


Note

Disclaimer: The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Share :

Related Posts

Ansible for Windows – Troubleshooting

Ansible for Windows – Troubleshooting

Managing Windows machines using Ansible is pretty straightforward and simple as in the document. You need to configure and enable WinR M on your …

Python Virtual Environments for Ansible [LIVE]

It’s vital to test new technology before rolling it out into your production environment. I like to use Python virtual environments provided by the …

How to install an Ansible Collection on a disconnected Ansible control node

How to install an Ansible Collection on a disconnected Ansible control node

In this tutorial, I demonstrate how and where to install Ansible Content Collections in an Ansible control node that has no internet access. The …