Ansible task to confirm if a process is running The Next CEO of Stack OverflowAnsible...

Skipping indices in a product

Does it take more energy to get to Venus or to Mars?

Is HostGator storing my password in plaintext?

What is the purpose of the Evocation wizard's Potent Cantrip feature?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Is it professional to write unrelated content in an almost-empty email?

Several mode to write the symbol of a vector

Is there a difference between "Fahrstuhl" and "Aufzug"

What exact does MIB represent in SNMP? How is it different from OID?

Bold, vivid family

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

How do I go from 300 unfinished/half written blog posts, to published posts?

Between two walls

How do scammers retract money, while you can’t?

Novel about a guy who is possessed by the divine essence and the world ends?

SQL Server 2016 - excessive memory grant warning on poor performing query

Preparing Indesign booklet with .psd graphics for print

How to make a variable always equal to the result of some calculations?

Why do professional authors make "consistency" mistakes? And how to avoid them?

What expression will give age in years in QGIS?

Should I tutor a student who I know has cheated on their homework?

What benefits would be gained by using human laborers instead of drones in deep sea mining?

What is ( CFMCC ) on ILS approach chart?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?



Ansible task to confirm if a process is running



The Next CEO of Stack OverflowAnsible command-task runs into “Exec format error”What is the significance of an Ansible task reporting that something has changed?Ansible Determine Operating Systemansible: silently skip unreachable hostsIs There An Ansible Task To Create Servicesansible: execute local_action only once while running playbook against a group of hosts (multiple managed hosts)Ansible: replace cannot find a locally existing fileAnsible hangs after parsing inventoryAnsible - ios_vlan unable to run any commands that require privilege modeAnsible playbook to determine OS release












8















Ansible 2.1



In the playbook, I started a process:



- name: Start Automation Agent, and enable start on boot
service: name=mongodb-mms-automation-agent state=started enabled=yes


From play recap, it appears the process has successfully started.



TASK [install : Start automation agent, and enable start on boot] **************
changed: [server1]


However, when log in to remote host and do a ps, the process is not running. Checking on process log it had failed some pre-requisite (intended).



How do I write a task in a playbook to confirm the process has successfully started?










