How to match regex tab?

1,234 views
Skip to first unread message

ju

unread,
Dec 14, 2015, 11:13:35 PM12/14/15
to Ansible Project
Hi,

I am running ansible version 1.9. I am using replace module to add parameters to kernel line which starting with a <tab> in the grub.conf file

<tab>kernel /boot/vmlinuz-2.6.32-573.7.1.el6.x86_64 ro root=UUID=f360dd95-c0e6-49ff-b346-2dce04d6b433 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us elevator=noop biosdevname=0 LANG=en_AU.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_NO_LVM rd_NO_DM console=ttyS0,115200n8 console=tty0 consoleblank=0 net.ifnames=0

I tried the regexp="^[\t]kernel*\n$", but ansible cannot detect the kernel line, the regexp="^[\t]kernel" did not work either. No luck for lineinfile module either. What I could be missing here? Appreciate any clues.

Thank you.

- j


Tim Rupp

unread,
Dec 15, 2015, 12:56:03 AM12/15/15
to Ansible Project
Hi j,

Are you sure that that file contains a tab? Or does it just look like a tab, or, is your editor replacing tabs with spaces?

I tried this playbook

---

- name: ASDASD
  hosts: all
  gather_facts: false
  connection: local

  tasks:
      - lineinfile:
            dest: ok.conf
            regexp: "^[\t]kernel"
            line: "tim"


With a file I created in nano (my vim is configured to replace tabs with spaces) that contained the following

<tab>kernel /boot/vmlinuz-2.6.32-573.7.1.el6.x86_

and it replaced it with "tim" as expected. The same, with tab converted to spaces, added the "tim" line as expected. Tested on 1.9.2 on wheezy.

-tim

ju

unread,
Dec 15, 2015, 5:07:27 AM12/15/15
to Ansible Project
Hi Tim,

You are right, it works if I don't use with_items (something wrong with my following statement). Anyway, the problem now is that rdblacklist=nouveau nouveau.modeset=0 is inserted to the end of file, not the kernel end of line. Is there any way to insert parameters to end of line, or it is impossible to insert things at the end of line in ansible? Sorry, I might have to post in another subject.

- name "Insert nouveau at the end of the kernel line"
  lineinfile: dest=/boot/grub/grub.conf state=present backrefs=True  regexp="{{ item.regexp }}" line="{{ item.insert }}"
  with_items:
    - { "regexp": "^[\t]kernel", "insert": " rdblacklist=nouveau nouveau.modeset=0\n" }
  sudo: true

Tim

unread,
Dec 16, 2015, 11:57:17 PM12/16/15
to ansible...@googlegroups.com
Hey j,

Yes, you can append using the backrefs argument.


Here's an example I tried on my box that works using an iterator

---

- name: ASDA
  hosts: all
  connection: local

  tasks:
      - name: Insert nouveau at the end of the kernel line
        lineinfile:
            dest: "/tmp/grub.conf"
            state: "present"
            backrefs: "yes"
            regexp: "{{ item.regexp }}"
            line: "{{ item.insert }}"
        with_items:
            - regexp: '(^\s+kernel(\s+(?!nouveau\.modeset=0)[\w=/\-\.,]+)*)\s*$'
              insert: '\1 nouveau.modeset=0'
            - regexp: '(^\s+kernel(\s+(?!rdblacklist=nouveau)[\w=/\-\.,]+)*)\s*$'
              insert: '\1 rdblacklist=nouveau'


I started with the examples mentioned here


and just expanded on them. The above example appears to be idempotent as well.

Hope that helps.

-tim

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b9bb751a-93b8-4139-8eec-6b590c479402%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ju

unread,
Dec 20, 2015, 8:39:15 PM12/20/15
to Ansible Project
Hi Tim,

Thank you so much, indeed, that works well, greatly appreciate it.

Cheers.

- j
Reply all
Reply to author
Forward
0 new messages