0

I hava this situation:

A play that run in localhost use include_task for create, on the fly with add_host, two sub-group extracting only one host from two group that are present in the inventory file. Another play in the same yaml file use this group as hosts (host:sub-group).

This is the inventory file:

    all:
  children:
    group_one:
      hosts:
        hostA01:
          ansible_host: host1a
        hostA02:
          ansible_host: host2a
        hostA03:
          ansible_host: host3a
      vars:
        cluster: hosta
        vip: 192.168.10.10
        home: /cluster/hosta
        user: usr_hosta
        pass: pass_hosta
    group_two:
      hosts:
        hostB01:
          ansible_host: host1b
        hostB02:
          ansible_host: host2b
        hostB03:
          ansible_host: host3b
      vars:
        cluster: hostb
        vip: 192.168.10.20
        home: /cluster/hostb
        user: usr_hostb
        pass: pass_hostb    
    other groups...

I have created the sub groups, with add_host, for each group in inventoty file. The name the sub-groups add the prefix "sub-" to inventory original groups, like sub-(one/two/etc..)

In hostvars i retrieve this situation:

"groups": {
            "all": [
                "hostA01", 
                "hostA02", 
                "hostA03", 
                "hostB01", 
                "hostB02", 
                "hostB03",
                "other_host_from _other groups"
            ], 
            "group_one": [
                "hostA01", 
                "hostA02", 
                "hostA03"
            ], 
            "group_two": [
                "hostB01", 
                "hostB02", 
                "hostB03"
            ], 
            "other_group": [
                "other_host", 
                .....
            ], 
            "sub-group_one": [
                "hostA01"
            ], 
            "sub_group_two": [
                "hostB01"
            ], 
            "sub-other_group": [
                "other_first_host"
            ], 
            "ungrouped": []
        }, 
        "vars_for_group": {
            "group_one": {
                "cluster: hosta
                "vip: 192.168.10.10
                "home: /cluster/hosta
                "user: usr_hosta
                "pass: pass_hosta, 
                "ansible_host": "host1a", 
                "host": "hostA01"
            }, 
           "group_two": {
                "cluster: hostb
                "vip: 192.168.10.20
                "home: /cluster/hostb
                "user: usr_hostb
                "pass: pass_hostb, 
                "ansible_host": "host1b", 
                "host": "hostB01"
            },
           "otehr_groups": {
                .......
            }, 
        }, 
        "inventory_hostname": "127.0.0.1", 
        "inventory_hostname_short": "127", 
        "module_setup": true, 
        "playbook_dir": ""/home/foo/playbook", 
        "choice": "'sub-two'"
    }
}

The environment variable "choice" in the last line of hostvars derives from other tool and indicates the group on which the final user wants to operate (one, two, ..., all).

Now, my playbook is:

 ---
- hosts: 127.0.0.1
  become: yes
  gather_facts: yes
  remote_user: root
  
  
  tasks:

    - name: include news_groups
      ansible.builtin.include_tasks:
        newsgroups.yaml
      vars:
        choice: "{{ lookup('env','CHOICE') }}"

  

- hosts: "{{ hostvars['localhost']['groups']['{{ hostvars['localhost']['choice'] }}'] }}"
  name: second_play
  become: yes
  gather_facts: no
  remote_user: root
  
  tasks:
  
  

- hosts: all
  name: other play
  gather_facts: no
  vars:
    other_vars:...

    

  tasks:
    .....

Unfortunately this line don't work.

- hosts: "{{ hostvars['localhost']['groups']['{{ hostvars['localhost']['choice'] }}'] }}"

I have made several attempts with many different configuration and different sintax (without {{...}}, with or without "double quotes" and 'single quote' but it always seems syntactically wrong or still cannot find the group indicated by the variable. Or, maybe could it also be that the approach is wrong? Any suggestions?

Thanks in advance

ferdinand
  • 21
  • 3
  • 1
    One: [mustaches do not stack](https://stackoverflow.com/a/67408681/2123530), two, the `vars` are only visible on task you define them on, if defined at that level. If you want `choice` to persist in the facts on `localhost`, you need to use a `set_fact`. – β.εηοιτ.βε Oct 17 '22 at 14:16
  • Thank you. But i have used `set_fact` and `choice` persist whitout problem, as can be seen from hostvars debug. The problem is take the `hostvars['localhost']['group']` with the key `[hostvars['localhost']['choice']`. In pseudo-code the the question is how to do this `hosts: "{{ hostvars['localosth']['group'][(hostvar['localhost']['choice']) }}`. I hope it is clearer now. – ferdinand Oct 17 '22 at 14:34
  • `hostvars['localhost']['groups'][hostvars['localhost']['choice']]`. That was the _one_. – β.εηοιτ.βε Oct 17 '22 at 14:38
  • This said, since you just want to select a group that is named after a choice of some sort and that groups are not under hosts, you could simply do: `- hosts: "{{ hostvars['localhost']['choice'] }}"` or `- hosts: "{{ hostvars.localhost.choice }}"` – β.εηοιτ.βε Oct 17 '22 at 14:40
  • 1
    Thanks sir. this works. Sorry, but I must be really tired, I've tried a lot of syntax but not this simple one – ferdinand Oct 17 '22 at 14:45

0 Answers0