add quotes around variable in ansible variables

11,289

You can also use the quote filter:

postgresql_listen_addresses: "{{ ansible_default_ipv4.address | quote }}"

See Playbooks Filters.

Share:
11,289
George Shuklin
Author by

George Shuklin

Updated on June 27, 2022

Comments

  • George Shuklin
    George Shuklin almost 2 years

    I've got problem with one of ansible playbooks (enrise.postgresql).

    It accepts variable postgresql_listen_addresses (list of values to listen_addresses in /etc/postgresql/9.3/main/postgresql.conf).

    I want to use value ansible_default_ipv4.address, but ansible provide it without quotes, and postgress wants it with single quotes (like '192.168.0.1').

    I have variable like this:

    postgresql_listen_addresses: '{{ [ ansible_default_ipv4.address ] }}'
    

    When address is without quotes, postgress complains about unquoted IP address (syntax error).

    How can I add quotes around value of ansible_default_ipv4.address?

    Thanks.

    UPD:

    I was able to solve it with this ugly code. If someone can better, please help.

    print_quotes: "'%s'"
    postgresql_listen_addresses: [ "{{print_quotes|format(ansible_default_ipv4.address) }}"