Dinesh Ravi
Dinesh Ravi

Reputation: 1235

Ansible Regex: Get a integer from command & pass to other command to run

I have to execute below 2 commands whose value depends on the system.

# sysctl -w kernel.shmmax= parse_from_shm.sh script #For example  17179869184
# sysctl -w kernel.shmall= parse_from_shm.sh script #For example  4194304

./shm.sh will echo both system values required in the below format

kernel.shmmax=4185686016
kernel.shmall=1021896

So I have to parse & get integer value above result & execute ultimately below 2 commands

# sysctl -w kernel.shmmax=4185686016
# sysctl -w kernel.shmall=1021896

I have tried to register & parse the integer values using regex. But I couldn't able to process it perfectly. Any help would be of great help.

---
- hosts: fossology_test
  become: true
  become_user: root
  environment: 
   HOME: /usr/ansible
  gather_facts: no
  tasks:
   - name: run shell script
     become: true
     become_user: root
     command: ./shm.sh
     args:
      chdir: /usr/local/src/
     register: results
   - set_fact:
        shmmax: "{{ results.stdout | regex_search(shmmaxregexp, '\\1' )  }}"
        shmall: "{{ results.stdout | regex_search(shmallregexp, '\\1' )  }}"
     vars:
        shmmaxregexp: 'shmmax=([^\"]+)'
        shmallregexp: 'shmall=([^\"]+)'
   - name: sysctl -w kernel.shmmax="{{ shmmax | int }}"
     become: true
     become_user: root
     command: sysctl -w kernel.shmmax="{{ shmmax | int }}"
   - name: sysctl -w kernel.shmall="{{ shmall }}"
     become: true
     become_user: root
     command: sysctl -w kernel.shmall="{{ shmall }}"

This is the output

dinesh@dinesh-VirtualBox:~/Documents/remote/Ansible-Playbook/fossology_playbook$ ansible-playbook regex.yml -K -v
Using /etc/ansible/ansible.cfg as config file
BECOME password: 

PLAY [fossology_test] ************************************************************************************

TASK [run shell script] **********************************************************************************
changed: [fossology_test] => {"changed": true, "cmd": ["./shm.sh"], "delta": "0:00:00.005912", "end": "2020-03-28 05:25:42.022156", "rc": 0, "start": "2020-03-28 05:25:42.016244", "stderr": "", "stderr_lines": [], "stdout": "kernel.shmmax=4185686016\nkernel.shmall=1021896", "stdout_lines": ["kernel.shmmax=4185686016", "kernel.shmall=1021896"]}

TASK [set_fact] ******************************************************************************************
ok: [fossology_test] => {"ansible_facts": {"shmall": ["1021896"], "shmmax": ["4185686016\nkernel.shmall=1021896"]}, "changed": false}

TASK [sysctl -w kernel.shmmax="0"] ***********************************************************************
changed: [fossology_test] => {"changed": true, "cmd": ["sysctl", "-w", "kernel.shmmax=0"], "delta": "0:00:00.003133", "end": "2020-03-28 05:25:42.574223", "rc": 0, "start": "2020-03-28 05:25:42.571090", "stderr": "", "stderr_lines": [], "stdout": "kernel.shmmax = 0", "stdout_lines": ["kernel.shmmax = 0"]}

TASK [sysctl -w kernel.shmall="[u'1021896']"] ************************************************************
changed: [fossology_test] => {"changed": true, "cmd": ["sysctl", "-w", "kernel.shmall=[u'1021896']"], "delta": "0:00:00.003558", "end": "2020-03-28 05:25:43.071811", "rc": 0, "start": "2020-03-28 05:25:43.068253", "stderr": "sysctl: setting key \"kernel.shmall\": Invalid argument", "stderr_lines": ["sysctl: setting key \"kernel.shmall\": Invalid argument"], "stdout": "kernel.shmall = [u'1021896']", "stdout_lines": ["kernel.shmall = [u'1021896']"]}

PLAY RECAP ***********************************************************************************************
fossology_test             : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

I am using ansible 2.9.6

dinesh@dinesh-VirtualBox:/$ ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/dinesh/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]

Upvotes: 0

Views: 325

Answers (1)

mdaniel
mdaniel

Reputation: 33231

As you can very clearly see in the set_fact results dict, the output of regexp_search is a list of matched strings, not the just the capture group. And, because your regex is imprecise, that's why your shmmax is the numbers plus a newline plus the rest of the text.

The accurate regex is shmmax=([0-9]+) because those values aren't "any character except a double quote" it's "any number after the equals sign"

Upvotes: 2

Related Questions