jgilfoil

ansible register with_items loop

Feb 22nd, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. Given a directory structure like this:
  2. [user@testbox01 test]$ ls -al /app/psoft/test/*
  3. /app/psoft/test/12.1.2.00:
  4. total 12
  5. drwxrwxr-x. 2 psoft psoft 4096 Feb 19 16:41 .
  6. drwxrwxr-x. 4 psoft psoft 4096 Feb 19 15:08 ..
  7. -rw-rw-r--. 1 psoft psoft 54 Feb 19 16:41 list.txt
  8.  
  9. /app/psoft/test/12.1.3.00:
  10. total 12
  11. drwxrwxr-x. 2 psoft psoft 4096 Feb 19 16:41 .
  12. drwxrwxr-x. 4 psoft psoft 4096 Feb 19 15:08 ..
  13. -rw-rw-r--. 1 psoft psoft 12 Feb 19 16:41 context.xml
  14.  
  15.  
  16. test.yml playbook:
  17.  
  18. - hosts: all
  19. tasks:
  20. - name: gather list of files
  21. shell: ls {{ item }}
  22. register: files
  23. with_items:
  24. - /app/psoft/test/*/list.txt
  25. - /app/psoft/test/*/context.xml
  26.  
  27. - name: use shell to print list of file paths
  28. shell: "echo {{ item }}"
  29. with_items: "{{files.stdout_lines}}"
  30.  
  31.  
  32. output is:
  33.  
  34. [user@ansibleserver01 ansible]$ ansible-playbook -i testing-inventory playbook_playground/test.yml --limit testbox01
  35.  
  36. PLAY ***************************************************************************
  37.  
  38. TASK [setup] *******************************************************************
  39. ok: [testbox01]
  40.  
  41. TASK [gather list of files] ****************************************************
  42. changed: [testbox01] => (item=/app/psoft/test/*/list.txt)
  43. changed: [testbox01] => (item=/app/psoft/test/*/context.xml)
  44.  
  45. TASK [use shell to print list of file paths] ***********************************
  46. [DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this will be a fatal error.. This feature will
  47. be removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
  48.  
  49. PLAY RECAP *********************************************************************
  50. testbox01 : ok=2 changed=1 unreachable=0 failed=0
  51.  
  52.  
  53. Expected result:
  54. it should just print a list of files:
  55. /app/psoft/test/12.1.2.00/list.txt
  56. /app/psoft/test/12.1.3.00/context.xml
Advertisement
Add Comment
Please, Sign In to add comment