Ansible: the list of available hosts is empty

I have this below a playbook where the remote host is the user, and then I try to collect facts about the remote host and copy them to a file locally:

---
- 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
...

I get the following error in the file 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":

I also tried the following game, which also gives the same error:

---
 - 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
...

Any help would be appreciated.

+4
source share
1 answer

Ansible assumes that you have all your hosts in an inventory file somewhere.

add_hostAdds your host to the currently running Ansible, and does not apply to the copy of Ansible that you are calling .

You will also need:

  • , ansible all -i '{{ hostname }},' -m setup ( -i '<hostname>,'

+2

All Articles