Search code examples
dynamicansibleparameter-passinghost

Can Ansible match hosts passed as parameter without using add_hosts module


Is it possible to pass the IP address as parameter 'Source_IP' to ansible playbook and use it as hosts ?

Below is my playbook ipinhost.yml:

---
- name: Play 2- Configure Source nodes
  hosts: "{{ Source_IP }}"
  serial: 1
  tasks:
   - name: Copying from "{{ inventory_hostname }}" to this ansible server.
     debug: 
       msg: "MY IP IS: {{ Source_IP }}" 

The playbook fails to run with the message "Could not match supplied host pattern." Output below:

  ansible-playbook ipinhost.yml -e Source_IP='10.8.8.11'
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 [WARNING]: Could not match supplied host pattern, ignoring: 10.8.8.11


PLAY [Play 2- Configure Source nodes] ***********************************************************************************************************************
skipping: no hosts matched

PLAY RECAP **************************************************************************************************************************************************

I do not wish to use ansible's add_host i.e i do not wish to build a dynamic host list as the Source_IP will always be a single server.

Please let me know if this is possible and how can my playbook be tweaked to make it run with hosts matching '10.8.8.11'?


Solution

  • If it is always a single host, a possible solution is to pass a static inline inventory to ansible-playbook.

    1. target your play to the 'all' group. => hosts: all
    2. call your playbook with an inlined inventory of one host. Watch out: the comma at the end of IP in the command is important:
    ansible-playbook -i 10.8.8.11, ipinhost.yml