Thankyou for taking time and looking into it..
you made a valid questions, 
1. I am trying this approach so that I can pass {item} as jenkins 
parameters. when dev fro item group is given it should print all servers 
from that env.
2.  group_vars/server1.yml  I am trying to get the first part working so I 
did not specify the output file here. ( will make a note next time not to 
miss it)
also can you please elaborate more here "Your serverlist is a string with "{{ 
item }}" in it, but you're using it in a shell module invocation with no 
loops, i.e. where "item" isn't defined." 
I mean best possibilities to fulfill my need.
On Friday, May 19, 2023 at 1:37:37 PM UTC-7 Todd Lewis wrote:

> > will my below playbook work
> I don't think so.
>
> 1. Your serverlist is a string with "{{ item }}" in it, but you're using 
> it in a shell module invocation with no loops, i.e. where "item" isn't 
> defined.
>
> 2. In your shell script, your jq output is being directed in 
> group_vars/server1.yml *with replacement*. Once you fix issue #1 and 
> start looping, you'll find servers1.yml contains the data only for the 
> last iteration.
>
> I'm puzzled by your placement of the serverlist definition in a separate 
> file rather than in a vars: section in the playbook, or even hard-coded 
> directly in the embedded shell script. Nor is it clear to me what you 
> expect the final debug step to show you. I mean, we know it will look 
> like this:
>
> TASK [unnamed debug loop] 
> ******************************************************
> ok: [localhost] => (item=dev) => {
>     "msg": "data , 
> https://test-dev.test.com/testapi/test/server?environment=dev&os=linux";
> }
> ok: [localhost] => (item=uat) => {
>     "msg": "data , 
> https://test-dev.test.com/testapi/test/server?environment=uat&os=linux";
> }
> ok: [localhost] => (item=stg2) => {
>     "msg": "data , 
> https://test-dev.test.com/testapi/test/server?environment=stg2&os=linux";
> }
>
> but what's the point?
>
> On Friday, May 19, 2023 at 3:19:16 PM UTC-4 Narmada Karthika wrote:
>
>> thank you all for your findings and recommendations
>>
>> In the same playbook I would like to explain one more scenario , I am 
>> using set_facts to use above playbook output as input in the playbook, and 
>> I have variables.yml 
>>
>> It has to go through loop and get the server details from each 
>> environment, will my below playbook work
>> my variables.yml file
>> # Environment and os as input to pull all servers out of this
>> serverlist: "https://test-dev.test.com/testapi/test/server?environment={{ 
>> item }}&os=linux" +# 
>> # item should be replaced with the item from the loop and pass it to 
>> below playbook
>>
>> vars_files:
>>     - "variables.yml"
>>   tasks:
>>     - name: set the facts per host
>>       set_fact:
>>         access_token: "{{ hostvars.localhost.output.stdout }}" # this is 
>> the output of the above play 
>>     - name: Make a post call to get server details
>>       shell: |
>>         cd /var/output/
>>         curl --location --request GET {{ serverlist }} \--header 
>> Authorization: Bearer {{ access_token }} > /var/output/servers1.json 
>> jq -r '.[]|.serverData[].servers[]' servers1.json > 
>> /home/deploy-user/ansible/group_vars/servers1.yml 
>>       register: data
>>     - debug: msg="data , {{ serverlist }}" # this need to replaced from 
>> one var file
>>       loop:
>>         - dev
>>         - uat
>>         - stg2
>>
>>
>> On Friday, May 19, 2023 at 5:07:45 AM UTC-7 Rowe, Walter P. (Fed) wrote:
>>
>>> Your curl command places the output in /var/output/{{ item }}.json. 
>>> Your jq command reads the output from {{ item }}.json which is not 
>>> /var/output/{{ item }}.json.
>>>
>>> Walter
>>> --
>>> Walter Rowe, Division Chief
>>> Infrastructure Services, OISM
>>> Mobile: 202.355.4123 <(202)%20355-4123>
>>>
>>> On May 19, 2023, at 1:21 AM, Narmada Karthika <[email protected]> 
>>> wrote:
>>>
>>> I am getting error saying  I am using 2 var_files. 
>>> ERROR! failed at splitting arguments, either an unbalanced jinja2 block 
>>> or quotes: 
>>>
>>> I also tried using include_vars which gave me same error, the values in 
>>> these 2 files need to be replace at diff locations at play book
>>> some one please suggest what is the best way for this
>>>
>>>  name: post call to get servergroup
>>>   hosts: localhost
>>>   become: true
>>>   gather_facts: no
>>>   tasks:
>>>     - name: set the facts per host
>>>       set_fact:
>>>         access_token: "{{ hostvars.localhost.output.stdout }}" # this 
>>> is the output of the above play
>>>     - name: Make a post call to get server details
>>>       shell: |
>>>         cd /var/output/
>>>         curl --location --request GET "{{ serverlist }}" \--header 
>>> 'Authorization: Bearer "{{ access_token }}" > /var/output/"{{ item }}".json
>>>         jq -r '.[]|.serverData[].servers[]' "{{ item }}".json > 
>>> /home/deploy-user/ansible/group_vars/"{{ item }}".yml
>>>       # item and server list from one file and 
>>> register: data
>>>     - debug: msg="data , {{ repo }}" # this need to replaced from one 
>>> var file
>>>       loop:
>>>         - dev
>>>         - uat
>>>         - stg2
>>>
>>>
>>> -- 
>>> 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 [email protected].
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/ansible-project/41e35941-ac2e-48fe-9654-489f0c298c7dn%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/ansible-project/41e35941-ac2e-48fe-9654-489f0c298c7dn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>>
>>>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3d6d45d6-4d58-411b-9b20-efbd56683ffcn%40googlegroups.com.

Reply via email to