Question:
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…?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
--- - 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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 🙂
Answer:
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:
1 2 3 4 5 6 7 8 9 10 |
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 |