Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Ansible查找下一个可用端口_Ruby On Rails_Ansible_Puma - Fatal编程技术网

Ruby on rails 使用Ansible查找下一个可用端口

Ruby on rails 使用Ansible查找下一个可用端口,ruby-on-rails,ansible,puma,Ruby On Rails,Ansible,Puma,我正在使用Ansible部署一个使用Puma作为web服务器的RubyonRails应用程序。作为部署的一部分,Puma配置绑定到端口8080上服务器的IP地址: bind "tcp://{{ ip_address }}:8080" 然后在nginx vhost配置中使用此选项访问应用程序: upstream {{ app_name }} { server {{ ip_address }}:8080; } 所有这些都很好。但是,我现在想在同一台服务器上部署应用程序的多个副本(暂存、生产)

我正在使用Ansible部署一个使用Puma作为web服务器的RubyonRails应用程序。作为部署的一部分,Puma配置绑定到端口8080上服务器的IP地址:

bind "tcp://{{ ip_address }}:8080"
然后在nginx vhost配置中使用此选项访问应用程序:

upstream {{ app_name }} {
  server {{ ip_address }}:8080;
}
所有这些都很好。但是,我现在想在同一台服务器上部署应用程序的多个副本(暂存、生产),显然8080上有多个绑定会导致问题,因此我需要使用不同的端口

最简单的解决方案是将端口包含在组var中,然后在部署应用程序时将其放入。但是,这需要了解服务器上已经运行的应用程序的背景知识,而且感觉部署应该能够“发现”要使用的端口

相反,我在考虑通过端口进行某种迭代,从8080开始,然后检查每个端口,直到一个端口没有被使用
netstat-anp | grep8080
如果正在使用端口,则会给出一个返回代码0,因此也许我可以使用该命令进行测试(尽管我不确定如何执行循环位)


以前有人遇到过这个问题吗?有没有一个更优雅的解决方案是我忽略的?

我会定义允许的端口列表,并将其与可用端口进行比较

大概是这样的:

- hosts: myserver
  vars:
    allowed_ports:
      - 80
      - 8200
  tasks:
    - name: Gather occupied tcp v4 ports
      shell: netstat -nlt4 | grep -oP '(?<=0.0.0.0:)(\d+)'
      register: used_ports

    - name: Set bind_port as first available port
      set_fact:
        bind_port: "{{ allowed_ports | difference(used_ports.stdout_lines | map('int') | list) | first | default(0) }}"
      failed_when: bind_port | int == 0

    - name: Show bind port
      debug: var=bind_port
-主机:myserver
变量:
允许的\u端口:
- 80
- 8200
任务:
-名称:收集已占用的tcp v4端口
shell:netstat-nlt4 | grep-oP'(?)?