jaytaylor / ansible-kafka

Ansible Kafka role
https://galaxy.ansible.com/jaytaylor/kafka/
Other
67 stars 52 forks source link

override default server variable #36

Open adriensaladin opened 6 years ago

adriensaladin commented 6 years ago

Hi,

I had to override server.advertised_listeners, which is defined in the default role variables, and it was quite a difficult task.

I asked a coworker which knows Ansible much better than I and the best method we found so far is to use pre_tasks as bellow:

- hosts: kafka
  roles:
    - role: ansible-kafka
      kafka_hosts: "{{ ansible_play_batch }}"
      kafka_version: 1.0.0     # Kafka version override.

  vars:
    zookeeper_hosts: "kafka1-1:2181,kafka1-2:2181,kafka1-3:2181"
    kafka_zookeeper_hosts: ['kafka1-1', 'kafka1-2', 'kafka1-3']

  pre_tasks:
    - name: set advertised listeners
      set_fact:
        server: '{{ server | combine({"advertised_listeners": "PLAINTEXT://"+ansible_hostname+":9092" }) }}'

The thing is we either need to completely redefine the server dict in the ansible play, or change the Ansible merge strategy in ansible.cfg (which is generally a bad idea) or use this set_fact trick.

Are you aware of a better method to redefine those variables ? If not, would you like to add an example in the role documentation?

Cheers,

jaytaylor commented 6 years ago

Hi @adriensaladin,

I'm not sure I understand yet what the problem is. Usually overriding server.advertised_listeners from the empty value in default configuration should be sufficient. IIRC, whatever overrides you wish can be included either by setting them in a default location or explicitly specifying the variables file(s) from the command line when ansible is run.

It may also be worth reviewing the relevant ansible documentation on variables.

adriensaladin commented 6 years ago

Hi @jaytaylor,

From what I understand it is not easy to override a single key of an Ansible dictionary. Trying to set "server.advertised_listeners" in role vars will give an error and trying to do something like

server:
  advertised_listeners: some_value

will override server dict completely and not only its advertised_listeners key, unless the merge strategy is changed globally (http://docs.ansible.com/ansible/latest/intro_configuration.html#hash-behaviour).

So the example using set_fact and combine() I gave is the simplest way I could find to only override a single key of the server dictionary and keep other defaults.

jordiclariana commented 6 years ago

Hi guys,

i think this pull request will solve this problem (I was experiencing it too, hence I created the PR)