Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use register and with_items #17

Open
ioggstream opened this issue Sep 29, 2017 · 0 comments
Open

Use register and with_items #17

ioggstream opened this issue Sep 29, 2017 · 0 comments

Comments

@ioggstream
Copy link
Owner

ioggstream commented Sep 29, 2017

Add this example


- hosts: localhost
  tasks:
  - name: List all possibile /etc/hosts candidates
    stat:
      path: >-
        {{item}}
    with_items:
    - /etc/FOO
    - /etc/hosts
    register: etc_hosts_candidates
  - name: Show only existing
    debug:
      msg: >
        {{ etc_hosts_candidates.results 
           | selectattr("stat.exists", "equalto", true) 
           | list }}

Get the first one. Remember map should go before first as it works on lists
and if you put it after, iiuc the item is implicitly converted into its keys which have no "item" attribute

- hosts: localhost
  gather_facts: no
  tasks:
  - stat:
      path: >-
        {{item}}
    with_items:
    - /etc/FOO
    - /etc/hosts
    register: o
  - debug:
      msg: >
        {{ o.results
          | selectattr("stat.exists", "equalto", true)
          | map(attribute="item")
          | first
         }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant