Every answer mentions about how to print the home directory details while running the playbook and displaying it on screen using debug and var.
Adapting to @TrinitronX answer
An additional information on using this information to a new task.
I have a list of users whose home directory needs to be extracted. So I have added the user details to a list
- name: Get home directory
shell: >
getent passwd {{ item.user }} | cut -d: -f6
changed_when: false
with_items:
- "{{java}}"
register: user_home
Here this step will loop through all user list and will register that details to user_home. And this will be in the form of an array.
Then next step is to use this information to a new task, which is say for example sourcing a file into bash profile. This is just an example and can be any scenario, but method will remain the same.
- name: Set Java home in .bash_profile
lineinfile: path="{{ item.stdout }}/.bash_profile" regexp='^source "{{ java_dir }}/.bash_profile_java"' line='source "{{ java_dir }}/.bash_profile_java"' state=present
with_items:
- "{{ user_home.results }}"
loop_control:
label: "{{ item.stdout }}"
I have set a fact for java_dir to /usr/java/latest in the same playbook.
Array user_home.results will contain the details of the Get home directory task.
Now we loop through this array and take out the stdout value which contains the home directory path.
I have put loop_control for printing the home directory only, else it will print the entire array.
By this process, we can ensure that if n number of users are there, we can follow this method and all will be taken care.
Note: I have started to learn the Ansible, in case if any terminology I have used is wrong, please excuse. I have spend some time for figuring out on how to do this and thought of sharing the same.