iterate over disk devices

181 views
Skip to first unread message

Filip Slunecko

unread,
Mar 28, 2014, 11:41:02 PM3/28/14
to ansible...@googlegroups.com
Hi all,

Is there a way how ti easily iterate over facter_blockdevices?

Example:

      - name: checking disks for failure
        command:  /usr/sbin/smartctl /dev/{{ item }}
        with_items: facter_blockdevices
        register: smart_out

Facts:
"facter_blockdevices": "sda,sdb,sdc,sdd,sde,sdf",

Thanks!

Filip

Věroš Kaplan

unread,
Mar 29, 2014, 8:54:38 AM3/29/14
to ansible...@googlegroups.com
Ahoj Filip,

I would consider separating facter_blockdevices fact to a list. ( see https://gist.github.com/VerosK/9853931 ).

I don't  now if this is the right Ansible-way, but at least it works.

--Věroš

---
- hosts: localhost
  vars:
    facter_blockdevices: sda,sdb,sdc,sdd,sde,sdf
  gather_facts: no
  tasks:
    - name: Separate facts
      set_fact: blockdevices="{{facter_blockdevices.split(',')}}"

    - name: Show the devices
      shell: echo {{ item }}
      with_items: blockdevices


Paul Durivage

unread,
Mar 31, 2014, 10:28:11 AM3/31/14
to ansible...@googlegroups.com
Hi Filip,

You'll find that calling the split method on the string works.


12345678910

---
- 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(',')"


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fa84c84b-b4d8-4cd7-a8d7-894df146c8a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Filip Slunecko

unread,
Mar 31, 2014, 2:04:32 PM3/31/14
to ansible...@googlegroups.com
Thank you!

I tried jinja filters and they do not work :).
I assume I can use any python function there right?

Paul Durivage

unread,
Mar 31, 2014, 2:22:57 PM3/31/14
to ansible...@googlegroups.com
Hi Filip,

No problem!

No, not every Python function is available in Jinja.  In that example, you see we can use any method associated with string in Jinja, because the string is still an object, and it's methods come with it.  Just remember that you always have access to the objects methods.

For future reference, by the way, the list of standard Jinja filters are here: http://jinja.pocoo.org/docs/templates/#builtin-filters
I'm actually not sure if all the Ansible filters are documented in a single place, but IIRC they are all in the following Python file:



Reply all
Reply to author
Forward
0 new messages