Skip to content

Instantly share code, notes, and snippets.

@angstwad
Created March 31, 2014 14:24
Show Gist options
  • Save angstwad/9893480 to your computer and use it in GitHub Desktop.
Save angstwad/9893480 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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
Copy link

jmehnle commented Feb 3, 2016

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

@davidorman
Copy link

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
Copy link

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