简体   繁体   中英

Docker Playbook yml - ssh to a host with different port

I have deploy script for a Docker container.

- hosts: "{{ HOSTS|default('...') }}"
  remote_user: "{{ USER|default('root') }}"
  vars:
    deploy_port: "{{ DEPLOY_PORT|default(80) }}"
    git_branch: "{{ GIT_BRANCH|default('development') }}"
    python_settings: "{{ PYTHON_SETTINGS|default('development') }}"
  tasks:
  - name: Log into Docker registry
    docker_login:
      registry_url: ...
      username: ...
      password: ...
  ....

So firstly I do SSH to a HOSTS with user USER. But now, I have HOSTS that have different ssh port (not 22). How do I set this?

I try with port, and it did't work

- hosts: "{{ HOSTS|default('...') }}"
  port: 23
  remote_user: "{{ USER|default('root') }}"
  vars:
    deploy_port: "{{ DEPLOY_PORT|default(80) }}"
    git_branch: "{{ GIT_BRANCH|default('development') }}"
    python_settings: "{{ PYTHON_SETTINGS|default('development') }}"
  tasks:
  - name: Log into Docker registry
    docker_login:
      registry_url: ...
      username: ...
      password: ...
  ....

This is a var you should set in your inventory for hosts that do not have the default port. Its name is ansible_port (and not port as you tried).

Very basically:

  • Create a host_vars folder (at same level as your playbook or at same level as your inventory file)
  • For each machine having a different port that 22, create a <name_of_host>.yml file in host_vars with the following content (replace XX with your actual port):
     --- ansible_port: XX

Some reference documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM