简体   繁体   English

Ansible 更改剧本中的 ssh 端口

[英]Ansible change ssh port in playbook

Here is the inventory file这是库存文件

---
[de-servers]
192.26.32.32

[uk-servers]
172.21.1.23
172.32.2.11

and my playbook is look like this:我的剧本是这样的:

- name: Install de-servers configurations
  hosts: de-servers  
  roles:
    - de-server-setup

- name: Install uk-servers configurations
  hosts: uk-servers  
  roles:
    - uk-server-setup

- name: Do some other job on de-servers (cannot be done until uk-servers is installed)
  hosts: de-servers
  roles:
    - de-servers-rest-of-jobs

In role de-servers-setup role the ssh port is changed from 22 to 8888, so when the last task is called it fails because it cannot connect to host through 22 port.在角色 de-servers-setup 角色中,ssh 端口从 22 更改为 8888,因此在调用最后一个任务时失败,因为它无法通过 22 端口连接到主机。 How to overcome this ssh port change?如何克服此 ssh 端口更改?

In the role de-server-setup add a task to change the ansible_port host variable.在角色de-server-setup添加一个任务来更改ansible_port主机变量。

- name: Change ssh port to 8888
  set_fact:
    ansible_port: 8888

The only thing I can think of that might work would be to create ssh aliases for your hosts.我能想到的唯一可行的方法是为您的主机创建 ssh 别名。 In your .ssh/config :在您的.ssh/config

Host de.1.before
  HostName 192.26.32.32
  Port 22

Host de.1.after
  HostName 192.26.32.32
  Port 8888

Then use these aliases in your Ansible inventory:然后在您的 Ansible 清单中使用这些别名:

[de-servers-before]
de.1.before

[de-servers-after]
de.1.after

And the defined groups then respectively in your plays:然后分别在您的戏剧中定义组:

- name: Install de-servers configurations
  hosts: de-servers-before
  roles:
    - de-server-setup

- name: Install uk-servers configurations
  hosts: uk-servers  
  roles:
    - uk-server-setup

- name: Do some other job on de-servers (cannot be done until uk-servers is installed)
  hosts: de-servers-after
  roles:
    - de-servers-rest-of-jobs

My full solution to this was to create a common playbook imported at the top of all other playbooks that checks the status of the non-standard ansible_port defined in the inventory.我对此的完整解决方案是在所有其他剧本的顶部创建一个通用剧本,以检查清单中定义的非标准ansible_port的状态。 If the port is open then continue as normal.如果端口打开,则继续正常。 If it's not open check port 22 and set the ansible_port fact to that if so.如果它没有打开检查端口 22 并将ansible_port事实设置为如果是这样。

Later, when the SSH server is configured for the first time and the default port is changed to my non-standard port, I then update the ansible_port fact manually in my playbook so that any further Ansible connections in the current run will work as expected.稍后,当第一次配置 SSH 服务器并将默认端口更改为我的非标准端口时,我然后在我的剧本中手动更新ansible_port事实,以便当前运行中的任何其他 Ansible 连接将按预期工作。

My inventory looks like this:我的库存是这样的:

[webservers]
web01.somedomain.com ansible_port=1234

My playbook looks like this:我的剧本是这样的:

- name: Determine SSH port
  hosts: all
  gather_facts: no
  remote_user: root
  tasks:
    - name: "Check port {{ ansible_port }}"
      wait_for:
        port: "{{ ansible_port }}"
        state: "started"
        host: "{{ inventory_hostname }}"
        connect_timeout: "5"
        timeout: "5"
      delegate_to: "localhost"
      ignore_errors: "yes"
      register: ssh_port

    - name: "Check port 22"
      wait_for:
        port: "22"
        state: "started"
        host: "{{ inventory_hostname }}"
        connect_timeout: "5"
        timeout: "5"
      delegate_to: "localhost"
      ignore_errors: "yes"
      register: ssh_port_default
      when: 
        - ssh_port is defined 
        - ssh_port.state is undefined

    - name: Set SSH port to 22
      set_fact:
        ansible_port: "22"
      when: ssh_port_default.state is defined

