1

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

2
  • Did you try the suggestion? (replacing the single quotes ' surrounding your variable with double quotes "') Commented Dec 14, 2023 at 6:40
  • Apologies for the delay. I made some adjustments to the script: Initially, the script requested to add a section. I included "Section" in the task details, but the Zabbix agent configuration file doesn't contain a section. The output now displays as follows. Moreover, investory_hostname is printing only local IP address and not hostname any idea will be very of great help. now the output is like [General] Server: zabbix.example.com ServerActive: zabbix.example.com hostname : 192.168.x.x So how can i deal with Section when there is no section in the file and IP to hostname Commented Dec 17, 2023 at 22:27

1 Answer 1

2

Since the default Zabbix Agent Config file is an INI file with just

LogFile=/tmp/zabbix_agentd.log Server=127.0.0.1 ServerActive=127.0.0.1 Hostname=Zabbix server 

an minimal example playbook with ini_file module – Tweak settings in INI files

--- - hosts: localhost become: false gather_facts: false tasks: - ini_file: path: zabbix_agentd.conf option: "{{ item.option }}" value: "{{ item.value }}" loop: - { option: 'Server', value: 'zabbix.example.com' } - { option: 'ServerActive', value: 'zabbix.example.com' } - { option: 'Hostname', value: '{{ inventory_hostname }}' } 

will result into an output of

TASK [ini_file] **************************************************************************** changed: [localhost] => (item={u'option': u'Server', u'value': u'zabbix.example.com'}) changed: [localhost] => (item={u'option': u'ServerActive', u'value': u'zabbix.example.com'}) changed: [localhost] => (item={u'option': u'Hostname', u'value': u'localhost'}) 

and a changed configuration file with

LogFile=/tmp/zabbix_agentd.log Server = zabbix.example.com ServerActive = zabbix.example.com Hostname = localhost 

So there is absolute no need for "editing" the file with lineinfile and Regular Expressions.

1
  • I made some adjustments to the script: Initially, the script requested to add a section. I included "Section" in the task details, but the Zabbix agent configuration file doesn't contain a section. The output now displays as follows. Moreover, investory_hostname is printing only local IP address and not hostname any idea will be very of great help. now the output is like [General] Server: zabbix.example.com ServerActive: zabbix.example.com hostname : 192.168.x.x So how can i deal with Section when there is no section in the file and IP to hostname Commented Dec 18, 2023 at 20:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.