2

I have a simple inventory in hosts:

dockermachine ansible_host=10.10.10.10

I need to be able to provide any other IP I wish from the command line, overwriting the default, something like: ansible-playbook -i hosts@dockermachine.ansible_host=11.11.11.11 site.yml

How can I do this?

user1876484
  • 610
  • 6
  • 16
  • Possible duplicate of [Override hosts variable of Ansible playbook from the command line](https://stackoverflow.com/questions/33222641/override-hosts-variable-of-ansible-playbook-from-the-command-line) – 3sky Jun 24 '19 at 10:08
  • @3sky: I tried this, and I get "skipping: no hosts matched", since the new IP is not inside of hosts. In that question there was a need to switch between host groups "web"/"droplets" which were defined in advance. I need to provide an arbitrary IP on the command line. – user1876484 Jun 24 '19 at 10:12
  • Because: `I don't think Ansible provides this feature` :) If You really need that, try to modify `hosts` with sed, and run playbook. `sed -i.bak "/dockermachine ansible_host=10.10.10.10/c\dockermachine ansible_host=11.11.11.11" hosts | ansible-playbook ...| mv hosts.bak hosts` – 3sky Jun 24 '19 at 10:28

1 Answers1

5

If your inventory is really that simple, you can probably live without the hostname. Remove the entry from your current default inventory. Make sure your playbook is targeted against the all group and launch your playbook with a single host ip inventory:

ansible-playbook -i 10.10.10.11, my_playbook.yml

Note: the trailing comma after the IP is not a mistake, it needs to be there so that the IP after -i parameter is interpreted as a comma separated list of hosts and not as an inventory file path.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66