查看 Ansible 的 Register 变量内容

方式1. 在playbook 命令加入参数 -v 就可以打印出 register 的内容

playbook内容
[root@ansible1 ~]# cat test.yml

  • hosts: localhost
    gather_facts: no
    tasks:
    • name: test with register
      shell: cat /root/input.json
      register: result

playbook引用到的json文件内容
[root@ansible1 ~]# cat /root/input.json
{
services {
vsftpd
}
}

运行playbook
[root@ansible1 ~]# ansible-playbook -v test.yml
Using /etc/ansible/ansible.cfg as config file
/etc/ansible/hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/etc/ansible/hosts did not meet script requirements, check plugin documentation if this is unexpected

PLAY [localhost] ***************************************************************************************************

TASK [test with register] ******************************************************************************************
changed: [localhost] => {“changed”: true, “cmd”: “cat /root/input.json”, “delta”: “0:00:00.031795”, “end”: “2019-01-02 07:34:44.844675”, “rc”: 0, “start”: “2019-01-02 07:34:44.812880”, “stderr”: “”, “stderr_lines”: [], “stdout”: “{\n services {\n vsftpd\n }\n}”, “stdout_lines”: ["{", " services {", " vsftpd", " }", “}”]} #register的内容

PLAY RECAP *********************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0

方式2. 通过debug 模块打印出register的内容

[root@ansible1 ~]# cat debug-register.yml

  • hosts: localhost
    gather_facts: no
    tasks:
    • shell: echo hello world
      register: say_hi
    • debug: var=say_hi
    • debug: var=say_hi.stdout
    • debug: var=sya_hi[‘stdout’]

[root@ansible1 ~]# ansible-playbook debug-register.yml

PLAY [localhost] ****************************************************************************************************

TASK [shell] ********************************************************************************************************
changed: [localhost]

TASK [debug] ********************************************************************************************************
ok: [localhost] => {
“say_hi”: {
“changed”: true,
“cmd”: “echo hello world”,
“delta”: “0:00:00.033823”,
“end”: “2019-01-04 10:20:43.640881”,
“failed”: false,
“rc”: 0,
“start”: “2019-01-04 10:20:43.607058”,
“stderr”: “”,
“stderr_lines”: [],
“stdout”: “hello world”,
“stdout_lines”: [
“hello world”
]
}
}

PLAY RECAP **********************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0

猜你喜欢

转载自blog.csdn.net/weixin_44487339/article/details/86425223