title: How to add a host that is a variable before executing a module in ansible?
body: <p>This is the task I want to execute</p>
  
  <pre><code>- hosts: variable-vm
    become: yes
    tasks:
     - name: Transfer file from local to remote
       copy:
         src: /tmp/file
         dest: /home/ansible/
  </code></pre>
  
  <p>Now before the user executes my playbook I want him to enter </p>
  
  <pre><code>--extra-vars "server=variable-vm"
  </code></pre>
  
  <p>In other tasks this was defined at the beginning of the yml file like this:</p>
  
  <pre><code>- hosts: webservers
    vars:
      my_server: "{{ server }}"
    become: yes
    tasks:
  </code></pre>
  
  <p>and when I needed to I would deligate the task to that variable vm with</p>
  
  <pre><code> delegate_to: '{{ my_server }}'
  </code></pre>
  
  <p>Keep in mind that I have defined the variable-vm inside the inventory under "webservers"</p>
  
  <p>Essentially my machine is connected to dozens of servers with ssh keys and the servers are defined under /etc/hosts. All I want from the user is to enter the name of the server that is defined in /etc/hosts and use that variable as an input for "hosts:" before the copy task.</p>
  
  <p>As far as I can see there is no other way to specify the remote machine I want to copy the file to.</p>