Answers:
您可以在主机上尝试local_action,以查看是否可以连接到相应的端口并注册成功的端口并将其设置为事实。您想关闭收集事实,因为否则安装模块在尝试与已经重新配置的主机连接时将失败。完成此游戏后,只需在下面添加其他内容并添加collect_facts即可。
- name: determine ssh port
hosts: all
gather_facts: false
vars:
custom_ssh_port: 222
tasks:
- name: test default ssh port
local_action: wait_for port=22 timeout=5 host={{inventory_hostname}}
register: default_ssh
ignore_errors: true
- name: set ansible_ssh_port to default
set_fact: ansible_ssh_port=22
when: default_ssh.elapsed < 5
- name: test ssh on high port
local_action: wait_for port={{custom_ssh_port}} timeout=5 host={{inventory_hostname}}
register: high_ssh
when: default_ssh.elapsed >= 5
ignore_errors: true
- name: set ansible_ssh_port high
set_fact: ansible_ssh_port={{custom_ssh_port}}
when: default_ssh.elapsed >= 5 and high_ssh.elapsed < 5
有人向我指出,这将浪费您使用它的剧本的时间。您还可以在剧本的vars部分中设置ansible_ssh_port,该剧本只能在具有重新配置的ssh端口的主机上运行。例如
- name: change ssh ports
tasks:
- name: edit sshd_config
lineinfile ..
notify: restart ssh
handlers:
- name: restart ssh
service: sshd state=restarted
- name: continue setup
vars:
- ansible_ssh_port : 5422
tasks:
...
@RichardSalts感谢您帮助我开始。我用nc来检查端口,它应该快得多。这是我的bootstrap.xml:
使用ansible 1.5(开发版3b8fd62ff9)测试了最新更新2014/01/28 20:26:03
---
# Be sure to set the following variables for all hosts:
# vars:
# oldsshport: 22
# sshport: 555
# Might fail without setting remote_tmp = /tmp/ansible/$USER in your ansible.cfg. Also fix for directly below.
# Once host is setup most of the checks are skipped and works very quickly.
# Also, be sure to set non-standard shells in a different playbook later. Stick with /bin/bash until you can run apt install.
# Assumes root user has sshkey setup already. Not sure how to utilize the --ask-pass option. For now, use ssh-copy-id prior to running playbook on new host for root user (if needed).
# Test new ssh port
- name: ssh test nc {{ sshport }}
local_action: shell nc -z -w5 {{ inventory_hostname }} {{ sshport }}
register: nc_ssh_port
failed_when: nc_ssh_port.stdout.find('failed') != -1
changed_when: nc_ssh_port.stdout == ""
ignore_errors: yes
# Set port to new port if connection success
- name: set ansible_ssh_port
set_fact: ansible_ssh_port={{ sshport }}
when: nc_ssh_port|success
# Fail back to old port if new ssh port fails
- name: ssh test nc port {{ oldsshport }}
local_action: shell nc -z -w5 {{ inventory_hostname }} {{ oldsshport }}
register: nc_ssh_default
changed_when: nc_ssh_default.stdout == ""
ignore_errors: yes
when: nc_ssh_port|changed
# Set ansible to old port since new failed
- name: set ansible_ssh_port to {{ oldsshport }}
set_fact: ansible_ssh_port={{ oldsshport }}
when: nc_ssh_default|success and nc_ssh_port|changed
# Check if root user can ssh
- name: find user
local_action: shell ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=5 -p {{ ansible_ssh_port }} root@{{ inventory_hostname }} exit
register: ssh_as_root
failed_when: ssh_as_root.stdout.find('failed') != -1
changed_when: ssh_as_root.stderr.find('Permission denied') == -1
# If root user success, set this up to change later
- name: first user
set_fact: first_user={{ ansible_ssh_user }}
when: ssh_as_root|changed
# Set ssh user to root
- name: root user
set_fact: ansible_ssh_user=root
when: ssh_as_root|changed
# ANSIBLE FIX: /tmp/ansible isn't world-writable for setting remote_tmp = /tmp/ansible/$USER in ansible.cfg
- name: /tmp/ansible/ directory exists with 0777 permission
file: path=/tmp/ansible/ owner=root group=root mode=0777 recurse=no state=directory
changed_when: False
sudo: yes
# Setup user accounts
- include: users.yml
# Set ssh user back to default user (that was setup in users.yml)
- name: ansible_ssh_user back to default
set_fact: ansible_ssh_user={{ first_user }}
when: ssh_as_root|changed
# Reconfigure ssh with new port (also disables non-ssh key logins and disable root logins)
- name: sshd.conf
template: src=sshd_config.j2 dest=/etc/ssh/sshd_config owner=root group=root mode=0644
register: sshd_config
sudo: yes
# Force changes immediately to ssh
- name: restart ssh
service: name=ssh state=restarted
when: sshd_config|changed
sudo: yes
# Use updated ssh port
- name: set ansible_ssh_port
set_fact: ansible_ssh_port={{ sshport }}
when: nc_ssh_port|changed
如果无法建立与默认SSH端口的连接,是否可以通过将Ansible后退到指定的端口来自动执行此操作?
我还需要类似的功能,所以我分叉并修补了Ansible ssh插件,希望Ansible Inc.会采用它。他们没有。它会测试非std ssh端口规范以查看它们是否打开,如果未打开,则会还原为默认的ssh端口。这是一个非常小的补丁,可从https://github.com/crlb/ansible获得。
如果您有端口列表,并且想要检查所有端口并使用一个有效的端口,则可以在您的剧本中使用它:
- name: just test
hosts: server
gather_facts: false
vars:
list_of_ssh_ports: [22, 222, 234]
tasks:
- name: test ssh on port
sudo: no
local_action: wait_for port={{item}} timeout=5 host={{inventory_hostname}}
register: ssh_checks
with_items: "{{list_of_ssh_ports}}"
ignore_errors: true
- debug: msg = "{{item}}"
with_items: "{{ssh_checks.results}}"
- name: set available ansible_ssh_port
sudo: no
set_fact: ansible_ssh_port={{item.item}}
when: ssh_checks is defined and {{item.elapsed}} < 5
with_items: "{{ssh_checks.results}}"
我提出了一个强大的幂等任务列表,以充当角色来更改SSH端口并处理连接到正确端口的操作,而不必更改清单文件。我已经在博客上发布了详细信息:https : //dmsimard.com/2016/03/15/changing-the-ssh-port-with-ansible/
'dict object' has no attribute 'state'