Run command on hosts and sum of stdout at the endRun command on the Ansible hostAnsible and `changed_when` based on `stdout` valueAnsible command execution errorconditional execution of playbook based on exit status of the command module?Ansible: Accumulate output across multiple hosts on task runHow to run Glusterfs-cli command “peer probe” using Ansible host group with condition “when”ansible multiple per-host output to fileAnsible - check for task failure during looping task's executionRunning sub shell command in Ansible ad-hoc

Does travel insurance for short flight delays exist?

Avoiding racist tropes in fantasy

How to respectfully refuse to assist co-workers with IT issues?

How do I request a longer than normal leave of absence period for my wedding?

Who was president?

Did a flight controller ever answer Flight with a no-go?

In an emergency, how do I find and share my position?

What is this symbol: semicircles facing eachother

How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?

Are there any music source codes for sound chips?

If all stars rotate, why was there a theory developed, that requires non-rotating stars?

Cross-referencing enumerate item

Are modern clipless shoes and pedals that much better than toe clips and straps?

C++20 constexpr std::copy optimizations for run-time

Checking a beta regression model via glmmTMB with DHARMa package

What is the hex versus octal timeline?

If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?

Can realistic planetary invasion have any meaningful strategy?

What is the history of the university asylum law?

I have a player who yells

Would it be possible to have a GMO that produces chocolate?

Prove your innocence

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

See details of old sessions



Run command on hosts and sum of stdout at the end


Run command on the Ansible hostAnsible and `changed_when` based on `stdout` valueAnsible command execution errorconditional execution of playbook based on exit status of the command module?Ansible: Accumulate output across multiple hosts on task runHow to run Glusterfs-cli command “peer probe” using Ansible host group with condition “when”ansible multiple per-host output to fileAnsible - check for task failure during looping task's executionRunning sub shell command in Ansible ad-hoc






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to run a command on y hostgroups which gives '1' in stdout. After running in all hosts, I want sum of all 1.



For example, it runs on 5 hosts, for all 5 hosts I'll get 1 ... at the end I want 5 as output



Or may be a way to execute the command and calculate the sum in one task only.



- name: Check file in tmp
shell: ls -ltrh /tmp/a.txt | wc -l
register: count
when: "'webserver' in group_names"









