Issue
I use this inventory script: xezpeleta/Ansible-Proxmox-inventory.
It works fine but now I want to add a new group based on proxmox_name
field value.
I found the lines to duplicate in the script.
I duplicate this:
# Create group 'running'
# so you can: --limit 'running'
status = results['_meta']['hostvars'][vm]['proxmox_status']
if status == 'running':
if 'running' not in results:
results['running'] = {
'hosts': []
}
results['running']['hosts'] += [vm]
into this:
# Create group 'bddservers'
# so you can: --limit 'bddservers'
bddservers = results['_meta']['hostvars'][vm]['proxmox_name']
if bddservers == 'x-xxxxx-x-Bxx':
if '.-....-.-B..' not in results:
results['bddservers'] = {
'hosts': []
}
results['bddservers']['hosts'] += [vm]
to create a group consisting of only BDD servers.
I want to base it on the hostname server (proxmox_name
field) but when I try pattern '*B*'
it doesn't work. If I replace it with a single hostname server it works, I have a group bddservers with only the hostname I set.
How can I replace the single hostame by a pattern? The naming convention is like this: x-xxxxx-x-Bxx
when the B
is for BDD servers only. The code is in Python 3.
Solution
You can do this without modification of the inventory script:
- name: Add hosts to the BDD group
add_host:
name: "{{ inventory_hostname }}"
groups: bddservers
when: proxmox_name is regex('.*-.*-.*-B.*')
Also, note that your original regex does not match the example host name you give in your question. I simplified it so you can use it to check and modify accordingly later.
Speaking of your original intention to add the hosts to the group from the script - you just need to fix the condition to something like this:
import re
...
name = results['_meta']['hostvars'][vm]['proxmox_name']
if re.fullmatch('.*-.*-.*-B.*', name):
if 'bddservers' not in results:
results['bddservers'] = {
'hosts': []
}
results['bddservers']['hosts'] += [vm]
Answered By - Alexander Pletnev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.