share|improve this question



























    8















    Ansible 2.1



    In the playbook, I started a process:



    - name: Start Automation Agent, and enable start on boot
    service: name=mongodb-mms-automation-agent state=started enabled=yes


    From play recap, it appears the process has successfully started.



    TASK [install : Start automation agent, and enable start on boot] **************
    changed: [server1]


    However, when log in to remote host and do a ps, the process is not running. Checking on process log it had failed some pre-requisite (intended).



    How do I write a task in a playbook to confirm the process has successfully started?










    share|improve this question

























      8












      8








      8


      4






      Ansible 2.1



      In the playbook, I started a process:



      - name: Start Automation Agent, and enable start on boot
      service: name=mongodb-mms-automation-agent state=started enabled=yes


      From play recap, it appears the process has successfully started.



      TASK [install : Start automation agent, and enable start on boot] **************
      changed: [server1]


      However, when log in to remote host and do a ps, the process is not running. Checking on process log it had failed some pre-requisite (intended).



      How do I write a task in a playbook to confirm the process has successfully started?










      share|improve this question














      Ansible 2.1



      In the playbook, I started a process:



      - name: Start Automation Agent, and enable start on boot
      service: name=mongodb-mms-automation-agent state=started enabled=yes


      From play recap, it appears the process has successfully started.



      TASK [install : Start automation agent, and enable start on boot] **************
      changed: [server1]


      However, when log in to remote host and do a ps, the process is not running. Checking on process log it had failed some pre-requisite (intended).



      How do I write a task in a playbook to confirm the process has successfully started?







      ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 3 '16 at 17:36









      Howard LeeHoward Lee

      3834510




      3834510






















          2 Answers
          2






          active

          oldest

          votes


















          9














          You can check with the failed Jinja2 filter after running your command that checks if the process is running.



          Here is an example that uses the output of the command systemctl status apache2 to decide if Apache is running:



          - name: Check if Apache is running
          command: systemctl status apache2
          ignore_errors: yes
          changed_when: false
          register: service_apache_status

          - name: Report status of Apache
          fail:
          msg: |
          Service apache2 is not running.
          Output of `systemctl status apache2`:
          {{ service_apache_status.stdout }}
          {{ service_apache_status.stderr }}
          when: service_apache_status | failed


          If the command of the first task failed, the second task will fail and show why the first task failed.


          The return code is stored in service_apache_status.rc.



          Example output of a failure:



          TASK: [Check if Apache is running] *********************** 
          failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", " Loaded: not-found (Reason: No such file or directory)", " Active: inactive (dead)"], "warnings": []}
          stdout: * apache2.service
          Loaded: not-found (Reason: No such file or directory)
          Active: inactive (dead)
          ...ignoring

          TASK: [Report status of Apache] ***************************
          failed: [localhost] => {"failed": true}
          msg: apache2 is not running
          systemctl status apache2 output:
          * apache2.service
          Loaded: not-found (Reason: No such file or directory)
          Active: inactive (dead)


          Here is different (albeit possibly less reliable) way, using pgrep, to check if the process is running:



          - name: Check if Apache is running
          shell: pgrep apache2
          ignore_errors: yes
          changed_when: false
          register: service_apache_status

          - name: Report status of Apache
          fail:
          msg: |
          Service apache2 is not running.
          Return code from `pgrep`:
          {{ service_apache_status.rc }}
          when: service_apache_status.rc != 0





          share|improve this answer


























          • How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

            – Howard Lee
            Jun 7 '16 at 21:15






          • 2





            @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

            – Deltik
            Jun 7 '16 at 21:17






          • 1





            instead of ps | grep you could try pgrep apache2

            – madeddie
            7 hours ago











          • @madeddie: I like it. Answer updated to suggest pgrep!

            – Deltik
            1 hour ago



















          4














          This is what I do now:



          - name: Confirm Automation Agent is running
          command: service mongodb-mms-automation-agent status
          register: agent_status
          failed_when: "'NOT' in agent_status.stdout"
          changed_when: False


          failed_when is introduced in 1.4. changed_when: False is used to suppress change status. Read more.






          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1084129%2fansible-task-to-confirm-if-a-process-is-running%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            9














            You can check with the failed Jinja2 filter after running your command that checks if the process is running.



            Here is an example that uses the output of the command systemctl status apache2 to decide if Apache is running:



            - name: Check if Apache is running
            command: systemctl status apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Output of `systemctl status apache2`:
            {{ service_apache_status.stdout }}
            {{ service_apache_status.stderr }}
            when: service_apache_status | failed


            If the command of the first task failed, the second task will fail and show why the first task failed.


            The return code is stored in service_apache_status.rc.



            Example output of a failure:



            TASK: [Check if Apache is running] *********************** 
            failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", " Loaded: not-found (Reason: No such file or directory)", " Active: inactive (dead)"], "warnings": []}
            stdout: * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)
            ...ignoring

            TASK: [Report status of Apache] ***************************
            failed: [localhost] => {"failed": true}
            msg: apache2 is not running
            systemctl status apache2 output:
            * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)


            Here is different (albeit possibly less reliable) way, using pgrep, to check if the process is running:



            - name: Check if Apache is running
            shell: pgrep apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Return code from `pgrep`:
            {{ service_apache_status.rc }}
            when: service_apache_status.rc != 0





            share|improve this answer


























            • How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

              – Howard Lee
              Jun 7 '16 at 21:15






            • 2





              @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

              – Deltik
              Jun 7 '16 at 21:17






            • 1





              instead of ps | grep you could try pgrep apache2

              – madeddie
              7 hours ago











            • @madeddie: I like it. Answer updated to suggest pgrep!

              – Deltik
              1 hour ago
















            9














            You can check with the failed Jinja2 filter after running your command that checks if the process is running.



            Here is an example that uses the output of the command systemctl status apache2 to decide if Apache is running:



            - name: Check if Apache is running
            command: systemctl status apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Output of `systemctl status apache2`:
            {{ service_apache_status.stdout }}
            {{ service_apache_status.stderr }}
            when: service_apache_status | failed


            If the command of the first task failed, the second task will fail and show why the first task failed.


            The return code is stored in service_apache_status.rc.



            Example output of a failure:



            TASK: [Check if Apache is running] *********************** 
            failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", " Loaded: not-found (Reason: No such file or directory)", " Active: inactive (dead)"], "warnings": []}
            stdout: * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)
            ...ignoring

            TASK: [Report status of Apache] ***************************
            failed: [localhost] => {"failed": true}
            msg: apache2 is not running
            systemctl status apache2 output:
            * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)


            Here is different (albeit possibly less reliable) way, using pgrep, to check if the process is running:



            - name: Check if Apache is running
            shell: pgrep apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Return code from `pgrep`:
            {{ service_apache_status.rc }}
            when: service_apache_status.rc != 0





            share|improve this answer


























            • How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

              – Howard Lee
              Jun 7 '16 at 21:15






            • 2





              @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

              – Deltik
              Jun 7 '16 at 21:17






            • 1





              instead of ps | grep you could try pgrep apache2

              – madeddie
              7 hours ago











            • @madeddie: I like it. Answer updated to suggest pgrep!

              – Deltik
              1 hour ago














            9












            9








            9







            You can check with the failed Jinja2 filter after running your command that checks if the process is running.



            Here is an example that uses the output of the command systemctl status apache2 to decide if Apache is running:



            - name: Check if Apache is running
            command: systemctl status apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Output of `systemctl status apache2`:
            {{ service_apache_status.stdout }}
            {{ service_apache_status.stderr }}
            when: service_apache_status | failed


            If the command of the first task failed, the second task will fail and show why the first task failed.


            The return code is stored in service_apache_status.rc.



            Example output of a failure:



            TASK: [Check if Apache is running] *********************** 
            failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", " Loaded: not-found (Reason: No such file or directory)", " Active: inactive (dead)"], "warnings": []}
            stdout: * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)
            ...ignoring

            TASK: [Report status of Apache] ***************************
            failed: [localhost] => {"failed": true}
            msg: apache2 is not running
            systemctl status apache2 output:
            * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)


            Here is different (albeit possibly less reliable) way, using pgrep, to check if the process is running:



            - name: Check if Apache is running
            shell: pgrep apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Return code from `pgrep`:
            {{ service_apache_status.rc }}
            when: service_apache_status.rc != 0





            share|improve this answer















            You can check with the failed Jinja2 filter after running your command that checks if the process is running.



            Here is an example that uses the output of the command systemctl status apache2 to decide if Apache is running:



            - name: Check if Apache is running
            command: systemctl status apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Output of `systemctl status apache2`:
            {{ service_apache_status.stdout }}
            {{ service_apache_status.stderr }}
            when: service_apache_status | failed


            If the command of the first task failed, the second task will fail and show why the first task failed.


            The return code is stored in service_apache_status.rc.



            Example output of a failure:



            TASK: [Check if Apache is running] *********************** 
            failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", " Loaded: not-found (Reason: No such file or directory)", " Active: inactive (dead)"], "warnings": []}
            stdout: * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)
            ...ignoring

            TASK: [Report status of Apache] ***************************
            failed: [localhost] => {"failed": true}
            msg: apache2 is not running
            systemctl status apache2 output:
            * apache2.service
            Loaded: not-found (Reason: No such file or directory)
            Active: inactive (dead)


            Here is different (albeit possibly less reliable) way, using pgrep, to check if the process is running:



            - name: Check if Apache is running
            shell: pgrep apache2
            ignore_errors: yes
            changed_when: false
            register: service_apache_status

            - name: Report status of Apache
            fail:
            msg: |
            Service apache2 is not running.
            Return code from `pgrep`:
            {{ service_apache_status.rc }}
            when: service_apache_status.rc != 0






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered Jun 6 '16 at 15:36









            DeltikDeltik

            13.4k144887




            13.4k144887













            • How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

              – Howard Lee
              Jun 7 '16 at 21:15






            • 2





              @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

              – Deltik
              Jun 7 '16 at 21:17






            • 1





              instead of ps | grep you could try pgrep apache2

              – madeddie
              7 hours ago











            • @madeddie: I like it. Answer updated to suggest pgrep!

              – Deltik
              1 hour ago



















            • How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

              – Howard Lee
              Jun 7 '16 at 21:15






            • 2





              @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

              – Deltik
              Jun 7 '16 at 21:17






            • 1





              instead of ps | grep you could try pgrep apache2

              – madeddie
              7 hours ago











            • @madeddie: I like it. Answer updated to suggest pgrep!

              – Deltik
              1 hour ago

















            How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

            – Howard Lee
            Jun 7 '16 at 21:15





            How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?

            – Howard Lee
            Jun 7 '16 at 21:15




            2




            2





            @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

            – Deltik
            Jun 7 '16 at 21:17





            @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.

            – Deltik
            Jun 7 '16 at 21:17




            1




            1





            instead of ps | grep you could try pgrep apache2

            – madeddie
            7 hours ago





            instead of ps | grep you could try pgrep apache2

            – madeddie
            7 hours ago













            @madeddie: I like it. Answer updated to suggest pgrep!

            – Deltik
            1 hour ago





            @madeddie: I like it. Answer updated to suggest pgrep!

            – Deltik
            1 hour ago













            4














            This is what I do now:



            - name: Confirm Automation Agent is running
            command: service mongodb-mms-automation-agent status
            register: agent_status
            failed_when: "'NOT' in agent_status.stdout"
            changed_when: False


            failed_when is introduced in 1.4. changed_when: False is used to suppress change status. Read more.






            share|improve this answer




























              4














              This is what I do now:



              - name: Confirm Automation Agent is running
              command: service mongodb-mms-automation-agent status
              register: agent_status
              failed_when: "'NOT' in agent_status.stdout"
              changed_when: False


              failed_when is introduced in 1.4. changed_when: False is used to suppress change status. Read more.






              share|improve this answer


























                4












                4








                4







                This is what I do now:



                - name: Confirm Automation Agent is running
                command: service mongodb-mms-automation-agent status
                register: agent_status
                failed_when: "'NOT' in agent_status.stdout"
                changed_when: False


                failed_when is introduced in 1.4. changed_when: False is used to suppress change status. Read more.






                share|improve this answer













                This is what I do now:



                - name: Confirm Automation Agent is running
                command: service mongodb-mms-automation-agent status
                register: agent_status
                failed_when: "'NOT' in agent_status.stdout"
                changed_when: False


                failed_when is introduced in 1.4. changed_when: False is used to suppress change status. Read more.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 8 '16 at 20:35









                Howard LeeHoward Lee

                3834510




                3834510






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1084129%2fansible-task-to-confirm-if-a-process-is-running%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Cannot install PyQt5 The Next CEO of Stack OverflowCannot install tcpreplay 3.4.4cannot...

                    Kapp-Putsch Acontecimentos | Outros artigos | Menu de navegação

                    Why did early computer designers eschew integers? The Next CEO of Stack OverflowWhat register...