Ansible variables and text replacement

ansible

I am trying to use Ansible to replace a sample text with a variable. I have tried using no quotes, "", and '', but nothing seems to work. I also tried using the replace module and the lineinfile module.

Please let me know what I am doing wrong.

- hosts: 10.1.1.10
  become: yes

  vars:
    server1:
      1a: "ED:5A:13:AA:34:20"
      2a: "ED:5A:13:AA:34:21"

  tasks:
  - name: Replace mac address in file  
    lineinfile:
      path: /tmp/file.json.j2
      regexp: 'FILL_THIS_IN'
      line: "{{ server1.1a }}"

Best Answer

Excerpt from the documentation about variables:

Variable names should be letters, numbers, and underscores. Variables should always start with a letter.

So you should refactor to:

server1:
  a1: "ED:5A:13:AA:34:20"
  a2: "ED:5A:13:AA:34:21"

and in the code:

line: "{{ server1.a1 }}"