What is item.strip().split()[0] ?

1,398 views
Skip to first unread message

AnsibleGuy

unread,
Jul 15, 2017, 2:34:40 PM7/15/17
to Ansible Project
May I know what is " item.strip().split()[0] " mean in below code? I'm thining, first value in a space seperated line. Ref: https://github.com/geerlingguy/ansible-role-nfs/blob/master/tasks/main.yml - name: Ensure directories to export exist
file: 'path="{{ item.strip().split()[0] }}" state=directory'
with_items: "{{ nfs_exports }}" notify: restart nfs

Mathias Ettinger

unread,
Jul 17, 2017, 7:31:58 AM7/17/17
to Ansible Project
Almost. It's the first value in a whitespace separated line, which means 'foo bar' and 'foo\n \tbar' would both be splitted to ['foo', 'bar'] and element 0 is 'foo' for both.

On a side note, split() without arguments has extra logic that make the call to strip() redundant.

Lastly, nfs_exports is a list defined as empty by default in https://github.com/geerlingguy/ansible-role-nfs/blob/master/defaults/main.yml. You may want to populate it by other means when running the playbook.

Arbab Nazar

unread,
Jul 17, 2017, 11:30:56 AM7/17/17
to Ansible Project
You can check the readme file of the role and take the example from there and create small playbook like this:

---
- hosts: all
  become
: no
  connection
: local
  vars
:
    nfs_exports
: { "/home/public *(rw,sync,no_root_squash)" }
  tasks
:

   
- name: Ensure directories to export
exist
      debug
:
        msg
: "{{ item.strip().split()[0] }}"
      with_items
: "{{ nfs_exports }}"

and run it like this to see the result:


─➤  ansible-playbook -i localhost, -c local playbook.yml


PLAY
[all] *******************************************************************************************************************


TASK
[Gathering Facts] *******************************************************************************************************
ok
: [localhost]


TASK
[Ensure directories to export exist] ************************************************************************************
ok
: [localhost] => (item=/home/public *(rw,sync,no_root_squash)) => {
   
"item": "/home/public *(rw,sync,no_root_squash)",
   
"msg": "/home/public"
}


PLAY RECAP
*******************************************************************************************************************
localhost                  
: ok=2    changed=0    unreachable=0    failed=0


Hope that helps you to understand the value.
Reply all
Reply to author
Forward
0 new messages