I have a file that has the following lines.
IPX,10.153.34.97,255.255.255.0,0002.c18b.f128,,,
IPX,10.153.34.101,255.255.255.0,0002.c18b.ed10,,,
IPX,10.153.34.104,255.255.255.0,0002.c18c.1a43,,,
IPX,10.153.34.111,255.255.255.0,0002.c18b.f93e,,,
IPX,10.153.34.113,255.255.255.0,0002.c18c.3148,,,
IPX,10.153.34.115,255.255.255.0,0002.c18c.30e8,,,
I am trying to use Ansible to edit the MAC address to look like the following (remove the periods from the MAC):
IPX,10.153.34.97,255.255.255.0,0002c18bf128,,,
IPX,10.153.34.101,255.255.255.0,0002c18bed10,,,
IPX,10.153.34.104,255.255.255.0,0002c18c1a43,,,
IPX,10.153.34.111,255.255.255.0,0002c18bf93e,,,
IPX,10.153.34.113,255.255.255.0,0002c18c3148,,,
IPX,10.153.34.115,255.255.255.0,0002c18c30e8,,,
My Ansible version is 2.12.10 and my code is the following:
- hosts: 127.0.0.1
tasks:
- name: Replace periods in MAC.
replace:
path: /home/test/ansible-mac-export/test-export3
regexp: ([0-9a-f]{4}[\.][0-9a-f]{4}[\.][0-9a-f]{4})
replace: ([0-9a-f]{4}[0-9a-f]{4}[0-9a-f]{4})
Obviously the fault is with my replace statement. However, I can't figure out how to change it to be what I need. I have also tried the following replace line without any success.
replace: '\1'
I know that I'm really close, but any help would be greatly appreciated.
CodePudding user response:
Given the file for testing
shell> cat /tmp/test.csv
IPX,10.153.34.97,255.255.255.0,0002.c18b.f128,,,
IPX,10.153.34.101,255.255.255.0,0002.c18b.ed10,,,
IPX,10.153.34.104,255.255.255.0,0002.c18c.1a43,,,
IPX,10.153.34.111,255.255.255.0,0002.c18b.f93e,,,
IPX,10.153.34.113,255.255.255.0,0002.c18c.3148,,,
IPX,10.153.34.115,255.255.255.0,0002.c18c.30e8,,,
Use the module community.general.read_csv and read the file
- community.general.read_csv:
path: /tmp/test.csv
fieldnames: layer,ip,mask,mac,x1,x2,x3
register: test
gives
test.list:
- ip: 10.153.34.97
layer: IPX
mac: 0002.c18b.f128
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.101
layer: IPX
mac: 0002.c18b.ed10
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.104
layer: IPX
mac: 0002.c18c.1a43
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.111
layer: IPX
mac: 0002.c18b.f93e
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.113
layer: IPX
mac: 0002.c18c.3148
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.115
layer: IPX
mac: 0002.c18c.30e8
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
Update mac. Declare the variable mac_update
mac_update: "{{ test.list|
map(attribute='mac')|
map('split', '.')|
map('join')|
map('community.general.dict_kv', 'mac')|
list }}"
gives
mac_update:
- mac: 0002c18bf128
- mac: 0002c18bed10
- mac: 0002c18c1a43
- mac: 0002c18bf93e
- mac: 0002c18c3148
- mac: 0002c18c30e8
zip the lists and combine the items. Declare the variable
test_update: "{{ test.list|zip(mac_update)|map('combine')|list }}"
gives
test_update:
- ip: 10.153.34.97
layer: IPX
mac: 0002c18bf128
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.101
layer: IPX
mac: 0002c18bed10
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.104
layer: IPX
mac: 0002c18c1a43
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.111
layer: IPX
mac: 0002c18bf93e
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.113
layer: IPX
mac: 0002c18c3148
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
- ip: 10.153.34.115
layer: IPX
mac: 0002c18c30e8
mask: 255.255.255.0
x1: ''
x2: ''
x3: ''
Update the file
- ansible.builtin.copy:
dest: /tmp/test.csv
content: |
{% for i in test_update %}
{{ i.layer }},{{ i.ip }},{{ i.mask }},{{ i.mac }},,,
{% endfor %}
gives
shell> cat /tmp/test.csv
IPX,10.153.34.97,255.255.255.0,0002c18bf128,,,
IPX,10.153.34.101,255.255.255.0,0002c18bed10,,,
IPX,10.153.34.104,255.255.255.0,0002c18c1a43,,,
IPX,10.153.34.111,255.255.255.0,0002c18bf93e,,,
IPX,10.153.34.113,255.255.255.0,0002c18c3148,,,
IPX,10.153.34.115,255.255.255.0,0002c18c30e8,,,
- Example of a complete playbook for testing
- hosts: localhost
vars:
mac_update: "{{ test.list|
map(attribute='mac')|
map('split', '.')|
map('join')|
map('community.general.dict_kv', 'mac')|
list }}"
test_update: "{{ test.list|zip(mac_update)|map('combine')|list }}"
tasks:
- community.general.read_csv:
path: /tmp/test.csv
fieldnames: layer,ip,mask,mac,x1,x2,x3
register: test
- debug:
var: test.list
- debug:
var: mac_update
- debug:
var: test_update
- ansible.builtin.copy:
dest: /tmp/test.csv
content: |
{% for i in test_update %}
{{ i.layer }},{{ i.ip }},{{ i.mask }},{{ i.mac }},,,
{% endfor %}
- Example of a simplified playbook that gives the same result
- hosts: localhost
tasks:
- community.general.read_csv:
path: /tmp/test.csv
fieldnames: layer,ip,mask,mac,x1,x2,x3
register: test
- ansible.builtin.copy:
dest: /tmp/test.csv
content: |
{% for i in test.list %}
{{ i.layer }},{{ i.ip }},{{ i.mask }},{{ i.mac|replace('.', '') }},,,
{% endfor %}