Finally, right after the SSH server is configured and the port has been changed I have this:最后,在配置 SSH 服务器并更改端口之后,我有这个:

- name: Set SSH port to 1234
  set_fact:
    ansible_port: "1234"

Easy way, edit /etc/ansible/hosts:简单的方法,编辑/etc/ansible/hosts:

[my_server]
ssdnodes:54321

and you can test it by issuing a ping:您可以通过发出 ping 来测试它:

ansible ssdnodes -m ping

and the response would be:响应将是:

ssdnodes | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

I would suggest to put the port number on the inventory file.我建议将端口号放在清单文件中。 Like the following example.像下面的例子。

[linux-servers]
xcpng5.homelab.com ansible_port=3511
xcpng2.homelab.com ansible_port=3522
xcpng1.homelab.com ansible_port=3523

I was trying to achieve the same and that helped me to set up different ssh port.我试图实现相同的目标,这有助于我设置不同的 ssh 端口。

I need to change the ssh ports on the hosts I manage and I want to use Ansible to do it.我需要更改我管理的主机上的 ssh 端口,我想使用 Ansible 来做到这一点。 Essentially, Ansible uses the following logic to manage it's SSH connections:本质上,Ansible 使用以下逻辑来管理其 SSH 连接:

    if self.port is not None:
        ssh -p {{ self.port }} ...
    else:
        ssh ...

where "self.port" is the port specification from the host inventory, or an override via the "-e" parameter, or an explicit declaration of the variables "ansible_port" and/or "ansible_ssh_port".其中“self.port”是主机清单中的端口规范,或通过“-e”参数的覆盖,或变量“ansible_port”和/或“ansible_ssh_port”的显式声明。 The recommended solution to changing ports is to employ the "wait_for" and "when" modules in "pre_tasks", but there are many inadequacies to this approach, particularly when many hosts are involved and especially when you want to use different ports on different hosts.更改端口的推荐解决方案是在“pre_tasks”中使用“wait_for”和“when”模块,但是这种方法有很多不足之处,尤其是当涉及到许多主机时,尤其是当您想在不同主机上使用不同端口时.

I cloned and patched the ssh plugin (versions 1 and 2) to change the logic as follows:我克隆并修补了 ssh 插件(版本 1 和 2)以更改逻辑如下:

if self.port is not None and self.port is OPEN:
    ssh -p {{ self.port }} ...
else:
    ssh ...

The patch, by itself, makes no changes on the target nodes but allows connections to succeed even if the ports on the nodes haven't changed yet.该补丁本身不会对目标节点进行任何更改,但即使节点上的端口尚未更改,也允许连接成功。 With the patch, it is now very easy to write roles/tasks to change ssh ports to whatever is in the host inventory.有了这个补丁,现在可以很容易地编写角色/任务来将 ssh 端口更改为主机清单中的任何内容。

If you're interested, you can find the patch and samples of how use it at https://github.com/crlb/ansible ;如果您有兴趣,可以在https://github.com/crlb/ansible 上找到如何使用它的补丁和示例; the README.md contains additional information. README.md 包含附加信息。

Below is my example for connecting with different ssh port using ansible-playbook.下面是我使用 ansible-playbook 连接不同 ssh 端口的示例。

---
- hosts: test-server
  vars:
        ansible_ssh_user: 'rohit'
        ansible_password: '123456'
        ansible_port: '2222'
  tasks:
        - name: "print simple command"
          command: cat /usr/bin/myscript.sh

In your inventory file you can define the ssh port like this在您的清单文件中,您可以像这样定义 ssh 端口


[de-servers] [去服务器]

192.26.32.32:8888 192.26.32.32:8888

[uk-servers] [英国服务器]

172.21.1.23:8888 172.21.1.23:8888

172.32.2.11:8888 172.32.2.11:8888

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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