# simple test of lookup plugins in with_*
---
- hosts: all
  connection: local
  vars:
    empty_list: []
  tasks:
  - name: test with_items
    action: command true
    with_items:
    - 1
    - 2
    - 3
  - name: test with_items with empty list
    action: command true
    with_items: $empty_list

  - name: test with_file and FILE
    action: command test "$item" = "$FILE(sample.j2)"
    with_file: sample.j2

  - name: test with_pipe
    action: command test "$item" = "$PIPE(cat sample.j2)"
    with_pipe: cat sample.j2

  - name: test LOOKUP and PIPE
    action: command test "$LOOKUP(pipe, cat sample.j2)" = "$PIPE(cat sample.j2)"

  - name: test with_sequence, generate
    command: touch /tmp/seq-${item}
    with_sequence: 0-16/2:%02x

  - name: test with_sequence, fenceposts 1
    copy: src=/tmp/seq-00 dest=/tmp/seq-10

  - name: test with_sequence, fenceposts 2
    file: dest=/tmp/seq-${item} state=absent
    with_items: [11, 12]

  - name: test with_sequence, missing
    file: dest=/tmp/seq-${item} state=absent
    with_sequence: 0x10/02:%02x

  - name: test with_sequence,remove
    file: dest=/tmp/seq-${item} state=absent
    with_sequence: 0-0x10/02:%02x

  - name: ensure test file doesnt exist
    # command because file will return differently
    action: command rm -f /tmp/ansible-test-with_lines-data
  - name: test with_lines
    action: shell echo "$item" >> /tmp/ansible-test-with_lines-data
    with_lines: cat sample.j2
  - name: verify with_lines
    action: copy src=sample.j2 dest=/tmp/ansible-test-with_lines-data
  - name: cleanup test file
    action: file path=/tmp/ansible-test-with_lines-data state=absent
    
    # Test nested loop
  - name: test nested loop with more than 3 elements
    command: test "{{ item[0] }}, {{ item[1] }}, {{ item[2] }}, {{ item[3] }}" = "red, 1, up, top"
    with_nested:
    - [ 'red' ]
    - [ 1 ]
    - [ 'up']
    - [ 'top']

# password lookup plugin
  - name: ensure test file doesn't exist
    # command because file will return differently
    action: command rm -f /tmp/ansible-test-with_password
  - name: test LOOKUP and PASSWORD with non existing password file
    action: command test "$LOOKUP(password, /tmp/ansible-test-with_password)" = "$PASSWORD(/tmp/ansible-test-with_password)"
  - name: test LOOKUP and PASSWORD with existing password file
    action: command test "$LOOKUP(password, /tmp/ansible-test-with_password)" = "$PASSWORD(/tmp/ansible-test-with_password)"
  - name: now test existing password via $item and with_password
    action: command test "$PASSWORD(/tmp/ansible-test-with_password)" = "$item"
    with_password:
      - /tmp/ansible-test-with_password
  - name: cleanup test file
    action: file path=/tmp/ansible-test-with_password state=absent
  - name: now test a password of non-default length (default=20, but here length=8)
    action: command test "$PASSWORD(/tmp/ansible-test-with_password length=8)" = "$LOOKUP(password, /tmp/ansible-test-with_password)"
  # - name: did we really create a password of length=8?
  #   action: command test "`expr length $PASSWORD(/tmp/ansible-test-with_password)`" = "8"
  - name: cleanup test file, again
    action: file path=/tmp/ansible-test-with_password state=absent

# indexed_items lookup plugin
  - name: create directory for indexed_items
    file: path=/tmp/ansible-test-with_indexed_items-data state=directory
  - name: test indexed_items
    shell: echo "{{ item.0 }}" > /tmp/ansible-test-with_indexed_items-data/{{ item.1 }}
    with_indexed_items:
    - a
    - b
    - c
  - name: check indexed_items content
    shell: test -f /tmp/ansible-test-with_indexed_items-data/{{ item.1 }} &&
           test "{{ item.0 }}" = "$(cat /tmp/ansible-test-with_indexed_items-data/{{ item.1 }})"
    with_indexed_items:
    - a
    - b
    - c
  - name: cleanup indexed_items test
    file: path=/tmp/ansible-test-with_indexed_items-data/{{ item.1 }} state=absent
    with_indexed_items:
    - a
    - b
    - c
  - name: remove with_indexed_items directory
    file: path=/tmp/ansible-test-with_indexed_items-data state=absent