How to create a set of different "provider" values depending on 'ansible_hostname'?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

How to create a set of different "provider" values depending on 'ansible_hostname'?

L1 Bithead

Hello,

I have a simple playbook adding/removing tags:

 

 

 

---
- name: 'Palo Alto PAN OS: Create a new tag object.'
  hosts: all
  connection: local
  gather_facts: true
  collections:
    - paloaltonetworks.panos
    
  pre_tasks:
    - name: Get the system information.
      include_vars: ./vars/provider.yml
      no_log: 'no'

  tasks:  
    - name: Create a tag object.
      when: operation == "create"
      panos_tag_object:
        provider: '{{ provider }}'
        name: '{{ tag_name }}'
        color: '{{ tag_color }}'
        comments: '{{ tag_comment }}'
        
    - name: Remove a tag object.
      when: operation == "remove"
      panos_tag_object:
        provider: '{{ provider }}'
        name: '{{ tag_name }}'
        color: '{{ tag_color }}'
        state: absent

 

 

 

The '/vars/provider.yml' looks like this:

 

 

provider:
  ip_address: '2000.iaas.company.com'
  api_key: MY876API9787KEY

 

 

I would like to have two more 'provider' values and let the playbook to make a choice depending on the "ansible_hostname" or IP address. I can't figure it out. Can someone help? Maybe there is a better approach?

 

Regards,

Michael

1 accepted solution

Accepted Solutions

L5 Sessionator
2 REPLIES 2

L5 Sessionator

You need to make a list of provider dicts, then loop over that:

 

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#standard-loops

Thanks. I found out It also possible to use host_vars to separate variables for different hosts.

  • 1 accepted solution
  • 3941 Views
  • 2 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!