Yes, there is a "groups" variable which is a dictionary with group name
keys where the values are a list of hosts in that group. That is,
groups[dev_cluster]
= ["server1", "server2", "server3"]. So you can do a inventory_hostname ==
groups[dev_cluster][0] comparison for the first item.

Note: I have not tested this in playbooks, only templates, and you might
run into bugs if your group has zero hosts. You also need to consider the
scenario where a host is in more than one group.

On Mon, Oct 2, 2017 at 2:06 AM, Marc L <marc.c.l...@gmail.com> wrote:

> I have some tasks that should only be processed if the host
> (inventory_hostname) is the first in the group of servers it's defined in.
>
> For example, here is my hosts file. Note that the group names are not
> fixed - the Role I want to use them in cannot rely on the name of the
> groups - it needs to be a bit more dynamic than that:
>
> *Hosts*
> server1
> server2
> server3
>
> [dev_cluster]
> server1
> server2
> server3
>
>
> [frontend_nodes]
> server1
> server2
>
>
> [frontend_nodes:vars]
> node_type_prefix=
> node_port=19043
> node_protocol=https
>
>
> [backend_nodes]
> server3
>
>
> [backend_nodes:vars]
> node_type_prefix=backend/
> node_port=19043
> node_protocol=https
>
> I have a playbook which I call like so, by passing in the name of the two
> groups - again - the name of these groups could vary, or I could add a 3rd
> or 4th group in.
>
> Playbook
> ---
> - hosts: frontend_nodes,backend_nodes
>   connection: local
>   gather_facts: no
>   vars:
>     ohp_auth_user: "username"
>     ohp_auth_password: "password"
>   roles:
>     - { role: ansible-ohp-config-backup, node_name: "{{
> inventory_hostname }}" }
>
>
>
> Within my role, I have a bunch of tasks that I only want to run if the
> hostname is the first in the the group it is defined in that has been
> passed by the playbook.
> So given the example above, only run on server1 (the first in
> frontend_nodes), and server 3 (the first in backend_nodes)
>
> I was thinking the syntax would be something like:
>
>   - name: Get OHP list of problems
>       uri:
>         url: "{{ node_protocol }}://{{ node_name }}:{{ node_port }}/{{
> node_type_prefix }}monitoring/problems"
>         method: GET
>         user: "{{ ohp_auth_user }}"
>         password: "{{ ohp_auth_password }}"
>         force_basic_auth: yes
>         validate_certs: no
>         status_code: 200
>         timeout: 10
>         return_content: yes
>         dest: "{{ backup_folder }}/{{ node_name }}-problems.xml"
>       when inventory_hostname = current_group[0]
>       delegate_to: localhost
>
>
>
>
> Is there any way to achieve this?
>
> Worst case scenario I might need to pass a list of the "first" hosts into
> the role as a parameter and evaluate there.
>
> --
> 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 ansible-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/0aafd66c-825b-4ab9-b157-6024abbc28f7%40googlegroups.
> com
> <https://groups.google.com/d/msgid/ansible-project/0aafd66c-825b-4ab9-b157-6024abbc28f7%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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAPJuRw8PPJvzMup%2Bjm8_WMQqCnPXvzvXoVne6E2YygSMEW5y2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to