title: Ansible way of replacing a line in config
body: <p>I have a json as follows:</p>
  
  <pre><code>{
      "bootstrap": true,
      "server": true,
      "datacenter": "aws",
      "data_dir": "/var/consul",
      "log_level": "INFO",
      "enable_syslog": true
  }
  </code></pre>
  
  <p>This is on 3 servers which are in ansible inventory file as </p>
  
  <pre><code>[consul]
  10.0.0.1
  10.0.0.2
  10.0.0.3
  </code></pre>
  
  <p>Now to make the nodes join the cluster i will have to actually add the following config line as well</p>
  
  <pre><code>"start_join": ["ip_of_other_node_1", "ip_of_other_node_2"] 
  </code></pre>
  
  <p>and this will go on each of the 3 servers</p>
  
  <p>So basically it means if <code>10.0.0.1</code> is one of those nodes in cluster, it's config will look like </p>
  
  <pre><code>{
      "bootstrap": true,
      "server": true,
      "datacenter": "aws",
      "data_dir": "/var/consul",
      "log_level": "INFO",
      "enable_syslog": true,
      "start_join": ["10.0.0.2","10.0.0.3"]
  }
  </code></pre>
  
  <p>I am trying to this via ansible as follows: </p>
  
  <pre><code>  - name: Add the ip's of other servers to join cluster
      lineinfile:
          path: /etc/consul.d/server/config.json
          regexp: '^"enable_syslog"'
          insertafter: '^"enable_syslog"'
          line: '"start_join": ["{{ groups['consul'][1] }}", "{{ groups['consul'][2] }}"]'
      when: inventory_hostname == '{{ groups['consul'][0] }}'
  </code></pre>
  
  <p>Which is not really helping me out saying syntax error at line: , i am not sure what is the best way to achieve something like this via ansible and also what how to tackle the case when i increase the servers in inventory.</p>