share|improve this question






























    0















    I want to run a command on y hostgroups which gives '1' in stdout. After running in all hosts, I want sum of all 1.



    For example, it runs on 5 hosts, for all 5 hosts I'll get 1 ... at the end I want 5 as output



    Or may be a way to execute the command and calculate the sum in one task only.



    - name: Check file in tmp
    shell: ls -ltrh /tmp/a.txt | wc -l
    register: count
    when: "'webserver' in group_names"









    share|improve this question


























      0












      0








      0








      I want to run a command on y hostgroups which gives '1' in stdout. After running in all hosts, I want sum of all 1.



      For example, it runs on 5 hosts, for all 5 hosts I'll get 1 ... at the end I want 5 as output



      Or may be a way to execute the command and calculate the sum in one task only.



      - name: Check file in tmp
      shell: ls -ltrh /tmp/a.txt | wc -l
      register: count
      when: "'webserver' in group_names"









      share|improve this question














      I want to run a command on y hostgroups which gives '1' in stdout. After running in all hosts, I want sum of all 1.



      For example, it runs on 5 hosts, for all 5 hosts I'll get 1 ... at the end I want 5 as output



      Or may be a way to execute the command and calculate the sum in one task only.



      - name: Check file in tmp
      shell: ls -ltrh /tmp/a.txt | wc -l
      register: count
      when: "'webserver' in group_names"






      ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 16:14









      RamanRaman

      195 bronze badges




      195 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          0















          An option would be to create a temporary file and use it for the shared purpose. The play below



          - hosts: test_jails
          gather_facts: true
          tasks:
          - name: Create tempfile
          tempfile:
          state: file
          register: fcounter
          run_once: true
          delegate_to: localhost
          - name: Write hostname to the tempfile
          shell: "echo ansible_hostname >> fcounter.path "
          delegate_to: localhost

          # < --------- HERE COMES THE PLAY --------- >

          - name: Print number of hosts
          shell: "cat fcounter.path | wc -l"
          register: counter
          run_once: true
          delegate_to: localhost
          - debug:
          var: counter.stdout
          run_once: true


          gives:



          ok: [test_01] => 
          "counter.stdout": "3"

          PLAY RECAP ****************************************************************
          test_01 : ok=5 changed=3 unreachable=0 failed=0
          test_02 : ok=2 changed=1 unreachable=0 failed=0
          test_03 : ok=2 changed=1 unreachable=0 failed=0





          share|improve this answer
































            0















            If I get your exact need, you can get the result without using shell. Please note that using json_query in my example below requires pip install jmespath. If this is not possible, you can most probably achieve the same result using the map filter combining 'extract' and 'attribute='.



            - name: Check file exists on server
            stat:
            path: /tmp/a.txt
            register: a_file
            when: whatever_condition

            - name: Count number of matches
            debug:
            msg: >-
            map('ternary', 1, 0)

            delegate_to: localhost
            run_once: true






            share|improve this answer





























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              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%2fstackoverflow.com%2fquestions%2f55361706%2frun-command-on-hosts-and-sum-of-stdout-at-the-end%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









              0















              An option would be to create a temporary file and use it for the shared purpose. The play below



              - hosts: test_jails
              gather_facts: true
              tasks:
              - name: Create tempfile
              tempfile:
              state: file
              register: fcounter
              run_once: true
              delegate_to: localhost
              - name: Write hostname to the tempfile
              shell: "echo ansible_hostname >> fcounter.path "
              delegate_to: localhost

              # < --------- HERE COMES THE PLAY --------- >

              - name: Print number of hosts
              shell: "cat fcounter.path | wc -l"
              register: counter
              run_once: true
              delegate_to: localhost
              - debug:
              var: counter.stdout
              run_once: true


              gives:



              ok: [test_01] => 
              "counter.stdout": "3"

              PLAY RECAP ****************************************************************
              test_01 : ok=5 changed=3 unreachable=0 failed=0
              test_02 : ok=2 changed=1 unreachable=0 failed=0
              test_03 : ok=2 changed=1 unreachable=0 failed=0





              share|improve this answer





























                0















                An option would be to create a temporary file and use it for the shared purpose. The play below



                - hosts: test_jails
                gather_facts: true
                tasks:
                - name: Create tempfile
                tempfile:
                state: file
                register: fcounter
                run_once: true
                delegate_to: localhost
                - name: Write hostname to the tempfile
                shell: "echo ansible_hostname >> fcounter.path "
                delegate_to: localhost

                # < --------- HERE COMES THE PLAY --------- >

                - name: Print number of hosts
                shell: "cat fcounter.path | wc -l"
                register: counter
                run_once: true
                delegate_to: localhost
                - debug:
                var: counter.stdout
                run_once: true


                gives:



                ok: [test_01] => 
                "counter.stdout": "3"

                PLAY RECAP ****************************************************************
                test_01 : ok=5 changed=3 unreachable=0 failed=0
                test_02 : ok=2 changed=1 unreachable=0 failed=0
                test_03 : ok=2 changed=1 unreachable=0 failed=0





                share|improve this answer



























                  0














                  0










                  0









                  An option would be to create a temporary file and use it for the shared purpose. The play below



                  - hosts: test_jails
                  gather_facts: true
                  tasks:
                  - name: Create tempfile
                  tempfile:
                  state: file
                  register: fcounter
                  run_once: true
                  delegate_to: localhost
                  - name: Write hostname to the tempfile
                  shell: "echo ansible_hostname >> fcounter.path "
                  delegate_to: localhost

                  # < --------- HERE COMES THE PLAY --------- >

                  - name: Print number of hosts
                  shell: "cat fcounter.path | wc -l"
                  register: counter
                  run_once: true
                  delegate_to: localhost
                  - debug:
                  var: counter.stdout
                  run_once: true


                  gives:



                  ok: [test_01] => 
                  "counter.stdout": "3"

                  PLAY RECAP ****************************************************************
                  test_01 : ok=5 changed=3 unreachable=0 failed=0
                  test_02 : ok=2 changed=1 unreachable=0 failed=0
                  test_03 : ok=2 changed=1 unreachable=0 failed=0





                  share|improve this answer













                  An option would be to create a temporary file and use it for the shared purpose. The play below



                  - hosts: test_jails
                  gather_facts: true
                  tasks:
                  - name: Create tempfile
                  tempfile:
                  state: file
                  register: fcounter
                  run_once: true
                  delegate_to: localhost
                  - name: Write hostname to the tempfile
                  shell: "echo ansible_hostname >> fcounter.path "
                  delegate_to: localhost

                  # < --------- HERE COMES THE PLAY --------- >

                  - name: Print number of hosts
                  shell: "cat fcounter.path | wc -l"
                  register: counter
                  run_once: true
                  delegate_to: localhost
                  - debug:
                  var: counter.stdout
                  run_once: true


                  gives:



                  ok: [test_01] => 
                  "counter.stdout": "3"

                  PLAY RECAP ****************************************************************
                  test_01 : ok=5 changed=3 unreachable=0 failed=0
                  test_02 : ok=2 changed=1 unreachable=0 failed=0
                  test_03 : ok=2 changed=1 unreachable=0 failed=0






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 27 at 0:15









                  Vladimir BotkaVladimir Botka

                  6,3582 gold badges6 silver badges19 bronze badges




                  6,3582 gold badges6 silver badges19 bronze badges


























                      0















                      If I get your exact need, you can get the result without using shell. Please note that using json_query in my example below requires pip install jmespath. If this is not possible, you can most probably achieve the same result using the map filter combining 'extract' and 'attribute='.



                      - name: Check file exists on server
                      stat:
                      path: /tmp/a.txt
                      register: a_file
                      when: whatever_condition

                      - name: Count number of matches
                      debug:
                      msg: >-
                      map('ternary', 1, 0)

                      delegate_to: localhost
                      run_once: true






                      share|improve this answer































                        0















                        If I get your exact need, you can get the result without using shell. Please note that using json_query in my example below requires pip install jmespath. If this is not possible, you can most probably achieve the same result using the map filter combining 'extract' and 'attribute='.



                        - name: Check file exists on server
                        stat:
                        path: /tmp/a.txt
                        register: a_file
                        when: whatever_condition

                        - name: Count number of matches
                        debug:
                        msg: >-
                        map('ternary', 1, 0)

                        delegate_to: localhost
                        run_once: true






                        share|improve this answer





























                          0














                          0










                          0









                          If I get your exact need, you can get the result without using shell. Please note that using json_query in my example below requires pip install jmespath. If this is not possible, you can most probably achieve the same result using the map filter combining 'extract' and 'attribute='.



                          - name: Check file exists on server
                          stat:
                          path: /tmp/a.txt
                          register: a_file
                          when: whatever_condition

                          - name: Count number of matches
                          debug:
                          msg: >-
                          map('ternary', 1, 0)

                          delegate_to: localhost
                          run_once: true






                          share|improve this answer















                          If I get your exact need, you can get the result without using shell. Please note that using json_query in my example below requires pip install jmespath. If this is not possible, you can most probably achieve the same result using the map filter combining 'extract' and 'attribute='.



                          - name: Check file exists on server
                          stat:
                          path: /tmp/a.txt
                          register: a_file
                          when: whatever_condition

                          - name: Count number of matches
                          debug:
                          msg: >-
                          map('ternary', 1, 0)

                          delegate_to: localhost
                          run_once: true







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 27 at 16:42

























                          answered Mar 27 at 16:36









                          ZeitounatorZeitounator

                          2,4331 gold badge4 silver badges19 bronze badges




                          2,4331 gold badge4 silver badges19 bronze badges






























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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%2fstackoverflow.com%2fquestions%2f55361706%2frun-command-on-hosts-and-sum-of-stdout-at-the-end%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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript