ansible-kafka icon indicating copy to clipboard operation
ansible-kafka copied to clipboard

override default server variable

Open adriensaladin opened this issue 7 years ago • 3 comments

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,

adriensaladin avatar Feb 03 '18 09:02 adriensaladin

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.

jaytaylor avatar Feb 21 '18 21:02 jaytaylor

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.

adriensaladin avatar Mar 01 '18 15:03 adriensaladin

Hi guys,

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

jordiclariana avatar Mar 21 '18 17:03 jordiclariana