Answers: <p>Disclaimer: I'm not using Ansible.</p>
  
  <p>What I would do is use a random "predictable" number.
  According to Ansible doc you can seed the random number generator:</p>
  
  <blockquote>
    <p>As of Ansible version 2.3, it’s also possible to initialize the random
    number generator from a seed. This way, you can create
    random-but-idempotent numbers:</p>
    
    <p>"{{ 59 |random(seed=inventory_hostname) }} * * * * root
    /script/from/cron"</p>
  </blockquote>
  
  <p>So in your case for a port number (I assume unpriviledged) I would go for a variable with something like:</p>
  
  <pre><code>port="{{ 32767 |random(start=1024,seed=service_name) }}"
  </code></pre>
  
  <p>Max at 32767 to avoid clashing with any client initiated port (See <a href="https://en.wikipedia.org/wiki/Ephemeral_port" rel="noreferrer">Ephemeral port</a> for the reason).</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-03-02T13:13:11.937Z
Answers: That is a good idea, which I considered, but it makes the increasing part impossible imo. Having certain services which are logically related in a certain port range is probably very desirable.
Answers: midor
Answers: CC BY-SA 3.0
Answers: 2017-03-02T10:48:01.473Z
Answers: 2017-03-02T10:48:01.473Z
Answers: Tensibai
Body: <p>I am new to Ansible, but I have to maintain a set of playbooks, which correspond to services to be setup in a given environment. They need to be assigned a port, certificates, etc. This results in many files with lists of essentially always the same names and an assignment to them.</p>
  
  <p>In many cases I think I can easily reuse the service_name as variable, but when mapping to IPs, ports or other numeric identifiers I have not yet figured out a way to deterministically assign them different numbers in a way that is reproducible, and preferably remains the same even when new services are added. I have considered using a SQLite database to store the services from and to generate the values from their ids, but I have no idea how to integrate that with Ansible.</p>
  
  <p>I assume that assigning increasing port numbers is not something entirely new; it is something a lot of sysadmins to on a day-to-day basis so there has to be some way to do that.</p>
  
  <p><strong>Edit</strong>: We directly add the port numbers etc. in <code>group_vars/all.yml</code> like this:</p>
  
  <pre><code>ports:
      service1:1024
      service2:1025
      service3:1026
  </code></pre>
  
  <p>The inventory is generated automatically, since we create additional jails (BSD) and depends on the roles that will be executed.</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-03-02T11:49:43.707Z
Comments: Since you probably have to add the service to the inventory anyway, what's stopping you to add a specific port number there manually? Or if your inventory is automatically generated, then you should probably solve the problem at the inventory generation level
Comments: SztupY
Comments: CC BY-SA 3.0
Comments: 2017-03-02T23:25:57.467Z
Comments: Would you please include some Ansible snippets? It's not clear if you are using `group_vars` or adding variables right into inventory files. It's also not clear how your inventory is being generated.
Comments: Woodland
Comments: CC BY-SA 3.0
Comments: 2017-03-04T03:18:27.780Z
Comments: Have you considered using a service discovery tool like HashiCorp's Consul?
Comments: Foghorn CTO
Tags: <ansible><ports>
Title: How to deterministic and reproducibly assign increasing port numbers in Ansible?
UserDisplayName: midor
ThreadText: # How to deterministic and reproducibly assign increasing port numbers in Ansible?
  
  (Asked by: username_0 on 2017-03-02)
  
  I am new to Ansible, but I have to maintain a set of playbooks, which correspond to services to be setup in a given environment. They need to be assigned a port, certificates, etc. This results in many files with lists of essentially always the same names and an assignment to them.
  
  In many cases I think I can easily reuse the service\_name as variable, but when mapping to IPs, ports or other numeric identifiers I have not yet figured out a way to deterministically assign them different numbers in a way that is reproducible, and preferably remains the same even when new services are added. I have considered using a SQLite database to store the services from and to generate the values from their ids, but I have no idea how to integrate that with Ansible.
  
  I assume that assigning increasing port numbers is not something entirely new; it is something a lot of sysadmins to on a day-to-day basis so there has to be some way to do that.
  
  **Edit**: We directly add the port numbers etc. in `group_vars/all.yml` like this:
  
  ```
  ports:
      service1:1024
      service2:1025
      service3:1026
  
  ```
  
  The inventory is generated automatically, since we create additional jails (BSD) and depends on the roles that will be executed.
  
  **Tags:** ansible, ports
  
  > **Comments:**
  > **username_1**: Since you probably have to add the service to the inventory anyway, what's stopping you to add a specific port number there manually? Or if your inventory is automatically generated, then you should probably solve the problem at the inventory generation level  
  > **username_2**: Would you please include some Ansible snippets? It's not clear if you are using `group_vars` or adding variables right into inventory files. It's also not clear how your inventory is being generated.  
  > **username_3**: Have you considered using a service discovery tool like HashiCorp's Consul?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2017-03-02. Score: 5
  
  Disclaimer: I'm not using Ansible.
  
  What I would do is use a random "predictable" number.
  According to Ansible doc you can seed the random number generator:
  
  > As of Ansible version 2.3, it’s also possible to initialize the random
  > number generator from a seed. This way, you can create
  > random-but-idempotent numbers:
  >
  > "{{ 59 |random(seed=inventory\_hostname) }} \* \* \* \* root
  > /script/from/cron"
  
  So in your case for a port number (I assume unpriviledged) I would go for a variable with something like:
  
  ```
  port="{{ 32767 |random(start=1024,seed=service_name) }}"
  
  ```
  
  Max at 32767 to avoid clashing with any client initiated port (See [Ephemeral port](https://en.wikipedia.org/wiki/Ephemeral_port) for the reason).
  
  > **Comments:**
  > **username_0**: That is a good idea, which I considered, but it makes the increasing part impossible imo. Having certain services which are logically related in a certain port range is probably very desirable.  
  
  ---
  
  