6

I have a task in an Ansible playbook that is supposed to iterate with a list of users and do a lineinfile to enable access to a postgress database:

- name: Postgres localhost access
  lineinfile:
    dest: "{{postgres_dest}}/data/pg_hba.conf"
    line: "host    all             {{item.user}}   127.0.0.1/32            trust"
    regexp: "^host[\s\t]*all[\s\t]*{{item.user}}[\s\t]*127.0.0.1/32[\s\t]*trust"
    insertbefore: EOF
  with_items: "{{postgres_users}}"
  notify: postgres reload
  tags:
    - postgres
    - postgres_hba

the problem I'm getting is that ansible thinks that the {{item.user}} is not being escaped with "", which actually is not true, since this will expand due to the "" of the whole line. The exact error I get:

Syntax Error while loading YAML script, jenkins.yml
Note: The error may actually appear before this position: line 156, column 9

        line: "host    all             {{item.user}}   127.0.0.1/32            trust"
        regexp: "^host[\s\t]*all[\s\t]*{{item.user}}[\s\t]*127.0.0.1/32[\s\t]*trust"
        ^
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

any ideas on how to do this?

sebamontini
  • 360
  • 2
  • 10

1 Answers1

7

first of all, thanks to the guys in IRC on the #ansible channel :)

seems that the issue wasn't with the vars itself but with the un-scaped backslashes

changed the line to: regexp: "^host[\\s\\t]*all[\\s\\t]*{{item.user}}[\\s\\t]*127.0.0.1/32[\\s\\t]*trust" and now it works great

sebamontini
  • 360
  • 2
  • 10
  • 1
    In the [examples of the docs](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#examples) they recommend using single quotes because they don't need escaping. – Benjamin Jan 14 '21 at 11:51