George Shuklin
George Shuklin

Reputation: 7897

add quotes around variable in ansible variables

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) }}"

Upvotes: 5

Views: 20041

Answers (2)

Zoltán
Zoltán

Reputation: 22156

You can also use the quote filter:

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

See Playbooks Filters.

Upvotes: 2

Kashyap
Kashyap

Reputation: 17476

Did you already try this?

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

or this if postgress expects double quotes instead of single.

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

I have no simple way of testing so I only have hope to support the theory... :-)

Upvotes: 1

Related Questions