# Accessing Cfg file values within nested loops + using sections + passing section in cmd argument

**URL:** <https://forum.ansible.com/t/accessing-cfg-file-values-within-nested-loops-using-sections-passing-section-in-cmd-argument/34059>\
**Category:** Ansible Project\
**Created:** [October 19, 2020, 5:29pm UTC](https://forum.ansible.com/t/accessing-cfg-file-values-within-nested-loops-using-sections-passing-section-in-cmd-argument/34059 "2020-10-19T17:29:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshay\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@Akshay\_Kumar](https://forum.ansible.com/u/Akshay_Kumar)\
**Post date:** [October 19, 2020, 5:29pm UTC](https://forum.ansible.com/t/accessing-cfg-file-values-within-nested-loops-using-sections-passing-section-in-cmd-argument/34059/1 "2020-10-19T17:29:42Z")

</div>

Hi All,

Hope all is well with everyone during these bad times because of covid :-).

I would like to know some good advices from the community on a scenario am working on , it goes as followed:

Cfg file:  
SectionA:  
ls:  
dirname:   
Command: List  
cp:  
srcdirname:   
tgtDirname:   
command: copy

SectionB:  
ls:  
dirname:   
command: List

And in playbook right now i am accessing it using with\_dict and include\_vars but i am not able to use cmd line argument for “section” for eg if i say  
ansible-playbook playbook.yaml -e section=SectionA

Any suggestions of how we can access the values of dicts by first checking the section of Cfg file which is passed on cmd line and then performing action according to the command in the dict values.

Thanks in advance for your response .

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 19, 2020, 6:56pm UTC](https://forum.ansible.com/t/accessing-cfg-file-values-within-nested-loops-using-sections-passing-section-in-cmd-argument/34059/2 "2020-10-19T18:56:17Z")

</div>

This is too abstract to comment on in a useful way.  
What does "not able to use" mean - does it fail?  
Please provide the real, actual, working playbook and try to use less  
opaque terms.

Also, what is the overall goal you are trying to achieve?

> From the little information you've shared, the closest I can guess is

"copy some configuration file(s)".  
Please share that as well.

---

<div class="post-metadata">

**Author:** ![Akshay\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@Akshay\_Kumar](https://forum.ansible.com/u/Akshay_Kumar)\
**Post date:** [October 19, 2020, 7:51pm UTC](https://forum.ansible.com/t/accessing-cfg-file-values-within-nested-loops-using-sections-passing-section-in-cmd-argument/34059/3 "2020-10-19T19:51:27Z")

</div>

Hi D.Visser,

Sorry for not being able to share the info properly , i am using the below playbook

- hosts: localhost  
vars\_files:
- “{{ config }}.cfg”

tasks:

- name: run ls  
shell: “‘{{ item.command }}’ ‘{{ item.dir }}’”  
when: “‘{{ item.command }}’ == ‘ls’”  
with\_items: “{{ SectionA }}”

cfg file -----------

SectionA:  
first:  
DirName: /home/kumar/DevFileTestLocation  
Command: ls  
SectionB:  
ls:  
Second:  
DirName: /home/kumar/Dev

So when i call ansible-playbook playbook.yaml , it works because i am calling test SectionA directly in the playbook under with\_items but if am running command like  
ansible-playbook playbook.yaml -e section=SectionA , so that my dict is picked automatically via cmd line , it doesnt work.  
I want that flexibility so that i can call playbook on the basis if my sections and use data within those sections.

also i just want to know if there is any other way to use the data in sections when i set cmd line param using section=SectionA/SectionB.

Thanks
