Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Splitting in Jinja2/Ansible
---
- name: Test split
hosts: localhost
gather_facts: false
vars:
- facter_blockdevices: "sda,sdb,sdc,sdd,sde,sdf"
tasks:
- name: Let's split
debug: var=item
with_items: "facter_blockdevices.split(',')"
@angstwad

This comment has been minimized.

Copy link
Owner Author

angstwad commented Mar 31, 2014

09:23 $ ansible-playbook test-split.yml

PLAY [Test split] *************************************************************

TASK: [Let's split] ***********************************************************
ok: [localhost] => (item=sda) => {
    "item": "sda"
}
ok: [localhost] => (item=sdb) => {
    "item": "sdb"
}
ok: [localhost] => (item=sdc) => {
    "item": "sdc"
}
ok: [localhost] => (item=sdd) => {
    "item": "sdd"
}
ok: [localhost] => (item=sde) => {
    "item": "sde"
}
ok: [localhost] => (item=sdf) => {
    "item": "sdf"
}

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0
@Constantin07

This comment has been minimized.

Copy link

Constantin07 commented May 15, 2015

Hi,

How to split the string inside the loop?

Inside the with_items loop the {{ item }} gets a value like "/roles/files/external/example.values.db" and I just need the "example.values.db" for validation ...

Thanks,
Constantin

@jmehnle

This comment has been minimized.

Copy link

jmehnle commented Feb 3, 2016

@Constantin07, use {{ item | basename }}.

@davidorman

This comment has been minimized.

Copy link

davidorman commented Nov 27, 2017

This didnt work me, but

---
- name: Test split
  hosts: localhost
  gather_facts: false
  vars:
    - facter_blockdevices: "sda,sdb,sdc,sdd,sde,sdf"
  tasks:
    - name: Let's split
      debug: var=item
      with_items: "{{ facter_blockdevices.split(',') }}"

does:

ansible-playbook mytest.yml 

PLAY [Test split] *****************************************************************************************************************************

TASK [Let's split] ****************************************************************************************************************************
ok: [localhost] => (item=sda) => {
    "item": "sda"
}
ok: [localhost] => (item=sdb) => {
    "item": "sdb"
}
ok: [localhost] => (item=sdc) => {
    "item": "sdc"
}
ok: [localhost] => (item=sdd) => {
    "item": "sdd"
}
ok: [localhost] => (item=sde) => {
    "item": "sde"
}
ok: [localhost] => (item=sdf) => {
    "item": "sdf"
}

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0   
@thisisjagan

This comment has been minimized.

Copy link

thisisjagan commented Jul 24, 2018

I get the following output when i try this. Please help.

TASK [Let's split] ************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {}
ok: [localhost] => (item=None) => {}
ok: [localhost] => (item=None) => {}
ok: [localhost] => (item=None) => {}
ok: [localhost] => (item=None) => {}
ok: [localhost] => (item=None) => {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.