I have an Ansible playbook where I need to change the Server Name in one 
line and an Specific IP address in another line, as in this line there are 
a bunch of IP address separated by a comma.

I have the below play which works absolutely fine in case I have to change 
the entire IP address line with the defined set of IP addresses, But I'm 
looking for specific IP address i.e. 191.168.1.4 to be looked upon and if 
found then just replace that only and leave the rest of the IP as it is.
--- - name: Playbook to replace line in zabbix_agentd.conf hosts: all 
#remote_user: root gather_facts: False tasks: - name: Changing the 
zabbix-agent configuration on the client lineinfile: path: 
/tmp/zabbix_agentd.conf ### line to be searched & matched regexp: '{{ 
item.From }}' ### new line to be replaced with the old matched one line: 
'{{ item.To }}' state: present backup: yes backrefs: yes with_items: - { 
From: '#ServerActive=myzabbix1.example.com', To: 
'ServerActive=myzabbix2.example.com'} - { From: 
'Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5', To: 
'Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.10,192.168.1.5'} # 
Actions will be triggered at the end of each block of task and notifies a 
handler. notify: restart_zabbix_service handlers: - name: 
restart_zabbix_service # referenced by a globally unique name and are 
notified by notifiers. service: name: zabbix-agentd state: restarted  

On Thursday, September 12, 2019 at 7:38:32 PM UTC+5:30 philipp...@gmail.com 
wrote:

> not idempotent when the list ordering change, plus some others
>  
> for each item in the list you have to add it if not already there 
> (requires a look-ahead expression in regex)
> that is what make it quite complex at the end, but doable as long as
> per the spec the only need is to append to an existing list.
>
> hence the idea to leverage the template module
> that also can deal with removal of item from the list
>
> --
> Phil
>
>
> Le jeu. 12 sept. 2019 à 12:27, Dick Visser <dick....@geant.org> a écrit :
>
>> This will work, but it's not idempotent.
>> So if you run it again it will add the two CIDRs again.
>> For it to work idempotently, the regex will need to match both what
>> you have before and what you get after.
>> Something like this should work:
>>
>>
>> ---
>> - hosts: localhost
>>   connection: local
>>   gather_facts: false
>>   vars:
>>     cidrs:
>>       - 1.1.1.1/255.255.255.255
>>       - 2.2.2.2/255.255.255.255
>>
>>   tasks:
>>      - name: "insert values to a list"
>>        lineinfile:
>>            dest: /tmp/test.txt
>>            regexp: "^(?P<start>ALL:.*?)((,{{ cidrs | join(',') }})*)$"
>>            line: "\\g<start>,{{ cidrs | join(',') }}"
>>            backrefs: yes
>>
>>
>> The named group 'start' is to avoid clashing with a CIDR that begins
>> with a number.
>>
>>
>> Dick
>>
>>
>>
>> On Thu, 12 Sep 2019 at 09:15, Shivharsh Singh <shivha...@gmail.com> 
>> wrote:
>> >
>> > Hello Morrissey,
>> >
>> > You may use the below playbook to fulfill your requirements. It makes 
>> use of lineinfile module with regular expression to capture the line to be 
>> modified.
>> >
>> > ---
>> > - hosts: localhost
>> >   connection: local
>> >   gather_facts: false
>> >   tasks:
>> >      - name: "insert values to a list"
>> >        lineinfile:
>> >            dest: /opt/test.txt
>> >            regexp: '(^ALL.*$)'
>> >            line: '\1, {{item}}'
>> >            backrefs: yes
>> >        with_items:
>> >            - 1.1.1.1/255.255.255.255
>> >            - 2.2.2.2/255.255.255.255
>> >
>> > The file would be modified as shown below:
>> > Hello world !
>> > ALL, 1.1.1.1/255.255.255.255, 2.2.2.2/255.255.255.255
>> > This is a test file
>> >
>> > Hope this is helpful !
>> >
>> > Thanks,
>> > Shivharsh
>> >
>> > On Wednesday, 11 September 2019 19:36:27 UTC+5:30, TheSmiths wrote:
>> >>
>> >> Hi,
>> >>
>> >> I would like to use lineinfile module to do the next:
>> >>
>> >> 1. go over list of items (with_items)
>> >> 2. look for specific line that always starts with the word "ALL:"  
>> (hosts.allow)
>> >> 3. append each item at the END of the line with comma before the item
>> >>
>> >> for example
>> >>
>> >> ALL ,1.1.1.1/255.255.255.255 , 2.2.2.2/255.255.255.255
>> >>
>> >> Thanks,
>> >> Morrissey
>> >>
>> >>
>> > --
>> > You received this message because you are subscribed to the Google 
>> Groups "Ansible Project" group.
>> > To unsubscribe from this group and stop receiving emails from it, send 
>> an email to ansible-proje...@googlegroups.com.
>> > To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/acbe7f26-fea5-403b-92f3-b8e2c1506b1a%40googlegroups.com
>> .
>>
>>
>>
>> -- 
>> Dick Visser
>> Trust & Identity Service Operations Manager
>> GÉANT
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to ansible-proje...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/CAL8fbwOCE8wrZa0gdhgzB4fiNk5QuihfUxnLTFp0WgA0_UXQLg%40mail.gmail.com
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b4d2b8a9-b563-4056-926b-5dc00414ada4n%40googlegroups.com.

Reply via email to