ansible - 在 ansible 任务中使用的 delegate_to 问题
问题描述
这是使用“ delegate_to: "localhost"
”的任务尝试 ssh“localhost”而不是从“localhost”(ansible master)到远程的 ssh 端口的正确行为吗?
剧本失败:
"Timeout when waiting for localhost:2022"
这里的示例配置我复制它:
库存文件:
[testremote]
192.168.170.113 ansible_user=ja
Ansible 配置文件:
[defaults]
host_key_checking = False
inventory = hosts
callback_enabled = profile_tasks
ansible_port = 2022
剧本文件:
- hosts: testremote
gather_facts: false
vars:
desired_port: 2022
tasks:
- name: check if ssh is running on {{ desired_port }}
delegate_to: localhost
wait_for:
port: "{{ desired_port }}"
host: "{{ ansible_host }}"
timeout: 10
ignore_errors: true
register: desired_port_check
- when: desired_port_check is success
block:
- debug:
msg: "ssh is running on desired port"
- name: configure ansible to use port {{ desired_port }}
set_fact:
ansible_port: "{{ desired_port }}"
- name: run a command on the target host
command: uptime
register: uptime
- debug:
msg: "{{ uptime.stdout }}"
远程主机已经可以在所需的端口上访问:
[ansible]$ ssh -p 2022 ja@testremote date
Sun Jun 20 16:40:36 CEST 2021
[ansible]$ ping testremote
PING testremote (192.168.170.113) 56(84) bytes of data.
64 bytes from testremote (192.168.170.113): icmp_seq=1 ttl=63 time=1.14 ms
运行剧本时的结果:
[ansible]$ ansible-playbook test_playbook.yml
PLAY [testremote] **********************************************************************************************************************************************************
TASK [check if ssh is running on 2022] *************************************************************************************************************************************
fatal: [192.168.170.113 -> localhost]: FAILED! => {"changed": false, "elapsed": 10, "msg": "Timeout when waiting for localhost:2022"}
...ignoring
TASK [debug] ***************************************************************************************************************************************************************
skipping: [192.168.170.113]
TASK [configure ansible to use port 2022] **********************************************************************************************************************************
skipping: [192.168.170.113]
TASK [run a command on the target host] ************************************************************************************************************************************
fatal: [192.168.170.113]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.170.113 port 22: Connection refused", "unreachable": true}
PLAY RECAP *****************************************************************************************************************************************************************
192.168.170.113 : ok=1 changed=0 unreachable=1 failed=0 skipped=2 rescued=0 ignored=1
解决方案
推荐阅读
- r - 由于 nlsModel 中的错误,for 循环中的 nls 停止
- sql - Sql Developer - 使用 concat 和 GROUP BY 函数进行计数(错误 ORA-00904)
- unity3d - 10 秒后 Unity 多人游戏中的玩家生成
- c# - NLog:在运行时从模板生成配置
- .htaccess - Laravel 中的自定义 slug (URL)(使用 htaccess 是一个选项)
- javascript - 等待剑道图完全绘制
- reactjs - 如何使用 apache 修改传入的响应标头?
- scala - 运行 Scala 代码时发现类型不匹配
- ios - NSURLErrorDomain:无法连接到服务器
- java - 使用 Key_Class 作为参数插入最大堆