How to pass 'hosts' value to an imported or included playbook?

I’ve playbook A to include roles to conduct an operation. Now I want to create playbook B to import/include playbook A but want to pass ‘hosts’ value to A. In other words, I want to make A to work dynamically with different hosts depending on the playbooks (B/C/D/E/etc.) that include/import it.

I’ve searched around and couldn’t find a good way to do it, I’m running Ansible 2.7.10

I tried in Playbook B like [ -import_playbook playbook-A.yml my-host=‘host_B_name’] (in playbook A [ hosts: {{‘my-host’}}]), but got error: Invalid variable name in vars specified for PlaybookInclude: ‘my-host’ is not a valid variable name

Any help or idea here?

Thanks in advance!

IMHO, this is easier manageable from outside of the playbook. See for example
https://ansible-runner.readthedocs.io/en/latest/python_interface.html#usage-examples

It's possible to dynamically modify the inventory and change the predefined
hosts in the Play A. For example, in Play B, use 'add_host' to add hosts to a
group(s) and use it in Play A
https://docs.ansible.com/ansible/latest/modules/add_host_module.html#add-host-add-a-host-and-alternatively-a-group-to-the-ansible-playbook-in-memory-inventory

  - name: Play B
    hosts: hosts_in_play_B
    tasks:
        ...
      - name: Add hosts to group hosts_in_play_A
        add_host:
          name: '{{ item }}'
          groups: hosts_in_play_A
        loop: '{{ list_of_hosts_in_play_A }}'

  - name: Play A
    hosts: hosts_in_play_A
    tasks:
        ...

Next option, instead of 'add_host', is to dynamically change the inventory
and 'meta: refresh_inventory'
https://docs.ansible.com/ansible/latest/modules/meta_module.html#meta-execute-ansible-actions

* At the moment, it's not possible to 'include' a playbook. There is only
  'import_playbook'. There is no difference between importing a play and
  putting the code in-line.

* 'import_playbook' isn't a task.

HTH,

  -vlado