Add asnsible_host for each member of a group.

13 views
Skip to first unread message

Federico Olivieri

unread,
Nov 9, 2017, 6:19:27 AM11/9/17
to Ansible Project

Hi all

I have a group like this:

[webservers]
www[01:50].example.com

I would like to understand if there is a way to assign ansible_host variable for each member of the group like:

www01.example.com ansible_host=1.1.1.1
www02.example.com ansible_host=1.1.1.2
www03.example.com ansible_host=1.1.1.3

Obviously I would like to do it in one line, without define variable for each host like using regex

[webservers] www[01:50].example.com ansible_host=1.1.1.[1:50]

Brian Coca

unread,
Nov 9, 2017, 10:15:39 AM11/9/17
to Ansible Project
No, hostvars do not support ranges, but you can define a single
hostvar for the range and use templating to get what you want.


www[01:50].example.com ansible_host={{'1.1.1.' +
inventory_hostname.split('.')[0]|regex_replace('www','') }}

^ have not tested it, but something like this should work.


--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages