Reputation: 8523
This is my group_vars
:
services:
service_csmsaga:
service_name: "service_csmsaga"
port: "21100/tcp"
service_csmsagatcp:
service_name: "service_csmsagatcp"
port: "21200/tcp"
I want to add these lines into my /etc/services
using:
- name: Add saga services to /etc/services
lineinfile: dest=/etc/services regexp='^{{ item.value.service_name }}'
line="{{ item.value.service_name}} {{ item.value.port }}"
with_dict: services
The lines have already been added to /etc/services
like below:
service_csmsaga 21100/tcp
service_csmsaga 21100/tcp
service_csmsaga 21100/tcp
service_csmsagatcp 21200/tcp
but it still keeps on adding the line:
TASK [db_server : Add saga services to /etc/services] **************************
changed: [172.17.0.2] => (item={'value': {u'service_name': u'service_csmsaga', u'port': u'21100/tcp'}, 'key': u'service_csmsaga'}) => {"backup": "", "changed": true, "item": {"key": "service_csmsaga", "value": {"port": "21100/tcp", "service_name": "service_csmsaga"}}, "msg": "line replaced"}
changed: [172.17.0.2] => (item={'value': {u'service_name': u'service_csmsagatcp', u'port': u'21200/tcp'}, 'key': u'service_csmsagatcp'}) => {"backup": "", "changed": true, "item": {"key": "service_csmsagatcp", "value": {"port": "21200/tcp", "service_name": "service_csmsagatcp"}}, "msg": "line added"}
What am I missing here ?
Upvotes: 0
Views: 226
Reputation: 56839
Look at your regexp
parameter and then your data.
The first iteration (service_csmsaga
) will match both lines so will replace both lines with the first one. Then the second iteration will add a new line to the end because service_csmsagatcp
doesn't exist. Then when you run your playbook again your first iteration replaces all three lines and so on and so forth.
To fix it you need to either change your data (change the name of the service) or be more specific in your regex.
Something like this should work:
- name: Add saga services to /etc/services
lineinfile: dest=/etc/services regexp='^{{ item.value.service_name }}\s'
line="{{ item.value.service_name}} {{ item.value.port }}"
with_dict: services
This will then make sure it only matches the service_name
followed by some whitespace before replacing it so that your service names don't overlap.
Upvotes: 1