# how to prompt user for hostname to deploy playbook to?

**URL:** <https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130>\
**Category:** Ansible Project\
**Created:** [November 27, 2018, 7:26pm UTC](https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130 "2018-11-27T19:26:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tuyen\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tuyen\_Nguyen](https://forum.ansible.com/u/Tuyen_Nguyen)\
**Post date:** [November 27, 2018, 7:26pm UTC](https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130/1 "2018-11-27T19:26:16Z")

</div>

Hi

Can we run a playbook and have it prompt the user for the hostname to deploy the playbook tasks to?

This is what I have tried in my playbook.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 27, 2018, 9:57pm UTC](https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130/2 "2018-11-27T21:57:26Z")

</div>

I don’t know if this will help, but I never prompt for a host, and never define one in my playbooks with anything other than a variable:

ie.

`

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 27, 2018, 10:02pm UTC](https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130/3 "2018-11-27T22:02:57Z")

</div>

I believe your problem is that you are specifying hosts twice. The following works for me (gather facts is enabled for this example (to check ansible\_fqdn), but wouldn’t be necessary for this to work):

`

---

<div class="post-metadata">

**Author:** ![Tuyen\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tuyen\_Nguyen](https://forum.ansible.com/u/Tuyen_Nguyen)\
**Post date:** [December 5, 2018, 3:24pm UTC](https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130/4 "2018-12-05T15:24:24Z")

</div>

Hi

sorry for late response. Actually, I have found a way that works for me, and I hope can help others looking for the same thing. I found this solution elsewhere on the Internet as well. Basically, it seems hosts cannot take in a variable that is just defined, so the way around this is to have hosts reference a dynamic group, and then have the inputted hostname be added to the group via the add\_hosts task

---

<div class="post-metadata">

**Author:** ![a.srini](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@a.srini](https://forum.ansible.com/u/a.srini)\
**Post date:** [December 6, 2018, 5:24am UTC](https://forum.ansible.com/t/how-to-prompt-user-for-hostname-to-deploy-playbook-to/29130/5 "2018-12-06T05:24:44Z")

</div>

I am trying to pass 2 switch IP’s to get the available ports on both the switches. When I am trying to pass one switch IP, It is executing fine. When i am trying to pass 2 IP’s separating by split “SPACE” it is only executing on First switch IP.But not taking the second IP. Please see execution output and respective playbook

```auto
---
- hosts: localhost
  gather_facts: False
  vars_prompt:
  - name: ip_addr
    prompt: Please enter the switch name
    private: no
  vars_files:
    - ../vars/password.yml
  tasks:
  - add_host:
      name : "{{ item }}"
      groups: dynamically_created_hosts
    with_items: "{{ip_addr.split(' ')}}"
  - name: display all available ports
    display_available_ports:
        switch_ip: "{{ip_addr}}"
        user: "{{user}}"
        password: "{{password}}"
        vfid: -1
    register: result
  - debug: var=result

[root@san1 working]# ansible-playbook port_available_test.yml
Please enter the switch name: 17.16.15.16
PLAY [localhost] ********************************************************************************************************************************************
TASK [add_host] *********************************************************************************************************************************************
changed: [localhost] => (item=17.16.15.16)
TASK [display all available ports] **************************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************************
ok: [localhost] => {
    "result": {
        "available_ports": [
            {
                "name": "0/1",
                "port-type": "F_PORT"
            },
            {
                "name": "0/2",
                "port-type": "F_PORT"
            }
        ],
        "changed": false,
        "failed": false
    }
}
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0

[root@san1 working]# ansible-playbook port_available_test.yml
Please enter the switch name: 17.16.15.16 17.16.15.17
PLAY [localhost] ********************************************************************************************************************************************
TASK [add_host] *********************************************************************************************************************************************
changed: [localhost] => (item=17.16.15.16)
changed: [localhost] => (item=17.16.15.17)
TASK [display all available ports] **************************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************************
ok: [localhost] => {
    "result": {
        "available_ports": [
            {
                "name": "0/1",
                "port-type": "F_PORT"
            },
            {
                "name": "0/2",
                "port-type": "F_PORT"
            }
        ],
        "changed": false,
        "failed": false
    }
}
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0

```
