Reputation: 71
I'm trying to gather facts for AWS VPC ID in file using the lineinfile
module in ansible. But every time I gather those facts, it only creates new VPC ID in file but I need to override existing/previous VPC ID with new ID , so at a time I'm looking for only new VPC ID. How could I accomplish it ?
I used the below play but it doesn't working for me:
- name: VPN facts
ec2_vpc_vpn_info:
region: "{{ region }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
register: vpn_facts
- name: add vpn facts into file
lineinfile:
path: vpn_facts.yaml
state: present
regex: "vpn_id"
line: "vpn_id: {{ vpn_facts.vpn_connections[0].vpn_connection_id }}"
Upvotes: 1
Views: 94
Reputation: 191
If you are using a version of Ansible that is 2.11 or newer, you can use the 'search_string' attribute instead of regex. It will replace the last line that contains the search_string value with the value of the 'line' attribute.
- name: VPN facts
ec2_vpc_vpn_info:
region: "{{ region }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
register: vpn_facts
- name: add vpn facts into file
lineinfile:
path: vpn_facts.yaml
state: present
search_string: "vpn_id"
line: "vpn_id: {{ vpn_facts.vpn_connections[0].vpn_connection_id }}"
Upvotes: 2