How can wait until ansible script execute in nodejs

Viewed 1982

I am working on nodejs + ansible module, our module call ansible script, then ansible script will execute the process in remote machine. Finally I render ansible result and view via fontend. while execute the ansible script, browser goes to connection was reset.

my code:

 app.post('/create', function (req, res) {

  var playbook = new Ansible.Playbook().playbook("m2install");
    playbook.on('stdout', function (data) {
        console.log(data.toString());
    });
    playbook.on('stderr', function (data) {
        console.log(data.toString());
    });
    var command = playbook.inventory('inventories/M2Install/hosts').verbose('v');
    var promise = command.exec();
    promise.then(function (successResult) {
        tmp += "Create Magento Setup";
        tmp += successResult.output;
        fs.unlink('roles/install/vars/main.yml', function (err) {
            if (!err) {
                console.log('delete docker_variables.yml file ');
            } else {
                console.log('Not delete docker_variables.yml file');
            }
        });
        res.render("result", {data: tmp, title: "Magento Setup  Report"});
    }, function (error) {
        tmp += error;
        res.render("result", {data: tmp, title: "Magento Setup  Report"});
    });

  });


  app.post('/setup', function (req, res) {

      postcount = Object.keys(req.body).length
      console.log(postcount);

      for (var key in req.body) {

          console.log(req.body[key]);

          if (req.body[key] == "create") {
              res.render('create', {title: 'Magento Setup Form'});
              sucesscount++;
          }
          });

My ansible playbook:

---

- name: Build image and with buildargs
    docker_image:
       path: /opt/test/dockerfiles/mysql
       name: mysql

  - name: Build image and with buildargs
    docker_image:
       path: /opt/test/dockerfiles/nginx
       name: php_nginx

  - name: Create  Mysql Docker Container
    command: docker run -it -d --name m2_node_mysql --privileged mysql

  - name: Create  php+ nginx Docker Container
    command: docker run -it -d --name php_nginx  --privileged php_nginx

my ansible logs:

         mysql
  Using /etc/ansible/ansible.cfg as config file


  PLAY [webservers] **************************************************************


  TASK [Gathering Facts] *********************************************************

  ok: [192.168.1.101]


  TASK [mysql : Install Docker py] ***********************************************

  changed: [192.168.1.101] => {"changed": true, "cmd": ["pip", "install", "docker-py"], "delta": "0:00:01.409080", "end": "2017-08-01 11:15:19.196183", "rc": 0, "start": "2017-08-01 11:15:17.787103", "stderr": "You are using pip version 8.1.1, however version 9.0.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.", "stderr_lines": ["You are using pip version 8.1.1, however version 9.0.1 is available.", "You should consider upgrading via the 'pip install --upgrade pip' command."], "stdout": "Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages\nRequirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)", "stdout_lines": ["Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages", "Requirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)"]}


  TASK [mysql : Build Mysql image and with buildargs] ****************************

  1
  mysql
  Using /etc/ansible/ansible.cfg as config file


  PLAY [webservers] **************************************************************


  TASK [Gathering Facts] *********************************************************

  ok: [192.168.1.101]


  TASK [mysql : Install Docker py] ***********************************************

  changed: [192.168.1.101] => {"changed": true, "cmd": ["pip", "install", "docker-py"], "delta": "0:00:00.282909", "end": "2017-08-01 11:17:09.426975", "rc": 0, "start": "2017-08-01 11:17:09.144066", "stderr": "You are using pip version 8.1.1, however version 9.0.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.", "stderr_lines": ["You are using pip version 8.1.1, however version 9.0.1 is available.", "You should consider upgrading via the 'pip install --upgrade pip' command."], "stdout": "Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages\nRequirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)", "stdout_lines": ["Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages", "Requirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)"]}


  TASK [mysql : Build Mysql image and with buildargs] ****************************

  changed: [192.168.1.101] => {"actions": ["Built image m2_node_mysql_test2:latest from /opt/magento2/dockerfiles/mysql"], "changed": true, "image": {"Architecture": "amd64", "Author": "", "Comment": "", "Config": {"ArgsEscaped": true, "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": ["mysqld"], "Domainname": "", "Entrypoint": ["docker-entrypoint.sh"], "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.6", "MYSQL_VERSION=5.6.37-1debian8"], "ExposedPorts": {"3306/tcp": {}}, "Hostname": "e24d0b5cccc1", "Image": "sha256:c314e2de3d08e55873356869f05407e24586510110b1f95ff0e27a694b0608ed", "Labels": {}, "OnBuild": [], "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": {"/var/lib/mysql": {}}, "WorkingDir": ""}, "Container": "144eb23225ba2e493f3918cfd02e8c4c1a810df5f50cc55cb9c3925436efec5a", "ContainerConfig": {"ArgsEscaped": true, "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": ["/bin/sh", "-c", "#(nop) CMD [\"mysqld\"]"], "Domainname": "", "Entrypoint": ["docker-entrypoint.sh"], "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.6", "MYSQL_VERSION=5.6.37-1debian8"], "ExposedPorts": {"3306/tcp": {}}, "Hostname": "e24d0b5cccc1", "Image": "sha256:c314e2de3d08e55873356869f05407e24586510110b1f95ff0e27a694b0608ed", "Labels": {}, "OnBuild": [], "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": {"/var/lib/mysql": {}}, "WorkingDir": ""}, "Created": "2017-08-01T05:54:47.287482027Z", "DockerVersion": "1.10.3", "GraphDriver": {"Data": {"DeviceId": "3240", "DeviceName": "docker-253:3-11010052-6d8fd3972362865502b6e401c81da0ddcb39058e379c008aeae17b382046b81c", "DeviceSize": "10737418240"}, "Name": "devicemapper"}, "Id": "sha256:b9a84cf1b66bd54dd24b92171ee7b5e52566bf1dfea5756ca7ae07736080e948", "Os": "linux", "Parent": "sha256:c314e2de3d08e55873356869f05407e24586510110b1f95ff0e27a694b0608ed", "RepoDigests": [], "RepoTags": ["m2_node_mysql_test2:latest"], "Size": 298292364, "VirtualSize": 298292364}}


  PLAY RECAP *********************************************************************

  192.168.1.101              : ok=3    changed=2    unreachable=0    failed=0   



  changed: [192.168.1.101] => {"actions": ["Built image m2_node_mysql_test2:latest from /opt/magento2/dockerfiles/mysql"], "changed": true, "image": {"Architecture": "amd64", "Author": "", "Comment": "", "Config": {"ArgsEscaped": true, "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": ["mysqld"], "Domainname": "", "Entrypoint": ["docker-entrypoint.sh"], "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.6", "MYSQL_VERSION=5.6.37-1debian8"], "ExposedPorts": {"3306/tcp": {}}, "Hostname": "e24d0b5cccc1", "Image": "sha256:a0c53461849600d1ee4bfa7be732beb49470e961fce46e42853489b68317a6ec", "Labels": {}, "OnBuild": [], "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": {"/var/lib/mysql": {}}, "WorkingDir": ""}, "Container": "c09b76549dc03e5ae0cb046853f0dd63f053ae9c8b47c6563a94220d87f56b3e", "ContainerConfig": {"ArgsEscaped": true, "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": ["/bin/sh", "-c", "#(nop) CMD [\"mysqld\"]"], "Domainname": "", "Entrypoint": ["docker-entrypoint.sh"], "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.6", "MYSQL_VERSION=5.6.37-1debian8"], "ExposedPorts": {"3306/tcp": {}}, "Hostname": "e24d0b5cccc1", "Image": "sha256:a0c53461849600d1ee4bfa7be732beb49470e961fce46e42853489b68317a6ec", "Labels": {}, "OnBuild": [], "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": {"/var/lib/mysql": {}}, "WorkingDir": ""}, "Created": "2017-08-01T05:55:47.56392089Z", "DockerVersion": "1.10.3", "GraphDriver": {"Data": {"DeviceId": "3262", "DeviceName": "docker-253:3-11010052-41d504d5ad8375f1f76d2af19a133982e189b31baeecfd775a133842cdbf0432", "DeviceSize": "10737418240"}, "Name": "devicemapper"}, "Id": "sha256:b8903c8b02f2b88db1a0b7ae9cbcfdd80188f63f37c5430167cf7e946069348b", "Os": "linux", "Parent": "sha256:a0c53461849600d1ee4bfa7be732beb49470e961fce46e42853489b68317a6ec", "RepoDigests": [], "RepoTags": ["m2_node_mysql_test2:latest"], "Size": 298292364, "VirtualSize": 298292364}}


  PLAY RECAP *********************************************************************

  192.168.1.101              : ok=3    changed=2    unreachable=0    failed=0   

I need to wait nodejs until ansible script execute.

Note: Node js called my ansible module twice, see my ansible log, Suggest Me Why? if node call second time ansible module, browser goes to Connection was reset.

Suggest Me, how to do that and How to fix this issue.

0 Answers
Related