Skip to content
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

override default server variable #36

Open
adriensaladin opened this issue Feb 3, 2018 · 3 comments
Open

override default server variable #36

adriensaladin opened this issue Feb 3, 2018 · 3 comments

Comments

@adriensaladin
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Contributor

Hi guys,

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants