What does the output from the dynamic inventory software look like?

Hi,

I am running Ansible 2.9.7 and am looking at the dynamic inventory scripts. I don’t have any of the various software listed (AWS, OpenStack etc) in the examples section and I need something where I can automatically set the remote IP address at the time that the playbook is executed, ideally after prompting the user for this information.

I have managed to craft together a simple Python script with hard-coded data (for test purposes) but I cannot fathom the output, other than it should be in JSON format. Nor can I find this documented anywhere.

Does anyone have any ideas, suggestions or pointers?

Failing that, can you suggest a way to do what I want, ask the user for the IP address of the remote server and also the software they want to install? (Yeah I know, two questions instead of one:-) )

This is largely documented at https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-inventory-scripts

Additionally there is a JSON schema file describing the JSON data structure for inventory scripts located at https://github.com/ansible/ansible/blob/devel/examples/inventory_script_schema.json

... ideally after prompting the user for this information.
... ask the user for the IP address of the remote server and also the
software they want to install?

This is an example of the playbook you might be looking for, I think

  - hosts: localhost
    gather_facts: false
      vars_prompt:
      - name: "remote_host"
        prompt: "Enter FQDN or OP of the remote host"
        private: no
      - name: "pkg"
        prompt: "Enter name of the package"
        private: no
    tasks:
      - add_host:
          hostname: "{{ remote_host }}"
          groups: my_group_01
          sw: "{{ pkg }}"

  - hosts: my_group_01
    tasks:
      - debug:
          msg: Install {{ sw }} on {{ inventory_hostname }}

gives

  > ansible-playbook pb.yml
  Enter FQDN or OP of the remote host: test_01
  Enter name of the package: apache

  PLAY [localhost] *********************************

  TASK [add_host] **********************************
  changed: [localhost]

  PLAY [my_group_01] *******************************

  TASK [Gathering Facts] ***************************
  ok: [test_01]

  TASK [debug] *************************************
  ok: [test_01] => {
      "msg": "Install apache on test_01"
  }

  PLAY RECAP ***************************************
  localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0
  ignored=0
  test_01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0
  ignored=0

Thanks Matt, that looks exactly like what I need.

Thanks for the response Vladimir. I had actually found and tried using that code, it works fine for a single prompt but not for two, or at least not where there second input variable is used in a role name. I was after:

> - hosts: localhost
> tasks:
> - add_host:
> hostname: "{{ remote_host }}"
> groups: my_group_01
> sw: "{{ pkg }}"
>
> - hosts: my_group_01
> tasks:
> - debug:
> msg: Install {{ sw }} on {{ inventory_hostname }}

- hosts: localhost
  ...
  tasks:
    - name: Add remote host to new server list
      add_host:
        name: "{{ remote_host }}"
        groups: server_list
           
- hosts: server_list
  become: True
  roles:
  - role: common
  - role: "{{ pkg }}"

but it fails with "ERROR! 'pkg' is undefined"

In my example the variable "sw" in the "debug" task works because it was
declared in the first play's task "add_host". In your code the variable "pkg"
is in the "hostvars" of localhost only. See "hostvars"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#caching-facts