'saltstack cmd.script cause stall

i find a strange things ,but i do not know why, i build a salt init.sls to install and init mysql database,following is my script,and i find all work fine ,except the script do not finish.anyone can help?

# ps -ef|grep __salt  
root     15510 15501  0 16:40 ?        00:00:00 [__salt.tmp.cZvT] <defunct>


mysql-community-server:
  pkg:
    - installed


mysql_conf:
  file.managed:
    - name: /etc/my.cnf
    - source: ftp://10.0.0.4/config_file/salt/mysql/my.cnf
    - source_hash: ftp://10.0.0.4/config_file/salt/mysql/my.cnf.sha512
    - template: jinja
    - defaults:
       hostname: {{ grains['host'] }}
    - require:
      - pkg: mysql-community-server

/root/.bashrc:
  file.append:
    - text:
       - alias my3306='mysql -uroot -pDsuBbT6u4MYcCGm0% -S/tmp/mysql.sock --prompt="\\u@\\h:\\d \\r:\\m:\\s>"'

mysql_init:
  cmd.script:
    - source: ftp://10.0.0.4/config_file/salt/mysql/init_mysql.sh
    - source_hash: ftp://10.0.0.4/config_file/salt/mysql/init_mysql.sh.sha512
    - cwd: /tmp
    - require:
      - file: mysql_conf


# cat /tmp/__salt.tmp.cZvT6R.sh
#!/bin/bash

sql_user="alter user 'root'@'localhost' identified by 'my password'"

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
if [ $? = 0 ];then
service mysqld start
echo "${sql_user}" | mysql -uroot -S/tmp/mysql.sock
else
echo init failed && exit 1
fi
exit 0

# this command stall
# salt 'mysqltest1' state.sls mysql



Solution 1:[1]

Avoid using interactive programs in cmd.run or cmd.script. The mysql command line client provides a mysql --batch argument to avoid blocking on interactive prompts. Avoid pipelines where possible to simplify the (fallible) shell command execution environment.

For best results, use the mysql execution module (state module if available), and make sure the appropriate mysql python module is available to the salt-minion. Consider using community formulas like https://github.com/saltstack-formulas/mysql-formula

If you must use cmd.run or cmd.script: Don't run multiple steps in a script. There's no way to tell what is going wrong. Break it up into multiple salt states and provide requisites to ensure they run in the correct order under the correct conditions.

instead of the shell script you could have used state sls looking something like this:

mysqld initialized insecure:
  cmd.run:
    - name: mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
    - creates: /data/mysql/ibdata1
    - watch:
      - pkg: mysql-community-server

mysqld service running:
  service.running:
    - name: mysqld
    - require:
      - mysqld initialize insecure

{% set my_password = salt['pillar.get']('mysql:user:root:password') %}
mysqld local root password:
  cmd.run:
    - name: mysql -uroot -S/tmp/mysql.sock --batch --execute "alter user 'root'@'localhost' identified by '{{ my_password }}'"
    - prereq:
      - mysqld initialized insecure
    - require:
      - mysqld service running

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy