-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for hostname templating #474
Conversation
Hey @Gianlu - I'm not following your use-case entirely. For example, you can already do this in native Ansible/Jinja in a playbook: - hosts: "{{ target_host | lower }}"
tasks:
- name: Example task
debug:
msg: "This task runs on host {{ target_host }}" In other words.. post processing should either be done using the compose function or in a playbook. Here is another example that may work for you: Let's say we add this to our dynamic inventory file: compose:
ansible_inventory_hostname: "{{ hostname | lower }}" Then in a playbook, you could do something like this: - hosts: all
tasks:
- name: Example task
debug:
msg: "This task runs on host {{ ansible_inventory_hostname }}" Hopefully this resonates with your end goal. |
Hello, $ tree inventory
inventory
├── 00_virtual_manager01.yaml
├── 00_virtual_manager02.yaml
├── 00_virtual_manager03.yaml
├── 01_our_monitoring.yml
├── 10_cloud.yml
├── 11_our.falcon_hosts.yml
├── 15_ad1.microsoft.ad.ldap.yml
├── 15_ad2.microsoft.ad.ldap.yml
├── 79_groups_vars.yml
└── 80_final.yml and it's used as:
All files are dynamic inventories. The falcon hosts inventory in the final piece of the puzzle. In order to have the dictionary merged I've two requirements
plugin: crowdstrike.falcon.falcon_hosts
client_id: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=path/to/secret:client_id') }}"
client_secret: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=path/to/secret:client_secret') }}"
cloud: 'eu-1'
filter: "product_type_desc:'Server' + last_seen:>='now-30d' + chassis_type:!'10'" The first requirement is the scope of this PR. creds = {}
for key, env in cred_mapping.items():
value = self.get_option(key) or os.getenv(env)
if self.templar.is_template(value):
value = self.templar.template(variable=value,disable_lookups=False)
if value:
if key == "cloud":
creds["base_url"] = value
else:
creds[key] = value In order to support Hashicoprp Vault lookup or generic lookup. I hope I was able to explain myself, Thanks |
Let me think about it for a little bit to see what we can do. Question for you, are there other dynamic inventory plugin's that you use where this feature is already there? |
Hi: |
Got you.. thanks for that.. Let me review the PR and make sure there is nothing else we need to add for this. Afterwards, I can start a new PR for the 2nd part of your problem to support that as well, as that is a valid request as well! Thanks again for using this inventory and providing feedback! Hang tight. |
If you preferer, for the sesond part, I've already ready the patch in my branch so I can open the PR for your review. |
Yeah go ahead and open the PR.. I'll have some updates for it but it's a great starting point! |
I haven't forgotten about this. In fact, I've been diving a little more into this trying to get a better grasp of error handling and ensuring we get our desired outputs for an inventory. I went ahead and started using the What I'm noticing though, is that there are things such as automatic deduplication that occurs when trying to use a value such as hostnames:
- device_id You should get a 1:1 return from the API response. The caveat here would be that we would need to set the |
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #474 +/- ##
==========================================
+ Coverage 37.78% 37.95% +0.16%
==========================================
Files 17 17
Lines 831 830 -1
Branches 160 159 -1
==========================================
+ Hits 314 315 +1
+ Misses 516 514 -2
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
This helps our docs be clearer as to the potential pitfalls of using hostname for inventory_hostname due to duplication.
To not upset the Ansible lint gods, these updates provide the same example set in the 'sample file' format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ready to merge
Hi,
I suggest this imporovement in order to be able to do some "post-processing" in inventory name.
In my use case, the vm names are uppercase and the inventory_hostname variable for ansible inventory entries have to be lowercase.
This can be accomplished by:
The actual logic can be achieved by (I don't modified that part)
Let me know,
Bye