I am trying to have Ansible read a file and select the first match of my search parameter as well as remove unneeded text.
For example, I have the file below
<Proxy balancer://${balancerNameEAPI}>
# BalancerMember https://server1.domain.com:port hctemplate=tcp
BalancerMember https://server2.domain.com:port hctemplate=tcp
ProxySet lbmethod=bybusyness
</Proxy>
<Proxy balancer://${balancerNamePAPI}>
# BalancerMember https://server1.domain.com:port hctemplate=tcp
BalancerMember https://server2.domain.com:port hctemplate=tcp
ProxySet lbmethod=bybusyness
</Proxy>
<Proxy balancer://${balancerNameSAPI}>
# BalancerMember https://server1.domain.com:port hctemplate=tcp
BalancerMember https://server2.domain.com:port hctemplate=tcp
ProxySet lbmethod=bybusyness
</Proxy>
I am needing to take action on the commented out standby host which in this case is server1
.
I'm using the below script to read this config file and pull out the lines containing the correct host.
---
- name: test read file
hosts: localhost
gather_facts: false
vars:
source_file: /files/test/test.cfg
host_to_patch: []
tasks:
- set_fact:
host_to_patch: "{{ host_to_patch + [ item ] }}"
with_lines: "cat {{ source_file }}"
when: item is search('^# BalancerMember https://server')
- debug:
var: host_to_patch
This of course gives me the output of the entire line three times where I'm only interested in the server1.domain.com
portion of the line and at that I only need the first match. I'm wanting to tell Ansible this is the target hostname for another play further along in the playbook. The reason for this being that the servers in the file are an active standby pair and I need to do them one at a time starting with the standby host which could be different from month to month.
Below is the current output of what I have
ok: [localhost] => {
"host_to_patch": [
"# BalancerMember https://server1.domain.com:port hctemplate=tcp",
"# BalancerMember https://server1.domain.com:port hctemplate=tcp",
"# BalancerMember https://server1.domain.com:port hctemplate=tcp"
]
}
Please let me know if any further info would be helpful and thanks in advance!