Answers: <p>I used this solution:</p>
  <pre class="lang-bash prettyprint-override"><code>ssh u.local
  git clone https://github.com/serkodev/rabbitmq-cluster-docker
  cd ./rabbitmq-cluster-docker
  docker compose up -d
  exit
  </code></pre>
  <pre class="lang-bash prettyprint-override"><code>ssh -L /tmp/docker.sock:/var/run/docker.sock u.local
  </code></pre>
  <p><code>cat inventory/docker.yml</code>:</p>
  <pre class="lang-yaml prettyprint-override"><code>plugin: community.docker.docker_containers
  docker_host: unix:///tmp/docker.sock
  filters:
    - exclude: &gt;-
        inventory_hostname.startswith(&quot;rabbitmq-cluster-docker-haproxy&quot;)
  </code></pre>
  <pre class="lang-bash prettyprint-override"><code>ansible-playbook -i inventory/docker.yml ping.yml
  </code></pre>
  <p>output:</p>
  <pre class="lang-none prettyprint-override"><code>rabbitmq-cluster-docker-rabbitmq1-1 : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  rabbitmq-cluster-docker-rabbitmq2-1 : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  rabbitmq-cluster-docker-rabbitmq3-1 : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  </code></pre>
  <p>src: <a href="https://stackoverflow.com/a/59829788">https://stackoverflow.com/a/59829788</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-22T11:34:48.980Z
Answers: 2024-11-22T11:42:01.587Z
Answers: 2024-11-22T11:42:01.587Z
Answers: don Rumata
Answers: <p>I have found a hacky workaround for my problem. I replicated the behavior of the <code>community.postgresql.*</code> modules that I want to use in a bash script and execute it using the <code>community.docker.docker_container</code> module.</p>
  <pre><code>- name: Ensure databases and users exist
    register: &quot;pg_ensure_db&quot;
    changed_when: &quot;not 'created' in pg_ensure_db.container.Output&quot;
    community.docker.docker_container:
      name: postgresql-ensure-db
      image: &quot;{{postgres_image}}&quot;
      cleanup: true
      detach: false
      networks:
        - name: &quot;db&quot;
      command_handling: correct
      command: [&quot;bash&quot;, &quot;-c&quot;, &quot;{{lookup('template', 'postgres-ensure-db.j2.sh')}}&quot; ]
  </code></pre>
  <p>While this is not directly an answer to my question, and certainly less maintainable than if it was done natively using Ansible, I thought I would share it anyway until I find a better solution.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-07-16T08:29:10.370Z
Answers: 2023-07-16T08:29:10.370Z
Answers: Tiim
Body: <p>I have the following situation: I have a remote server that runs docker with a Postgres container. The Postgres instance is only reachable through a docker network, not directly from the remote server.</p>
  <p>I am trying to create an ansible role that makes sure all the required databases and users exist in the Postgres instance. For this I am trying to make Ansible first connect to the remote server, then connect to the Postgres docker container, and from there use the <code>community.postgresql.postgresql_*</code> modules to create the databases and users. However, I am stuck trying to get Ansible to do the nested connection. I currently have the following ansible-playbook:</p>
  <pre class="lang-yml prettyprint-override"><code># playbook.yml
  - name: Postgres
    hosts: dockerservers
    roles:
      - name: postgres
        tags: [postgres, never]
  
  # roles/postgres/tasks/main.yml
  - name: Connect to database docker container
    add_host:
      name: &quot;postgres&quot;
      ansible_connection: docker
    changed_when: false
  
  - name: Inside postgres docker container
    delegate_to: &quot;postgres&quot;
    block:
      - name: Debug
        community.postgresql.postgresql_info:
  </code></pre>
  <p>Docker tries to use the local docker cli (which is not installed) to connect to the remote docker daemon. Is there a way to somehow hop from the remote server to the docker container, instead of trying to directly connect to it?</p>
  
Tags: <docker><ansible><ssh><postgresql>
Title: Ansible Connecting to remote Docker container over SSH
UserDisplayName: Tiim
ThreadText: # Ansible Connecting to remote Docker container over SSH
  
  (Asked by: username_0 on 2023-07-15)
  
  I have the following situation: I have a remote server that runs docker with a Postgres container. The Postgres instance is only reachable through a docker network, not directly from the remote server.
  
  I am trying to create an ansible role that makes sure all the required databases and users exist in the Postgres instance. For this I am trying to make Ansible first connect to the remote server, then connect to the Postgres docker container, and from there use the `community.postgresql.postgresql_*` modules to create the databases and users. However, I am stuck trying to get Ansible to do the nested connection. I currently have the following ansible-playbook:
  
  ```yml
  # playbook.yml
  - name: Postgres
    hosts: dockerservers
    roles:
      - name: postgres
        tags: [postgres, never]
  
  # roles/postgres/tasks/main.yml
  - name: Connect to database docker container
    add_host:
      name: "postgres"
      ansible_connection: docker
    changed_when: false
  
  - name: Inside postgres docker container
    delegate_to: "postgres"
    block:
      - name: Debug
        community.postgresql.postgresql_info:
  
  ```
  
  Docker tries to use the local docker cli (which is not installed) to connect to the remote docker daemon. Is there a way to somehow hop from the remote server to the docker container, instead of trying to directly connect to it?
  
  **Tags:** docker, ansible, ssh, postgresql
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2023-07-16. Score: 1
  
  I have found a hacky workaround for my problem. I replicated the behavior of the `community.postgresql.*` modules that I want to use in a bash script and execute it using the `community.docker.docker_container` module.
  
  ```
  - name: Ensure databases and users exist
    register: "pg_ensure_db"
    changed_when: "not 'created' in pg_ensure_db.container.Output"
    community.docker.docker_container:
      name: postgresql-ensure-db
      image: "{{postgres_image}}"
      cleanup: true
      detach: false
      networks:
        - name: "db"
      command_handling: correct
      command: ["bash", "-c", "{{lookup('template', 'postgres-ensure-db.j2.sh')}}" ]
  
  ```
  
  While this is not directly an answer to my question, and certainly less maintainable than if it was done natively using Ansible, I thought I would share it anyway until I find a better solution.
  
  
  ---
  
  ### Answer by username_1 on 2024-11-22. Score: 0
  
  I used this solution:
  
  ```bash
  ssh u.local
  git clone https://github.com/serkodev/rabbitmq-cluster-docker
  cd ./rabbitmq-cluster-docker
  docker compose up -d
  exit
  
  ```
  
  ```bash
  ssh -L /tmp/docker.sock:/var/run/docker.sock u.local
  
  ```
  
  `cat inventory/docker.yml`:
  
  ```yaml
  plugin: community.docker.docker_containers
  docker_host: unix:///tmp/docker.sock
  filters:
    - exclude: >-
        inventory_hostname.startswith("rabbitmq-cluster-docker-haproxy")
  
  ```
  
  ```bash
  ansible-playbook -i inventory/docker.yml ping.yml
  
  ```
  
  output:
  
  ```none
  rabbitmq-cluster-docker-rabbitmq1-1 : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  rabbitmq-cluster-docker-rabbitmq2-1 : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  rabbitmq-cluster-docker-rabbitmq3-1 : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  
  ```
  
  src: <https://stackoverflow.com/a/59829788>
  
  
  ---
  
  