mercredi 25 septembre 2019

How to set value to Ansible variable upon condition in a loop

I have a with_item loop which has a variable 'USER' which needs to be assigned a value based on a condition check.

If '' is "Frontend" then USER variable should be user1 else it should be user2.

Below is something I could achieve after seeking help but the problem is that it always assigns 'user2' value. Never assigns user1 even if it should.

My playbook:

  - debug:
       msg: "User was "
     with_items: ""

   - set_fact:
        USER:  "user1"
     when: item.split('\t')[3] == "FrontEnd"
     with_items: ""

   - set_fact:
        USER:  "user2"
     when: item.split('\t')[3] == "BackEnd"
     with_items: ""

   - debug:
          msg: "User has "
     with_items: ""

The First debug prints and confirms that the value of item.split('\t')[3]

The second debug prints 'USER' but prints user2 even when the value is Frontend.

Can you please suggest?

TASK [debug] ************************************************************************************************************************************************ ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user1 2019-09-13 15:39 /was//testingjsp/testingcom.jsp 1786385840 /was//testingjsp FrontEnd) => { "msg": "User was FrontEnd" } ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user2 2019-09-13 15:29 /fin/scripts/testingscr.scr 367595418\n-rw-rw-r-- user2 2019-09-13 15:36 /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2 2019-09-13 15:37 /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2 2019-09-13 15:38 /fin/com/testingcom.com 90193476
/fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => { "msg": "User was BackEnd" }

TASK [debug] ************************************************************************************************************************************************ ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user1 2019-09-13 15:39 /was//testingjsp/testingcom.jsp 1786385840 /was//testingjsp FrontEnd) => { "msg": "User has user2" } ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user2 2019-09-13 15:29 /fin/scripts/testingscr.scr 367595418\n-rw-rw-r-- user2 2019-09-13 15:36 /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2 2019-09-13 15:37 /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2 2019-09-13 15:38 /fin/com/testingcom.com 90193476
/fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => { "msg": "User has user2" }

Aucun commentaire:

Enregistrer un commentaire