Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Fetch first few character of a register value

I would like to fetch first few character from a registered variable. Can somebody please suggest me how to do that.

   - hosts: node1
     gather_facts: False
     tasks:
      - name: Check Value mango
        shell: cat /home/vagrant/mango
        register: result

      - name: Display Result In Loop
        debug: msg="Version is {{ result.stdout[5] }}"

The above code displays the fifth character rather first 5 characters of the registered string.

PLAY [node1] ******************************************************************

TASK: [Check Value mango] *****************************************************
changed: [10.200.19.21] => {"changed": true, "cmd": "cat /home/vagrant/mango", "delta": "0:00:00.003000", "end": "2015-08-19 09:29:58.229244", "rc": 0, "start": "2015-08-19 09:29:58.226244", "stderr": "", "stdout": "d3aa6131ec1a2e73f69ee150816265b5617d7e69", "warnings": []}

TASK: [Display Result In Loop] ************************************************
ok: [10.200.19.21] => {
    "msg": "Version is 1"
}

PLAY RECAP ********************************************************************
10.200.19.21               : ok=2    changed=1    unreachable=0    failed=0
like image 355
SPM Avatar asked Aug 19 '15 09:08

SPM


1 Answers

You can work with ranges:

  - name: Display Result In Loop
    debug: msg="Version is {{ result.stdout[:5] }}"

This will print the first 5 characters.

  • 1:5 would print the characters 2 to 5, skipping the 1st char.
  • 5: Would skip the the first 5 chars and print the rest of the string
like image 138
udondan Avatar answered Oct 01 '22 06:10

udondan