Using (partially) host-specific lists with with_items

57 views
Skip to first unread message

Tore Olsen

unread,
Oct 27, 2014, 11:10:30 AM10/27/14
to ansible...@googlegroups.com

Hi,


I want to use logentries to forward important log files. Most servers have the same files, but there are some which are specific based on which services are running. So I figured out one way to do this by using with_items like the example below, but I'm not sure if this is considered a good practice. The programmer in me instinctively wanted to define a variable in group_vars/all and append to it (like a "subclass") in group_vars/my_custom_server but that doesn't seem to be possible(?).


Or should I just create one task for the generic case and another task for the more specific one?


What's the idiomatic way to do this in ansible?


-Tore



---

- hosts: all


  vars:

  - has_particular_file: no # or yes; to see the different behaviour


  tasks:

  - name: Test

    action: debug msg="{{ item.file }}"

    with_items:

    - { file: /var/log/foo.log }

    - { file: /var/log/bar.log }

    - { file: /var/log/particular.log, cond: "{{ has_particular_file }}" }

    when: item.cond is not defined or item.cond|bool


Michael DeHaan

unread,
Oct 30, 2014, 4:19:01 PM10/30/14
to ansible...@googlegroups.com
You could choose to define the "with_items" list in your group_vars/<whichever> or another variable file to make that a little cleaner.

The complex conditional is definitely available, though a little unusual.

It might be more common to check to see if a file exists with the "stat" module -- asking the remote system, rather than the configuration if it needs something.

If you have more than one role configuring something, only copy that last log file in the role that needs it.



--
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/62451252-1540-483f-988a-d6c1f799f800%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tore Olsen

unread,
Oct 31, 2014, 5:28:53 AM10/31/14
to ansible...@googlegroups.com
Hi Michael,

thanks for your reply. I did in fact modify my solution according to your latest comment, by defining different roles and running multiple logentries tasks. I realize it's simpler than attempting to dynamically build a list of arguments.

I like your idea about testing the remote system with stat though. It would be nice to define a list of common files in a group/whatever variable,
and use stat to filter.  Do you think the following is an appropriate solution?

  tasks:

  - name: Stat

    action: stat path="{{ item }}"

    with_items:

    - "{{ files }}"

    register: statreg


  - name: Test

    action: debug msg="{{ item.item }}"

    with_items: statreg.results

    when: item.stat.exists


Thanks again.

-Tore

Michael DeHaan

unread,
Nov 1, 2014, 10:49:14 AM11/1/14
to ansible...@googlegroups.com
Well the debug is just going to show output, rather than fail if something didn't exist, but I guess that would be fine if you just want to show output.

On this part:

    with_items:

    - "{{ files }}"


This can be written as:


with_items: files


And would be simpler.




Reply all
Reply to author
Forward
0 new messages