Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible first hostname of groups

I am not sure on how to find the first ansible hostname from group_names. Could you kindly advise me on how to do it?

hosts

[webservers]
server1
server2
server3

[webserver-el7]
server4
server5
server6

And i have 2 different playbook for each host groups

playbook1.yml

- name: deploy app
  hosts: webservers
  serial: 8
  roles:
    - roles1

playbook2.yml

- name: deploy app
  hosts: webservers-el7
  serial: 8
  roles:
    - roles1

the problem is that i have delegate task to first host of each group. previously i only used webservers group, so it was much easier by using the task below

- name: syncing web files to {{ version_dir }}
  synchronize:
    src: "{{ build_dir }}"
    dest: "{{ version_dir }}"
    rsync_timeout: 60
  delegate_to: "{{ groups.webservers | first }}"

If i have 2 different group_names, how can i select the first one of each group? so it can be more dynamic

like image 549
AlamHo Avatar asked Jan 10 '17 14:01

AlamHo


People also ask

How do I find my Ansible hostname?

In this post, we are going to see two built-in variables of ansible mostly used in Ansible playbooks and they are inventory_hostname and ansible_hostname while both these variables are to give you the hostname of the machine.

What is host group in Ansible?

Ansible uses a combination of a hosts file and a group_vars directory to pull variables per host group and run Ansible plays/tasks against hosts. group_vars/all is used to set variables that will be used for every host that Ansible is ran against.

How do I control Ansible playbook only on specific hosts?

Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.

How do you mention hosts in Ansible playbook?

You can use either a comma ( , ) or a colon ( : ) to separate a list of hosts. The comma is preferred when dealing with ranges and IPv6 addresses. targets all machines in the groups 'webservers' and 'dbservers' that are also in the group 'staging', except any machines in the group 'phoenix'.


2 Answers

If you want the first host of current play to be a kind of master host to sync from, I'd recommend another approach: use one of play_hosts or ansible_play_hosts (depending on your Ansible version) variables. See magic variables.
Like delegate_to: "{{ play_hosts | first }}".

The thing is when you say hosts: webservers-el7 to Ansible webservers-el7 is a pattern here. Ansible search for hosts to match this pattern and feed them into Play. You may have written webservers-el* as well. So inside Play you don't have any variable that will tell you "I'm running this Play on hosts from group webserver-el7...". You may only make some guess work analyzing group_names and groups magic variables. But this become clumsy when you have one host in several groups.

For hosts in single group only, you may try: groups[group_names | first] | first

like image 95
Konstantin Suvorov Avatar answered Oct 02 '22 05:10

Konstantin Suvorov


To get any element from group, use group[group_name][0...n].

This will get the first element from the group.

- debug: msg="{{ groups['group_name'][0] }}"
like image 35
Feroz Avatar answered Oct 02 '22 05:10

Feroz