Ansible 寄存器可转换变量属性

Ansible 寄存器可转换变量属性,ansible,ansible-playbook,Ansible,Ansible Playbook,使用Ansible时,我在以我想要的方式注册变量时遇到问题。使用下面的实现,我将始终必须调用变量上的.stdout-有没有更好的方法 我的剧本: 注意.stdout的不必要使用-我只想能够直接使用变量而不调用属性 --- - name: prepare for new deployment hosts: all user: ser85 tasks: - name: init deploy dir shell: echo ansible-deploy-$(date +%

使用Ansible时,我在以我想要的方式注册变量时遇到问题。使用下面的实现,我将始终必须调用变量上的.stdout-有没有更好的方法

我的剧本: 注意.stdout的不必要使用-我只想能够直接使用变量而不调用属性

---
- name: prepare for new deployment
  hosts: all
  user: ser85

  tasks:

  - name: init deploy dir
    shell: echo ansible-deploy-$(date +%Y%m%d-%H%M%S-%N)
    # http://docs.ansible.com/ansible/playbooks_variables.html
    register: deploy_dir

  - debug: var=deploy_dir

  - debug: var=deploy_dir.stdout

  - name: init scripts dir
    shell: echo {{ deploy_dir.stdout }}/scripts
    register: scripts_dir

  - debug: var=scripts_dir.stdout
执行剧本时的输出:

TASK [init deploy dir] *********************************************************
changed: [123.123.123.123]

TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
    "deploy_dir": {
        "changed": true,
        "cmd": "echo ansible-deploy-$(date +%Y%m%d-%H%M%S-%N)",
        "delta": "0:00:00.002898",
        "end": "2016-05-27 10:53:38.122217",
        "rc": 0,
        "start": "2016-05-27 10:53:38.119319",
        "stderr": "",
        "stdout": "ansible-deploy-20160527-105338-121888719",
        "stdout_lines": [
            "ansible-deploy-20160527-105338-121888719"
        ],
        "warnings": []
    }
}

TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
    "deploy_dir.stdout": "ansible-deploy-20160527-105338-121888719"
}

TASK [init scripts dir] ********************************************************
changed: [123.123.123.123]

TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
    "scripts_dir.stdout": "ansible-deploy-20160527-105338-121888719/scripts"
}

感谢您提供的任何帮助或见解-谢谢:)

如果我理解正确,您希望将
deploy\u dir.stdout
分配给一个无需
stdout
键即可使用的变量。可通过以下模块完成:


谢谢你,这很有效!实际上,我重新使用了变量名{{deploy.dir}},您使用{{my_deploy_dir}}时没有任何问题。我想知道Ansible是否使用相同的位置来存储用户定义的事实和变量。。。不管怎样,它是有效的——谢谢:)
tasks:
  - name: init deploy dir
    shell: echo ansible-deploy-$(date +%Y%m%d-%H%M%S-%N)
    # http://docs.ansible.com/ansible/playbooks_variables.html
    register: deploy_dir

  - set_fact: my_deploy_dir="{{ deploy_dir.stdout }}"

  - debug: var=my_deploy_dir