Hi All,

Thankyou for the response, I used Rigler's set_fact to determine the 
hostgroup like below

- name: Set Group
  set_fact:
    group: |
      {% if c == 1 %}
      grp1
      {% elif c ==2 %}
      grp3
      {% else %}
      grp4
      {% endif %}

- name: Run the playbook
  hosts: "{{ hostvars.localhost.group }}"
  roles:
    - myplaybook.yml

My inventory file is like below:

hosts.ini

[appdagents]
grp1
grp2
grp3
.
.
.
grp8

[grp1:vars]
var1="asfadfdsg"

[grp2:vars]
var1="gsfgfh"
.
.
.

[grp1]
host1/db1 ansible_host=host1 db=db1
host1/db2 ansible_host=host1 db=db2

.
.
.
etc

Im getting below error.

 [WARNING]: **Could not match supplied host pattern, ignoring: grp1**


I did look into few github issues and it says any commented lines in hosts.ini 
could cause a problem and I dont have any commented lines. 

Kindly help


On Saturday, 15 June 2019 23:09:39 UTC+5:30, Karl Auer wrote:
>
> Or maybe a series of set_fact: statements:
>
> - set_fact
>      group: 'BAD'
>
> - set_fact:
>      group: grp1
>   when: c == 1
> - set_fact:
>      group: grp2
>   when: c == 2
>
> - set_fact:
>      group: grp3
>   when: c == 3
>
> [...etc...]
>
> - fail:
>      msg: "Invalid value for 'c' or 'c' not set!"
>   when: group == 'BAD'
>
> Regards, K.
>
> On Mon, Jun 3, 2019 at 4:52 PM S C Rigler <[email protected] <javascript:>> 
> wrote:
>
>> In your example of what you're trying to do, you're setting "group" 
>> inside of the template but never returning it to the task.  What you might 
>> want is something like this:
>>
>> - name: Set Group
>>   set_fact:
>>     group: |
>>       {% if c == 1 %}
>>       grp1
>>       {% elif c ==2 %}
>>       grp3
>>       {% else %}
>>       grp4
>>       {% endif %}
>>
>> -- Steve
>>
>> On Mon, Jun 3, 2019 at 12:49 AM Ansar Sahit <[email protected] 
>> <javascript:>> wrote:
>>
>>> Hi All,
>>>
>>> Im trying to choose ansible inventory host group conditionally.
>>>
>>> - name: Set group
>>>   set_fact:
>>>     group: |
>>>         {% if c == 1 %}
>>>         {% set group = 'grp1' %}
>>>         {% elif c == 2 %}
>>>         {% set group == 'grp3' %}
>>>         {% else %}
>>>         {% group == 'grp4' %}
>>>         {% endif %}
>>>
>>> - debug:
>>>      var=group
>>>
>>>
>>> This doesnt seem to work.
>>>
>>> Whats the right way to use if-else with ansible set??
>>>
>>> Kinldy assist.
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected] <javascript:>.
>>> To post to this group, send email to [email protected] 
>>> <javascript:>.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/ansible-project/de181027-dc27-4190-8d1e-1e5feec12546%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/ansible-project/de181027-dc27-4190-8d1e-1e5feec12546%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/CAFbiokdBbOsnz5nVCMbX-9ouTTL4K8jL2WNoQKz-F-MBYZq0fg%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/CAFbiokdBbOsnz5nVCMbX-9ouTTL4K8jL2WNoQKz-F-MBYZq0fg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> -- 
> Karl Auer
>
> Email  : [email protected] <javascript:>
> Website: http://2pisoftware.com
>
> GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
> Previous: 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/8241a642-84aa-44bb-84a9-a25a1b2bcd92%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to