Antarjyoti Sen
Antarjyoti Sen

Reputation: 11

Edit json file using ansible

I am trying to edit a particular JSON file using Ansible. The file is present in remote servers which have to be logged in using LDAP authentication.

The file path is /etc/sensu/conf.d/client.json

and the line that I want to edit is as follows:

"subscriptions": ["Nginx", "Primus", "B2B", "Docker_Process_Check", "EBS", "base"],

I want to add one more value ("filebeat") in that line which after that should look like

"subscriptions": ["Nginx", "Primus", "B2B", "Docker_Process_Check", "EBS", "base","filebeat"],

Upvotes: 0

Views: 1945

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

AFAIK there's still no builtin module for JSON manipulation.

Either use 3rd-party module (like ghetto-json),

or make a complex regular expression with replace module (i.e. search for "subscriptions": [<...>] and place ,"filebeat" just before closing ]. You can use regex101.com to craft and test required expression.

Please also inspect this answer for additional info.

Upvotes: 1

Related Questions