Ansible loop, set different variables

27 views
Skip to first unread message

John Harmon

unread,
Dec 21, 2017, 11:43:54 AM12/21/17
to Ansible Project
I need to check if certain paths exists.  More than one path may exist on a server:
/home/user1/Scripts
/home/user2/Scripts
/home/user3/Scripts

If it exists, I want to define a variable.  For user1, variable user1 will be defined, user2 for user2 and so on.

I know I can easily check the paths in a loop, but how would I go about setting a custom var for each in the same loop?

- name: Check paths
  stat
:
    path
: {{ item.path }}
  with_items
:
   
- { path: '/home/user1/Scripts' }
    - { path: '/home/user2/Scripts' }
    - { path: '/home/user3/Scripts' }


As register isn't part of stat, I don't know how I would register different vars.

Olivier Cervello

unread,
Dec 21, 2017, 9:42:40 PM12/21/17
to Ansible Project
maybe something like:

- set_fact:
     users
: [ 'user1', 'user2', 'user3']


- name: Check paths
  stat
:

     path
: "/home/{{ item }}/Scripts"
  with_items
: "{{ users}}"
 
register: stat_results
Reply all
Reply to author
Forward
0 new messages