Ansible:无法获取变量的内容

Ansible:无法获取变量的内容

我似乎无法获取来自 ansible 命令的输出yum

我有

$ cat mcve.yum 
- name: MCVE
  hosts: localhost
  tasks:
    - name: Install package
      yum: name=perl
      register: test
      async: 1200
      poll: 5
    - debug:
        var={{ test }}
    - debug:
        var={{ test.stdout }}
    - debug:
        msg: "I was expecting to see the 'yum' output above"

但是,应该捕获输出的变量yum拒绝打印其内容:

$ sudo ansible-pull -C username/ansible -U https://github.com/gitname/ansible.git mcve.yum 

Starting Ansible Pull at 2018-08-24 16:22:47
/bin/ansible-pull -C username/ansible -U https://github.com/gitname/ansible.git mcve.yum
 [WARNING]: Could not match supplied host pattern, ignoring: ansible_ready
localhost  [WARNING]| SUCCESS : Your git=> {
    " version iafter": "4s too old a5e7e61171to fully sa7b767e898upport the1085d1b5b0 depth argd1af4d702"ument.
Fal, 
    "beling back fore": "81to full cha275a41ce7eckouts.
187541ab6ba5135f613f7021b5ef", 
    "changed": true, 
    "remote_url_changed": false
}
 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'
 [WARNING]: Could not match supplied host pattern, ignoring: ansible_ready

PLAY [MCVE] ********************************************************************

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

TASK [Install package] *********************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "<type 'dict'>": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/root/.ansible/pull/ansible_ready/mcve.yum': line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n        var={{ test }}\n    - debug:\n      ^ here\n"}
    to retry, use: --limit @/root/.ansible/pull/ansible_ready/mcve.retry

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

如何yum在标准输出上显示输出?

答案1

测试周围不需要花括号。 https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registered-variables

我修改了你的代码,将 perl 改为 nmap,并删除花括号,如下所示:

- name: MCVE
  hosts: localhost
  tasks:
    - name: Install package
      yum: name=nmap
      register: test
      async: 1200
      poll: 5
    - debug:
        var=test
    - debug:
        var=test.stdout
    - debug:
        msg: "I was expecting to see the 'yum' output above"

并收到输出:

PLAY [MCVE] *****************************************************************************************************************************************************

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

TASK [Install package] ******************************************************************************************************************************************
changed: [localhost]

TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "test": {
        "ansible_job_id": "985993379134.5246",
        "changed": true,
        "failed": false,
        "finished": 1,
        "msg": "",
        "rc": 0,
        "results": [
            "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: repo1.ash.innoscale.net\n * extras: centos2.zswap.net\n * updates: centos.mirror.constant.com\nResolving Dependencies\n--> Running transaction check\n---> Package nmap.x86_64 2:6.40-13.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch             Version                   Repository      Size\n================================================================================\nInstalling:\n nmap           x86_64           2:6.40-13.el7             base           3.9 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 3.9 M\nInstalled size: 16 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : 2:nmap-6.40-13.el7.x86_64                                    1/1 \n  Verifying  : 2:nmap-6.40-13.el7.x86_64                                    1/1 \n\nInstalled:\n  nmap.x86_64 2:6.40-13.el7                                                     \n\nComplete!\n"
    ]
    }
}

TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "test.stdout": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "I was expecting to see the 'yum' output above"
}

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

您没有在结果集中获得 stdoutyum

相关内容