How to assign set_fact variable some value based on condition ?

43 views
Skip to first unread message

Mohtashim S

unread,
Sep 5, 2019, 7:45:35 AM9/5/19
to Ansible Project
MYUSER should be assigned value "wsadm" or "user1" based on the output of {{ command_result.stdout.split('\t')[1] }}

I tried the below however, I'm getting error when I run.

My playbook looks like below:

---
- name: "Play 1-Find the details here"


  hosts
: localhost
  gather_facts
: no


  tasks
:
   
- name: "Search for {{ Number }} in the database"


     command
: >
       mysql
--user=root --password=mypass deployment
       
--host=localhost  -Ns -e "SELECT dest_ip,layer FROM deploy_dets WHERE num LIKE '{{ Number }}'"
     
register: command_result


   
- set_fact:


       dest_ip
: "{{ command_result.stdout.split('\t')[0] }}"
       MYUSER
: "wsadm"
     
when: "{{ command_result.stdout.split('\t')[1] }}" == 'WAS'
       MYUSER
: "user1"
     
when: "{{ command_result.stdout.split('\t')[1] }}" == 'APP'




   
- name: "Print output for Number Search"
     debug
:
       msg
: "{{ command_result.stdout.split('\t')[1] }}"
       msg
: "{{ MYUSER }}"



Error Output:

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! Syntax Error while loading YAML.
  did not find expected key
The error appears to be in '/app/axmw/Ansible/playbook/finacle_deployment/assignvars.yml': line 22, column 57, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
       MYUSER: "wsadm"
     when: "{{ command_result.stdout.split('\t')[1] }}" == 'WAS'
                                                        ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value.                               

Can you please suggest ?

Dick Visser

unread,
Sep 5, 2019, 8:33:37 AM9/5/19
to ansible...@googlegroups.com
Remove the curly braces in your condition 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d335b876-90da-4fb5-be2e-972bc08e3c73%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Mohtashim S

unread,
Sep 5, 2019, 8:53:40 AM9/5/19
to Ansible Project
I tried both suggestion but it still errors out:

       MYUSER: "wsadm"
     
when: command_result.stdout.split('\t')[3] == 'WAS'
       MYUSER
: "user1"
     
when: command_result.stdout.split('\t')[3]  == 'APP'


       MYUSER: "wsadm"
     
when: "command_result.stdout.split('\t')[3]" == 'WAS'
       MYUSER
: "user1"
     
when: "command_result.stdout.split('\t')[3]"  == 'APP'


Still getting error:

The offending line appears to be:
       MYUSER: "wsadm"
     when: "command_result.stdout.split('\t')[3]" == 'WAS'
                                                                           ^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages