How to get variables from an irreplaceable result

I have a shell script whose output is an echo of the following format
<variable_1>;<variable_2>;<variable_3> etc.

I want to use these variables and run a mysql query to update the database, for example, mysql -u<user> -p<password> -h<host> -e'insert into test_table values ("variable_1","variable_2","variable_3")'

My game with similar games looks like this.

 --- - hosts: infoServers sudo: yes gather_facts: no tasks: - name: gather info script: get_hostdata.sh register: result - name: print result local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }});' with_items: [ result.stdout.split(';')[0], result.stdout.split(';')[1], result.stdout.split(';')[2] ] 

ERROR: Syntax Error loading YAML script, test_variables.yml

Basically, I want to be able to use the output of a shell command, split it into some variables, and be able to use it in the following possible actions. Can you tell me how to access variables correctly?

thanks

+5
source share
2 answers

When you get such an error, you really must provide full error information. When I cut and pasted your playbook into a file and tried to run it, I got the following:

 ERROR: Syntax Error while loading YAML script, ff.yml Note: The error may actually appear before this position: line 11, column 43 local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }});' with_items: [ result.stdout.split(';')[0], result.stdout.split(';')[1], result.stdout.split(';')[2] ] ^ 

So, if this corresponds to the complete error you received, then the syntax of your with_items clause is incorrect.

I'm not sure why you are trying to do this using with_items. All you do in this case is an unnecessary change of variables. The following should also do exactly what you want:

 - name: print result local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ result.stdout.split(';')[0] }}","{{ result.stdout.split(';')[1] }}","{{ result.stdout.split(';')[2] }}");' 
+5
source

You need to quote and use {{}} correctly.

 - hosts: localhost tags: s16 gather_facts: no tasks: - shell: echo 'variable_1;variable_2;variable_3' register: result - local_action: debug msg="values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }}");'" with_items: [ "{{result.stdout.split(';')[0]}}", "{{result.stdout.split(';')[1]}}", "{{result.stdout.split(';')[2]}}" ] 

It will output something like:

 TASK: [debug msg="values ("{{ item[0] }}","{{ item[1] }}","{{ item[3] }}");'"] *** ok: [localhost -> 127.0.0.1] => (item=variable_1) => { "item": "variable_1", "msg": "values (\"v\",\"a\",\"i\");'" } ok: [localhost -> 127.0.0.1] => (item=variable_2) => { "item": "variable_2", "msg": "values (\"v\",\"a\",\"i\");'" } ok: [localhost -> 127.0.0.1] => (item=variable_3) => { "item": "variable_3", "msg": "values (\"v\",\"a\",\"i\");'" } 

As you can see with item[0], .., item[2] , you index the string "variable_1" instead of the array ["variable_1","variable_2","variable_3"]

The simplest (and much stronger) way to do this:

 - hosts: localhost tags: s17 gather_facts: no tasks: - shell: echo 'variable_1;variable_2;variable_3' register: result - debug: msg="insert into tt values ("{{result.stdout|replace(';', '","')}}");'" 
+3
source

Source: https://habr.com/ru/post/1216501/


All Articles