how to prompt user for hostname to deploy playbook to?

34 views
Skip to first unread message

Tuyen Nguyen

unread,
Nov 27, 2018, 2:26:16 PM11/27/18
to Ansible Project
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.

---
- hosts: all
  gather_facts: no

  vars_prompt:
  - name: "var_hostname"
    prompt: "Enter hostname to deploy to"
    private: no

- hosts: "{{ var_hostname }}"
  gather_facts: no

- tasks:
.
.
.


When I try that, it prompts for the hostname according to vars_prompt, but when it gets down to the tasks section, and attempts to run, it says that var_hostname is not defined.

How can I get it so that var_hostname is known to be defined and taken from prompt?  If I were to run the playlist with --extra-vars 'var_hostname=hostname' in the command line, then it does work because it is passed through as a variable on the command line, but I am hoping to be able to ask the user instead of needing to put it on the command line.

John Harmon

unread,
Nov 27, 2018, 4:57:26 PM11/27/18
to Ansible Project
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.
---
# Sample playbook structure
- hosts: "{{ host }}"
#  gather_facts: false
#  vars:
#    interval: 10
  tasks
:
   
- name: Sample Name
      copy
:
        content
: "Wussup"
        dest
: /tmp/Jeremy_Was_Here.txt
        force
: yes
        owner
: root
       
group: root
        mode
: 0555
      notify
: Wussup

  handlers
:
   
- name: Wussup
      service
:
        name
: whatever
        state
: restarted

When I call my playbook I specify the host:
ansible-playbooks /path/to/sample.yml -e host=somehost

Let me know if that doesn't help and I can keep looking into the issue you are running into.


John Harmon

unread,
Nov 27, 2018, 5:02:57 PM11/27/18
to Ansible Project
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):

---
- hosts: "{{ var_hostname }}"
  gather_facts
: yes

  vars_prompt
:

 
- name: var_hostname
    prompt
: "Enter hostname to deploy to"
   
private: no


  tasks
:
 
- name: TEST
    debug
:
     
var: ansible_fqdn


Tuyen Nguyen

unread,
Dec 5, 2018, 10:24:24 AM12/5/18
to Ansible Project
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

---
- hosts:  localhost
  gather_facts: no

  vars_prompt:
  - name: var_hostname
    prompt: "Enter hostname to deploy to"
    private: no

  tasks:
  -  add_host:
       hostname: "{{ var_hostname }}"
       groups: dynamic_list
     with_items: "{{ var_hostname.split(',') }}"

  - hosts: dynamic_list
     gather_facts: no

    tasks:
....

srinivas a

unread,
Dec 6, 2018, 12:24:44 AM12/6/18
to Ansible Project
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

---
- 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
Reply all
Reply to author
Forward
0 new messages