How to prompt user for a target host in Ansible?

ansible prompt for input variable
ansible prompt yes no
ansible multiline prompt
ansible tower vars_prompt
ansible pause: prompt register
ansible pause: prompt default
ansible register
ansible responses example

I want to write a bootstrapper playbook for new machines in Ansible which will reconfigure the network settings. At the time of the first execution target machines will have DHCP-assigned address.

The user who is supposed to execute the playbook knows the assigned IP address of a new machine. I would like to prompt the user for is value.

vars_prompt module allows getting input from the user, however it is defined under hosts section effectively preventing host address as the required value.

Is it possible without using a wrapper script modifying inventory file?

The right way to do this is to create a dynamic host with add_host and place it in a new group, then start a new play that targets that group. That way, if you have other connection vars that need to be set ahead of time (credentials/keys/etc) you could set them on an empty group in inventory, then add the host to it dynamically. For example:

- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: target_host
    prompt: please enter the target host IP
    private: no
  tasks:
    - add_host:
        name: "{{ target_host }}"
        groups: dynamically_created_hosts

- hosts: dynamically_created_hosts
  tasks:
  - debug: msg="do things on target host here"

Prompts, Prompts¶. When running a playbook, you may wish to prompt the user for certain input, and can do so with the 'vars_prompt' section. The user input is hidden by default but it can be made visible by setting private: no. Note. Prompts for individual vars_prompt variables will be skipped for any variable that is already defined through the command line --extra-vars option, or when running from a non-interactive session (such as cron or Ansible Tower).

You could pass it with extra-vars instead.

Simply make your hosts section a variable such as {{ hosts_prompt }} and then pass the host on the command line like so:

ansible-playbook -i inventory/environment playbook.yml --extra-vars "hosts_prompt=192.168.1.10"

Or if you are using the default inventory file location of /etc/ansible/hosts you could simply use:

ansible-playbook playbook.yml --extra-vars "hosts_prompt=192.168.1.10"

Prompting for hosts, When running a playbook, you may wish to prompt the user for certain input, and hosts: all vars_prompt: - name: username prompt: "What is your username? A pattern usually refers to a set of groups (which are sets of hosts) – in the above case, machines in the “webservers” group. Anyway, to use Ansible, you’ll first need to know how to tell Ansible which hosts in your inventory to talk to. This is done by designating particular host names or groups of hosts.

Adding to Matt's answer for multiple hosts.

input example would be 192.0.2.10,192.0.2.11

- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: target_host
    prompt: please enter the target host IP
    private: no
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: dynamically_created_hosts
      with_items: "{{ target_host.split(',') }}"


- hosts: dynamically_created_hosts
  tasks:
  - debug: msg="do things on target host here"

variable for hosts from vars_prompt no longer accepted · Issue , hosts: "{{target}}" and. ansible-playbook -e "target=myhost" . but vars_prompt won't work because of evaluation order. Sign in to reply. You have an Ansible prompt module which will prompt the user for the value when the play is run. You can give values as extra arguments to the command line arguments. etc. In this session, we will focus on Ansible prompts. A basic example of Ansible Prompts. To ask for a user input, we will use the vars_prompt section. Something like below,

Disclaimer: The accepted answer offers the best solution to the problem. While this one is working it is based on a hack and I leave it as a reference.

I found out it was possible use a currently undocumented hack (credit to Bruce P for pointing me to the post) that turns the value of -i / --inventory parameter into an ad hoc list of hosts (reference). With just the hostname/ip address and a trailing space (like below) it refers to a single host without the need for the inventory file to exist.

Command:

ansible-playbook -i "192.168.1.21," playbook.yml

And accordingly playbook.yml can be run against all hosts (which in the above example will be equal to a single host 192.168.1.21):

- hosts: all 

The list might contain more than one ip address -i "192.168.1.21,192.168.1.22"

ansible: run a task on a target host(s), ISSUE TYPE Bug Report COMPONENT NAME playbook ANSIBLE VERSION hosts: "{{ target }}" vars_prompt: - name: target prompt: "Enter  The second line defines the location of the SSH private key used to connect to the remote host. The last line instructs Ansible to use ubuntu as the username when connecting to the remote host. By default, Ansible will use the currently-logged username to establish connection. Now, we need to create the hosts file.

Mitogen for Ansible, SYNOPSIS. ansible <host-pattern> [-m module_name] [-a args] [options] Prompt for the password to use with --sudo, if any (deprecated, use become). --​ask-vault- Use this USERNAME to login to the target host, instead of the current user. Prompts¶ When running a playbook, you may wish to prompt the user for certain input, and can do so with the ‘vars_prompt’ section. A common use for this might be for asking for sensitive data that you do not want to record.

Restrict user providing asterisk * on the prompt ansible awx, One connection is used per target, in addition to one sudo invocation per user account. tasks delegating many target hosts to a single machine and user account. of a login or password prompt on an exotic or non-English operating system,  Manage user accounts and user attributes. For Windows targets, use the win_user module instead. The behavior is the same as userdel --force, check the man page for userdel on your system for details and support. This will check /etc/passwd for an existing account before invoking commands. If the local account database exists somewhere other

ansible - run a task on a target host(s), Below solution worked for me . --- - hosts: "{{ target }}" gather_facts: no tasks: - name: You cannot pass asterisk as target hosts meta: end_play  Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists know as inventory. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against. The default location for inventory is a file called /etc/ansible/hosts.

Comments
  • The inventory/environment part is just an example (as with playbook.yml). If you don't provide an inventory file with -i then Ansible will simply pick up the default inventory file /etc/ansible/hosts which is not normally what you want even when you are specifying the hosts using extra-vars
  • It's an example about how to pass extra-vars, not a "here's the exact line you need to type". And an inventory is a necessary argument, it just defaults to /etc/ansible/hosts and if that file doesn't exist then it will fail.
  • @techraf: Your suggested edits were rejected in the review queue. ydaetskcoR has nothing to do with that. Edits like that are simply not accepted on SO. Since this answer does currently provide the answer for you, you really should accept it, for future visitors' sake.
  • Your answer was deleted because it was a copy-paste of this one. Plagiarism like that is not allowed on SO. The OP of this answer now added your specific case to the answer, so I really see no reason not to accept this one.
  • I don't like this approach because if someone runs the playbook without parameters by mistake the hell breaks loose.