Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Ansible variable property

Using Ansible I'm having a problem registering a variable the way I want. Using the implementation below I will always have to call .stdout on the variable - is there a way I can do better?

My playbook: Note the unwanted use of .stdout - I just want to be able to use the variable directly without calling a propery...?

---
- 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

The output when I execute the playbook:

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

Any help or insights appreciated - thank you :)

like image 633
dutoitns Avatar asked May 27 '16 09:05

dutoitns


1 Answers

If I understood it right you want to assign deploy_dir.stdout to a variable that you can use without stdout key. It can be done with set_fact module:

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
like image 143
Pasi H Avatar answered Sep 18 '22 15:09

Pasi H