title: How to change play_hosts from playbook
body: <p>I have a file with two playbooks. Inventory is generated dynamically and there is no possibility to change it before starting playbook.</p>
  
  <p>Run with command:</p>
  
  <pre><code>ansible-playbook -b adapter.yml --limit=host_group
  </code></pre>
  
  <p>adapter.yml</p>
  
  <pre><code>- name: Prepare stage
    hosts: all
    # The problem is that the inventory contains hosts in the format "x.x.x.x" ie physical address.
    # I need to run a third-party role.
    # But, it needs hosts in the format "instance-alias", that is, the name of the instance.
  
    tasks:
    # for this I create a new host group
    - name: Add host in new format
      add_host:
        name: "{{ item.alias }}"
        host: "{{ item.ansible_host }}"
        groups: new_format_hosts
      with_items: "{{ groups.all }}"
  
   # I create a new play host group that matches the previous one in a new format
    - name: Compose new play_hosts group
      add_host:
        name: "{{ item.alias }}"
        groups: new_play_hosts
      when: item.ansible_host in play_hosts
      with_items: "{{ groups.all }}"
  
  - name: Management stage
    hosts: new_format_hosts
    # in this playbook I want to change the composition
    # of the target hosts and launch an external role
    vars:
      hostvars: "{{ hostvars }}"
      play_hosts: "{{ groups.new_play_hosts }}" # THIS DONT WORK
  
    - name: Run external role
      import_role:
        name: role_name
        tasks_from: file_name
  
  </code></pre>
  
  <p>But I can’t change play_hosts so that the launched role uses only new hosts.</p>
  
  <p>How to fix it?</p>