Pull of List in a Variable File as Extra-Vars

91 views
Skip to first unread message

wpgpo...@gmail.com

unread,
Aug 3, 2022, 5:02:16 PM8/3/22
to Ansible Project
Hello Team,

Can I ask some help on how to pull items as Extra-Vars?
So, here is the scenario.

dirname.yml
dir_list:
  - hello
  - goodbye
  - take-care

patch_file.yml
patch_lib:
  - lib_1
  - lib_2

general_var.yml
main_dir: "{{ app_name }}-{{ ansible_hostname }}-{{ ver }}"

main_playbook.yml
- name: Test Playbook Script for Debug
  hosts: all

  vars_files:
    - general_var.yml
    - dirname.yml
    - patch_file.yml

tasks:
    - name: Copy | All Mule v392 Patch to LIB-User Directory for "{{Application_Runtime | upper}}" Runtime
      copy:
        src: "/user/lib/{{ item .0 }}"
        dest: "/home/{{ item.1 }}/lib/user/"
        mode: 0644
      with_nested:
        - "{{ patch_lib }}"
        - "{{ main_dir }}"

And here's my playbook script that doesn't work.
ansible-playbook  main_playbook.yml --limit awssandbox001.us.com -e "app_name= dir_list"

The idea, it should be able to copy the list of 'patch_lib" into each "main_dir".
dest:
/home/hello-awssandbox001.us.com-123/lib/user
/home/goodbye-awssandbox001.us.com-123/lib/user
/home/take-care-awssandbox001.us.com-123/lib/user

Look forward for anybody that can able to help.


Thanks... WP

Walter Rowe

unread,
Aug 4, 2022, 9:11:49 AM8/4/22
to Ansible Project
Are you getting an error? Can we see it?
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

wpgpo...@gmail.com

unread,
Aug 4, 2022, 10:09:44 AM8/4/22
to Ansible Project
Yeah, I forgot to mention the result.
Basically, there was no error but instead it creates a new directory from the value of 'app_name' rather than the item list of the dir_list.

Result: /home/dir_list-awssandbox001.us.com-123/lib/user
Expected:
  - /home/hello-awssandbox001.us.com-123/lib/user
  - /home/goodbye-awssandbox001.us.com-123/lib/user
  - /home/take-care-awssandbox001.us.com-123/lib/user

Appreciated.

Thanks,
WP

Walter Rowe

unread,
Aug 4, 2022, 11:53:41 AM8/4/22
to Ansible Project
See if this does what you want ..

- name: Test Playbook Script for Debug
  hosts: localhost

  vars_files:
    - general_var.yml
    - dirname.yml 
    - patch_file.yml

  tasks:
    - name: Copy | All Mule v392 Patch to LIB-User Directory for "{{Application_Runtime | upper}}" Runtime
      copy:
        src: "/user/lib/{{ item .0 }}"
        dest: "/home/{{ item.1 }}-{{ ansible_hostname }}-{{ ver }}/lib/user/"
        mode: 0644
      with_nested:
        - "{{ patch_lib }}"
        - "{{ dir_list }}"

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

wpgpo...@gmail.com

unread,
Aug 4, 2022, 12:52:07 PM8/4/22
to Ansible Project
Thank You, Walter.
It does work as expected however, I do have couple of variables inside the dirname.yml and different directories inside of the home path.
The idea, I don't want to run the copy module (etc) in one execution but instead putting them in a group or set. Another purpose, I don't want them to restart them at the same time wherein it will be on schedule on when it will happen.

dirname.yml
dir_list:
  - hello
  - goodbye
  - take-care

another_list:
  - say
  - tell
  - make

exam_list:
  - etc
  - other

Home Directory
  - /home/hello-awssandbox001.us.com-123
  - /home/goodbye-awssandbox001.us.com-123
  - /home/takecare-awssandbox001.us.com-123
  - /home/say-awssandbox001.us.com-123
  - /home/tell-awssandbox001.us.com-123
  - /home/make-awssandbox001.us.com-123
  - /home/etc-awssandbox001.us.com-123
  - /home/other-awssandbox001.us.com-123

