相关文章推荐
独立的奔马  ·  vue ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have this below playbook where the remote host is an user input and subsequently I am trying to gather facts about the remote host and copy the same to a file in local:

- 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 below error in the temp file:

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 below playbook 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 will be appreciated.

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

add_host only adds your host to the currently running Ansible, and that doesn't propagate to the copy of Ansible you call.

You're going to have to either:

  • change the command to use an inline inventory list, like ansible all -i '{{ hostname }},' -m setup (More details re the use of -i '<hostname>,' here

  • or write out the hostname to a file, and use that as your inventory file

  • I actually need an user input which is the reason i am unable to use the -i option.. I think i will go with the your second option. Only that I will add the input hostname to the same inventory file (under /etc/ansible/hosts) and remove the entries at the end of the playbook. Could not find a better way .. thanks :) – Supratim Das Jun 21, 2016 at 9:11
    [nodes]
    node_u1 ansible_user=root   ansible_host=127.0.0.1
    node_u2 ansible_user=root   ansible_host=127.0.1.1
    node_u3 ansible_user=root   ansible_host=127.0.2.1
    node_u4 ansible_user=root   ansible_host=127.0.3.1
    node_u5 ansible_user=root   ansible_host=127.0.4.1
    

    Running ansible, use: ansible-playbook -i hosts.ini

    You can also save your hosts file in /etc/ansible/hosts to avoid passing hosts as parameters. Ansible looks there as a default location. Then just run using:

    ansible-playbook <playbook.yml>
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.