I'm encountering an issue while using Ansible to dynamically modify a Zabbix agent configuration file. Specifically, I'm attempting to employ an ansible lineinfile module with a loop to update multiple lines within the file.
The script effectively changes values within the file, but I'm facing a syntax error when attempting to incorporate a variable into the Zabbix agent configuration. The objective is to replace the line "Hostname=Zabbix Server" with "Hostname=$HOSTNAME", allowing the value to be automatically sourced from the target machine. For instance, if the computer name is "computer1", the agent file should reflect "Hostname=computer1".
Below is the relevant snippet of my current lineinfile code:
- name: Update Zabbix Agent Configuration ansible.builtin.lineinfile: path: /etc/zabbix/zabbix_agentd.conf regexp: "{{ item.regexp }}" line: "{{ item.line }}" loop: - { regexp: '^Server=', line: 'Server=monlocal.xyz.com' } - { regexp: '^ServerActive=', line: 'ServerActive=monlocal.xyz.com' } - { regexp: '^Hostname=', line: 'Hostname={{ ansible_facts['hostname'] }}' } here is the error I get.
The offending line appears to be: - { regexp: '^ServerActive=', line: 'ServerActive=monlocal.xyz.com' } ^ here There appears to be both 'k=v' shorthand syntax and YAML in this task. Only one syntax may be used. 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 }}" I know the error is with the 3rd line. when I remove the 3rd line I get the expected result. any help will be highly appreciated.
Thanks, Yousuf
'surrounding your variable with double quotes"')