Checking for a string and value

54 views
Skip to first unread message

Vino B

unread,
Mar 10, 2018, 12:38:14 AM3/10/18
to Ansible Project
Hi All,

  Request your help, I need to find whether the string numa_balancing is set to disabled in the file /etc/default/grub where the string in contained in the string GRUB_CMDLINE_LINUX_DEFAULT any help on this is much appreciated.

cat /etc/default/grub|grep GRUB_CMDLINE_LINUX_DEFAULT
GRUB_CMDLINE_LINUX_DEFAULT="root=/dev/hda1 disk=/dev/hda resume=swap console=ttyS0,115200n8 multipath=off net.ifnames=0 NON_PERSISTENT_DEVICE_NAMES=1 quiet elevator=noop intel_idle.max_cstate=1 processor.max_cstate=1 numa_balancing=disable"


From,
Vino.B

Kai Stian Olstad

unread,
Mar 10, 2018, 2:07:24 PM3/10/18
to ansible...@googlegroups.com
There are many ways to do that, here is one

- slurp:
src: /etc/default/grub
register: r

- debug: msg="Found it"
when: r.content | b64decode | regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disable')


--
Kai Stian Olstad

Vino B

unread,
Mar 11, 2018, 1:45:49 AM3/11/18
to Ansible Project
Hi Kai,

   Thank you very much, but the above solution always states tune eve when the search criteria is changed eg. in the above code if you change the =disable" to ="disbaleX" it should fail, but the output states as successful

when: r.content | b64decode | regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disableX') 

 
 From,
Vino.B

Kai Stian Olstad

unread,
Mar 11, 2018, 5:21:13 AM3/11/18
to ansible...@googlegroups.com
On Sunday, 11 March 2018 07.45.48 CET Vino B wrote:
>
> On Sunday, March 11, 2018 at 3:07:24 AM UTC+8, Kai Stian Olstad wrote:
> >
> > There are many ways to do that, here is one
> >
> > - slurp:
> > src: /etc/default/grub
> > register: r
> >
> > - debug: msg="Found it"
> > when: r.content | b64decode |
> > regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disable')
> >
> >
> >
>
> Thank you very much, but the above solution always states tune eve when
> the search criteria is changed eg. in the above code if you change the
> =disable" to ="disbaleX" it should fail, but the output states as successful
>
> when: r.content | b64decode |
> regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disableX')

I made a little test and can't confirm this

$ cat grub
GRUB_CMDLINE_LINUX_DEFAULT="root=/dev/hda1 disk=/dev/hda resume=swap console=ttyS0,115200n8 multipath=off net.ifnames=0 NON_PERSISTENT_DEVICE_NAMES=1 quiet elevator=noop intel_idle.max_cstate=1 processor.max_cstate=1 numa_balancing=disable"


cat test.yml
---
- hosts: localhost
tasks:
- slurp:
src: grub
register: r

- name: disable
debug: msg="Found it"
when: r.content | b64decode | regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disable')

- name: diableX
debug: msg="Found it"
when: r.content | b64decode | regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disableX')


Output:

TASK [disable] **********************************
ok: [localhost] => {}

MSG:

Found it


TASK [diableX] **********************************
skipping: [localhost] => {
"changed": false,
"skip_reason": "Conditional result was False"
}



--
Kai Stian Olstad

Vino B

unread,
Mar 11, 2018, 8:17:02 AM3/11/18
to Ansible Project
Hi Kai,

  Below is the playbook does not execute the debug part , can you help me what is wrong in the below play book.

---
- hosts: target
  remote_user: ansible
  become: true
  become_method: sudo
  tasks:
   - slurp:
      src: /etc/default/grub
     register: r
   - name: Check whether AutoNuma is disabled
     debug: msg="Found it"
     when: r.content|b64decode|regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disableX')
 
OUTPUT:

PLAY [target] **********************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [10.162.54.34]

TASK [slurp] ***********************************************************************************************************************************************************
ok: [10.162.54.34]

TASK [Check whether AutoNuma is disabled] ******************************************************************************************************************************
skipping: [10.162.54.34]

PLAY RECAP *************************************************************************************************************************************************************
10.162.54.34               : ok=2    changed=0    unreachable=0    failed=0

Kai Stian Olstad

unread,
Mar 11, 2018, 8:48:24 AM3/11/18
to ansible...@googlegroups.com
On 11.03.2018 13:17, Vino B wrote:
> Below is the playbook does not execute the debug part , can you help
> me
> what is wrong in the below play book.
>
> ---
> - hosts: target
> remote_user: ansible
> become: true
> become_method: sudo
> tasks:
> - slurp:
> src: /etc/default/grub
> register: r
> - name: Check whether AutoNuma is disabled
> debug: msg="Found it"
> when:
> r.content|b64decode|regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disableX')
>
> OUTPUT:

<snip />

> TASK [Check whether AutoNuma is disabled]
> ******************************************************************************************************************************
> skipping: [10.162.54.34]

The file does not have disableX in it so it will not run the task.
If you are looking for the opposite behaviour you need to add "not"

when: not
(r.content|b64decode|regex_search('^GRUB_CMDLINE_LINUX_DEFAULT=.*numa_balancing=disableX'))


--
Kai Stian Olstad

Vino B

unread,
Mar 12, 2018, 2:31:11 AM3/12/18
to Ansible Project
Reply all
Reply to author
Forward
0 new messages