setting variables depending on other variable value

65 views
Skip to first unread message

fanvalt

unread,
May 19, 2016, 10:47:15 AM5/19/16
to Ansible Project
Hello,

I would like to set in my playbook a variable depending on the ansible_user value.

If I set it twice, an error occurs, how can I write it:

vars:
  rep_user: {{ ansible_user }}
  rep_user: /home/{{ ansible_user }}
  when: ansible_user == fval

Si I would test my playbook with the fval ansible_user to deploy in my home directory, and when I will tun it for good, it will deploy on another directory.

Thank you

Brian Coca

unread,
May 19, 2016, 1:01:02 PM5/19/16
to ansible...@googlegroups.com
when: ONLY applies to tasks, for vars use ternary filter.

rep_user: "{{ (ansible_user == fval)|ternary('/home/' + ansible_user, ansible_user) }}"


----------
Brian Coca
Message has been deleted

fanvalt

unread,
May 23, 2016, 4:50:55 AM5/23/16
to Ansible Project


Le lundi 23 mai 2016 10:32:22 UTC+2, fanvalt a écrit :
Thank you but when I run the playbook with a debug module to dispay the rep_user variable, I am expecting the /home/fval value but I display the whole instruction:

in my main.yml tasks:
- debug: msg="{{ rep_user }}"


TASK [openhr : debug] **********************************************************
ok: [localhost] => {
    "msg": "{{ (ansible_user == fval)|ternary('/home/' + ansible_user, ansible_user) }} "

fanvalt

unread,
May 24, 2016, 6:46:48 AM5/24/16
to Ansible Project
I did bypass the issue by using a variable that I set previously when testing to /home/fval
When I will test for real, I will set it to {{ ansible_user }}
Reply all
Reply to author
Forward
0 new messages