Appreciated and thanks... WP

Walter Rowe

unread,
Aug 4, 2022, 1:06:16 PM8/4/22
to Ansible Project
You can group your inventory and provide the group name to the execution and it will only run on that group.

$ ansible-playbook --inventory my_inventory_file --limit group_name ... rest of your command ...

The "hosts: all" in your playbook will be limited to the group from your inventory that you specify on the command line.

Walter

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

wpgpo...@gmail.com

unread,
Aug 4, 2022, 1:35:37 PM8/4/22
to Ansible Project
Hello,
I have tried something like this one... Btw, I've been using group_vars already for my other variables.

dir tree:
- inventory
  - inv_test_01 (file)
  - group_vars (dir)
    - awssbx (file)

inv_test_01 (file)
[awssbx]

[awsdev]
awsdev001
awsdev002

awssbx (file)
dir_list:
  - hello
  - goodbye
  - take-care

Command:
$ ansible-playbook --inventory  inv_test_01 --limit  awssbx ... rest of the command ...
-e "app_name= dir_list"

or even without the -inventory also doesn't work. It is still creating folder of the 'dir_list'.

Thank you and appreciated.... WP

Walter Rowe

unread,
Aug 4, 2022, 2:11:35 PM8/4/22
to Ansible Project
Are you thinking there is different "dir_list" for each group?

--limit awssxb would also need an awssbx vars file that has one list of values for dir_list
--limt awssbx2 would also need an awssbx2 vars file that has a different list of values for dir_list?

In your playbook you can reference vars file by variable names, so you could use the group name

  vars_files:
    - general_var.yml
    - "{{ ansible_limit }}.yml"
    - patch_file.yml

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

Walter Rowe

unread,
Aug 4, 2022, 2:14:51 PM8/4/22
to Ansible Project

wpgpo...@gmail.com

unread,
Aug 4, 2022, 5:17:40 PM8/4/22
to Ansible Project
Thank you Walter...
Honestly, I have no idea and trying to understand and learning it now.
I'll get back once I have tried again.

Thank you and appreciated... WP

Vladimir Botka

unread,
Aug 4, 2022, 8:35:34 PM8/4/22
to 'Walter Rowe' via Ansible Project
> On Wednesday, August 3, 2022 at 5:02:16 PM UTC-4 wpgpo...@gmail.com wrote:
> > *general_var.yml*
> > main_dir: "{{ app_name }}-{{ ansible_hostname }}-{{ ver }}"

Use *vars* lookup plugin to indirectly reference the value of the
variable stored in *app_name*. Then use filter *product* to create
the combinations. For example, given

* the extra variable '-e app_name=dir_list'
* the variable *dir_list*
shell> cat dirname.yml
dir_list:
- hello
- goodbye
- take-care
* the inventory
shell> cat hosts
host01 ansible_hostname=01.us.com
* the variable 'ver=123'

The variable *main_dir*

shell> cat general_var.yml
main_dir: "{{ lookup('vars', app_name)|
product([ansible_hostname])|
product([ver])|
map('flatten')|
map('join','-')|list }}"

gives the list you want (simplified)

main_dir:
- hello-01.us.com-123
- goodbye-01.us.com-123
- take-care-01.us.com-123

Test the iteration

- debug:
msg: "copy {{ item.0 }} to {{ item.1 }}"
with_nested:
- "{{ patch_lib }}"
- "{{ main_dir }}"

gives (abridged)

msg: copy lib_1 to hello-01.us.com-123
msg: copy lib_1 to goodbye-01.us.com-123
msg: copy lib_1 to take-care-01.us.com-123
msg: copy lib_2 to hello-01.us.com-123
msg: copy lib_2 to goodbye-01.us.com-123
msg: copy lib_2 to take-care-01.us.com-123

Fit the details to your needs.

--
Vladimir Botka

wpgpo...@gmail.com

unread,
Aug 5, 2022, 12:58:58 PM8/5/22
to Ansible Project
Hello Vladimir,

It does works successfully. Many thanks and highly appreciated.

This thread is RESOLVED.

Thanks... WP
Reply all
Reply to author
Forward
0 new messages