Saturday, December 10, 2016

Ansible : lineinfile

https://groups.google.com/forum/#!msg/ansible-project/JvHfchsgRaU/Vw_CzBbvadgJ

-- code extract ----
  # Regexp implementation note:

  #  - the line to match follows the following pattern:

  #     - <startofline>: matched by ^

  #     - A number of white spaces/tabs: matched by \s+

  #     - the "kernel" string: matched by kernel

  #     - A list of: <spaces|tab><string> -> this is all the options passed to the kernel

  #     - possible spaces and tabs at the end : matched by the latest \s*

  #     - the <end of line>: matched by $

  #  - the list of: <spaces|tab><string1> described above 

  #    e.g:  /vmlinuz-2.6.32-358.el6.x86_64 ro root=/dev/mapper/VolGroup00-root_vol … rd_LVM_LV=VolGroup00/root_vol SYSFONT=latarcyrheb-sun16 rd_NO_DM

  #    is matched by: (\s+(?!pcie_aspm=off)[\w=/\-\.]+)*

  #    The (?!pcie_aspm=off) is to direct the regexp engine to check that the string 'pcie_aspm=off' 
is not present

  #       anywhere in the in the <string1> part. This is called negative lookahead.

  #       This is just a check, no characters are "consumed". The following [\w=/\-\.]+

  #       is ther to consume aka match any strings made of alphanumeric characters,the '/', the '-', the '.'  

  #  - if the 'pcie_aspm=off' string is present, there is no match

  #  - the capturing group 1 referred as to \1 in the line= statement 

  #    will capture the whole line but the trailing <withe spaces> and <end of line>

  

    

  - name: Activate a lacking kernel option 'pcie_aspm=off' in {{ targetgrub }}

    lineinfile: dest={{ targetgrub }}

              backup=True

              backrefs=True

              state=present

              regexp='(^\s+kernel(\s+(?!pcie_aspm=off)[\w=/\-\.]+)*)\s*$'

              line='\1 pcie_aspm=off'



-- end of code extract ----

http://daveops.co.uk/2014/05/using-ansible-to-append-a-string-to-the-end-of-a-line/

http://www.handverdrahtet.org/2016/01/ansible-using-numbered-backreference.html

No comments:

Post a Comment