Ansible - 获取寄存器值的前几个字符

时间:2015-08-19 09:32:39

标签: ansible ansible-playbook

我想从注册变量中获取前几个字符。有人可以建议我怎么做。

   - 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] }}"

上面的代码显示了注册字符串的第五个字符,而不是前5个字符。

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

1 个答案:

答案 0 :(得分:11)

您可以使用范围:

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

这将打印前5个字符。

  • 1:5将打印字符2到5,跳过第一个字符。
  • 5:将跳过前5个字符并打印其余字符串