Variable within the variable

51 views
Skip to first unread message

wpgpo...@gmail.com

unread,
May 30, 2024, 4:15:25 PMMay 30
to Ansible Project
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.

Todd Lewis

unread,
Jun 5, 2024, 8:22:40 AMJun 5
to ansible...@googlegroups.com, uto...@gmail.com
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.
- 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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a7edd5a7-90df-4087-b56a-4c4bad72f2e5n%40googlegroups.com.

-- 
Todd

wpgpo...@gmail.com

unread,
Jun 7, 2024, 3:58:22 PMJun 7
to Ansible Project
Thank you so much Todd. I will try and test your suggestion.
Reply all
Reply to author
Forward
0 new messages