Register Ansible variable property

30,969

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
Share:
30,969

Related videos on Youtube

dutoitns
Author by

dutoitns

15 years experience in IT in different roles

Updated on July 17, 2022

Comments

  • dutoitns
    dutoitns almost 2 years

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

  • dutoitns
    dutoitns almost 8 years
    Thank you, it works! I actually re-used the variable name {{ deploy.dir }} where you used {{ my_deploy_dir }} without any problems. I wonder if Ansible uses the same location to store user-defined facts and variables... Irregardless, it works - thank you :)

Related