# Variable within the variable

**URL:** <https://forum.ansible.com/t/variable-within-the-variable/38800>\
**Category:** Ansible Project\
**Created:** [May 30, 2024, 8:15pm UTC](https://forum.ansible.com/t/variable-within-the-variable/38800 "2024-05-30T20:15:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [May 30, 2024, 8:15pm UTC](https://forum.ansible.com/t/variable-within-the-variable/38800/1 "2024-05-30T20:15:25Z")

</div>

Hello Group,

Requesting for any help and guidance on how I can able to use the variable within the variables.

I have the variable (fuser\_dirname) that I’d usually used as the main variable in my playbook. However, I’d just need to have another variable with the list of the car name to make it as directory name.

This maybe similar to the bash script where I will cat the variable file then use the ‘for’ loop command.

Was wondering if anyone may have some idea on how to work this in just playbook module and script.

Below are the command, expected result, variable, and playbook files.

Command

ansible-playbook /ansible\_scripts/playbooks/testtemp/for\_test\_only.yml --limit fuser\_hostname

Result

fuserATawscar001:/apps\> ls -l  
drwxr-xr-x 3 fuser fuser 33 May 30 08:40 honda-sample-dir/  
drwxr-xr-x 3 fuser fuser 33 May 30 08:40 tesla-sample-dir/  
drwxr-xr-x 3 fuser fuser 33 May 30 08:40 tesla-sample-dir/

Variable File

/ansible\_scripts/vars/vars\_carmodel\_file.yml  
car\_dirname:

- honda
- toyota
- tesla

Playbook File

- name: Create and copy folder  
hosts: all  
become\_user: fuser

vars\_files:

- /ansible\_scripts/vars/vars\_carmodel\_file.yml

vars:  
fuser\_dirname: (( fuser\_folder ))-sample-dir

tasks:

- name: create  
file:  
path: “/home/mule/{{ fuser\_dirname }}”  
mode: 0755  
state: directory

- name: copy  
copy:  
src: “/home/fuser/{{ fuser\_dirname }}/”  
dest: “/apps/{{ fuser\_dirname }}”

Any help and guidance are much appreciated.

Thank you so much in advance.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 5, 2024, 12:22pm UTC](https://forum.ansible.com/t/variable-within-the-variable/38800/2 "2024-06-05T12:22:20Z")

</div>

You need to make your fuser\_dirname variable change as you loop over your car models.  
Then you need to make your tasks loop over the car models.

```auto
- name: Create and copy folder
  hosts: all
  become_user: fuser

  vars_files:
    - /ansible_scripts/vars/vars_carmodel_file.yml

  vars:
    fuser_dirname: " **{{ item }}-sample-dir**"

  tasks:
    - name: Create sample directories in /home/mule
      ansible.builtin.file:
        path: "/home/mule/{{ fuser_dirname }}"
        mode: "0755"
        state: directory
      **loop: "{{ car_dirname }}"**

    - name: Copy /home/fuser directories to /apps
      ansible.builtin.copy:
        src: "/home/fuser/{{ fuser_dirname }}/"
        dest: "/apps/{{ fuser_dirname }}"
      **loop: "{{ car_dirname }}"**

```

But there’s lots of other questionable stuff going on here. The first task is working in the target hosts /home/mule directory, but the second task is copying directories from the ansible controller’s /home/fuser tree to the target hosts without specifying group, owner, or mode.

Anyway, this is one answer to how to deal with the looping. You’ll need to work out these other details, though, before you have a useful process.

Also, use ansible-lint on your code.

---

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [June 7, 2024, 7:58pm UTC](https://forum.ansible.com/t/variable-within-the-variable/38800/3 "2024-06-07T19:58:22Z")

</div>

Thank you so much Todd. I will try and test your suggestion.
