gpt4 book ai didi

Ansible:提供的主机列表为空

转载 作者:行者123 更新时间:2023-12-04 11:15:53 27 4
gpt4 key购买 nike

我有下面的剧本,其中远程主机是用户输入,随后我试图收集有关远程主机的事实并将其复制到本地文件中:

---
- hosts: localhost
vars_prompt:
name: hostname
prompt: "Enter Hostname"
tasks:
- name: Add hosts to known_hosts file
add_host: name={{ hostname }} groups=new
- name: Check if Host is reachable
shell: ansible -m ping {{ hostname }}
- name: Remove existing remote hosts
shell: ssh-keygen -R {{ hostname }}
- name: Setup passwordless SSH login
shell: ssh-copy-id -i ~/.ssh/id_rsa user@{{ hostname }}
- name: Display facts
command: ansible {{ groups['new'] }} -m setup
register: output
- copy: content="{{ output }}" dest=/var/tmp/dir/Node_Health/temp
...

我在 temp 中收到以下错误文件:
Node_Health]# cat temp
{"start": "2016-06-17 09:26:59.174155", "delta": "0:00:00.279268", "cmd": ["ansible", "[udl360x4675]", "-m", "setup"], "end": "2016-06-17 09:26:59.453423", "stderr": " [WARNING]: provided hosts list is empty, only localhost is available", "stdout": "", "stdout_lines": [], "changed": true, "rc": 0, "warnings":

我也尝试了下面的剧本,它也给出了同样的错误:
---
- hosts: localhost
vars_prompt:
name: hostname
prompt: "Enter Hostname"
tasks:
- name: Add hosts to known_hosts file
add_host: name={{ hostname }} groups=new
- name: Check if Host is reachable
shell: ansible -m ping {{ hostname }}
- name: Remove existing remote hosts
shell: ssh-keygen -R {{ hostname }}
- name: Setup passwordless SSH login
shell: ssh-copy-id -i ~/.ssh/id_rsa user@{{ hostname }}
- hosts: new
tasks:
- name: Display facts
command: ansible {{ groups['new'] }} -m setup
register: output
- local_action: copy content="{{ output }}" dest=/var/tmp/dir/Node_Health/temp
...

任何帮助将不胜感激。

最佳答案

Ansible 假设您将所有主机都放在某个 list 文件中。
add_host仅将您的主机添加到当前运行的 Ansible,这不会传播到您调用的 Ansible 副本 .

你将不得不:

  • 更改命令以使用内联 list 列表,例如 ansible all -i '{{ hostname }},' -m setup (更多细节使用 -i '<hostname>,' here
  • 或将主机名写到文件中,并将其用作您的 list 文件
  • 关于Ansible:提供的主机列表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37878067/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com