'How to run ansible playbook with host missing in inventory file

I am trying to run some simple playbooks to collect data from new devices that are not yet in the inventory file. This is an example playbook I use to collect data from devices that are already in the inventory file:

- name: "Demonstrate connecting to ASA"
  connection: ansible.netcommon.network_cli
  gather_facts: false
  hosts: asa

  tasks:
    - name: Gather facts (asa)
      cisco.asa.asa_command:
        commands:
          - show inventory
      register: asa_vars

But what I want to achieve is that I can give any IP address of a system as variable to the playbook and the playbook executes the task on this IP which is then not in the inventory file. I am struggling how to archive that and tell ansible to use that given IP address instead of a host from inventory.

What would I use for the "host:" statement and how could I add additional parameters, for example to use a SSH bastion host, that I would usually add to an inventory file?



Solution 1:[1]

In your playbook:

- name: "Demonstrate connecting to ASA"
  connection: ansible.netcommon.network_cli
  gather_facts: false
  hosts: "{{ host }}"

On the command line:

ansible-playbook myplaybook.yml --extra-vars "host=8.8.8.8"

Solution 2:[2]

Maybe you're just missing the somewhat arcane syntax for specifying the inventory on the command line?

If -i is a comma separated list, it will treat it as a list of hosts rather than a file, so calling:

ansible-playbook -i 192.168.1.103, play.yml

With a trailing comma will run that play on a single, one off host.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gé Fa
Solution 2 Matt Blaha