# create variable from value gathered on remote

**URL:** <https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112>\
**Category:** Ansible Project\
**Created:** [September 16, 2022, 1:19pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112 "2022-09-16T13:19:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 16, 2022, 1:19pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/1 "2022-09-16T13:19:10Z")

</div>

I am trying to automate the creation of postgresql services on a shared postgres Server as my target.

we create an .env file for each service on the target with contains the portnumber used for that particular instance. Based on that I detect the largest currently reserved postgres port.

I can detect that number by this command on that host (not knowing whether this is the most scientific way to do this, but it works).

&nbsp;&nbsp;&nbsp;grep -r 'PGPORT=' /opt/db/postgres/bin/ | cut -d: -f2 | cut -d= -f2 | tail -1

It is worth mentioning that port scannic with i.e. netstat can not be utilized because it is possible a service may not be running at the time of play which might return a false result.

Now I want to create a vairable pg\_service\_port for my playbook with the next larger number detected (if the above returns 5433 the new value 5434 (5433 +1) shall be applied for the current run.

can somebody kindly point me to the right approach for such an endeavour? I could imagine debug may play a role in this but don't really have much of a clue with it.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 16, 2022, 1:54pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/2 "2022-09-16T13:54:22Z")

</div>

Instead of fragile grepping for port numbers in files that someone (you?) put somewhere and then incrementing those - why not just configure this yourself in the playbook?  
You then have a single source of truth.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 16, 2022, 2:25pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/3 "2022-09-16T14:25:05Z")

</div>

Dick above gave a better answer, but just to have the original  
question literally answered:

- shell: grep -r 'PGPORT=' /opt/db/postgres/bin/ | cut -d: -f2 | cut  
-d= -f2 | tail -1  
&nbsp;&nbsp;&nbsp;register: myport

&nbsp;&nbsp;myport['stdout'] will have the value you want

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 16, 2022, 4:44pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/4 "2022-09-16T16:44:29Z")

</div>

For example, given the files

> ssh admin@test\_11 cat /tmp/.env

SMTPPORT: 5432  
POPPORT: 5431  
PGPORT: 5433

> ssh admin@test\_12 cat /tmp/.env

SMTPPORT: 4432  
POPPORT: 4431  
PGPORT: 4433

> ssh admin@test\_13 cat /tmp/.env

SMTPPORT: 3432  
POPPORT: 3431  
PGPORT: 3433

Fetch the files and declare the variables you need

> cat pb.yml

- hosts: test\_11,test\_12,test\_13  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;env\_file: /tmp/.env  
&nbsp;&nbsp;&nbsp;&nbsp;env\_dest: "{{ playbook\_dir }}/env\_dest"  
&nbsp;&nbsp;&nbsp;&nbsp;my\_env\_file: "{{ [env\_dest, inventory\_hostname, env\_file]|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join('/') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;my\_ports: "{{ lookup('file', my\_env\_file)|from\_yaml }}"  
&nbsp;&nbsp;&nbsp;&nbsp;pg\_service\_port: "{{ my\_ports.PGPORT + 1 }}"  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- fetch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ env\_file }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ env\_dest }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: pg\_service\_port

give (abridged)

ok: [test\_11] =\>  
&nbsp;&nbsp;pg\_service\_port: '5434'  
ok: [test\_12] =\>  
&nbsp;&nbsp;pg\_service\_port: '4434'  
ok: [test\_13] =\>  
&nbsp;&nbsp;pg\_service\_port: '3434'

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 19, 2022, 10:23am UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/5 "2022-09-19T10:23:14Z")

</div>

Thanks Vladimir ... that looks pretty sophisticated (almost out of sight-ish) ... but could be a nice challenge to even understand what is going on 🙂

so far I detected one challenge. Maybe my initial question has not been sufficientely explicit with this.

I run this against a single host that has a couple of paralles postgres services running.  
The pg-somename.env files, defining the port for each instance which I want to use to detext the next free port are located in the same folder and the only thing I can tell about the filenames is that they follow a pg\*.env naming pattern.

as far as my experiments with fetch go it is not able to interpret such a pattern in the way regular bash (find, cat, less, ..) does. So the TASK below does not localize the files I am looking for.

&nbsp;&nbsp;&nbsp;- name: fetch all .env files to fetched  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.fetch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /opt/db/postgres/bin/.pg\*env  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: fetched/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flat: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes

is there a trick to make this work? A colleague mentioned using the find module in order to creating a list for a loop precedding a fetch might be possible. But that seems to be pretty hairy as well.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 19, 2022, 10:34am UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/6 "2022-09-19T10:34:22Z")

</div>

Find the files first. See  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html)

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 19, 2022, 2:06pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/7 "2022-09-19T14:06:37Z")

</div>

I manage to get something like this from a set\_fact following the find TASK

TASK [creating a list with the filenames] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/gwagner/repos/ansible/open\_source/postgres\_create\_service/tasks/fetchsomething.yml:20  
ok: [vm-414001-0227.step.zrz.dvz.cn-mv.de] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_facts": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"env\_files": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service10.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service11.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service12.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service13.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service14.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service15.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service16.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service17.env",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/opt/db/postgres/bin/.pg-service18.env"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false  
}

---

<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:** [September 19, 2022, 2:25pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/8 "2022-09-19T14:25:57Z")

</div>

You're so close. You're passing in a list as a single item. Instead of

&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ found\_files.files | map(attribute='path') | map('basename') | list }}"

do this:

&nbsp;&nbsp;&nbsp;loop: "{{ found\_files.files | map(attribute='path') | map('basename') | list }}"

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 19, 2022, 3:34pm UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/9 "2022-09-19T15:34:02Z")

</div>

great, that works. So one part of the puzzle is solved, thx

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 20, 2022, 9:13am UTC](https://forum.ansible.com/t/create-variable-from-value-gathered-on-remote/37112/10 "2022-09-20T09:13:23Z")

</div>

this is how I manged this in the end. probably the solution suggested by @Vladimit Botka is more scientific but I could not indorporate the find operation required with the rest of the suggestion
