ansible pass shell command result to variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.



my idea was to do something like this:



 - name: generate repli-id
shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
register: slave_repli

- debug: var=slave_repli.stdout_lines

- name: rewrite
template: src=templates/root.j2 dest=/root/test.conf


so I get the id generated



TASK [debug] *******************************************************************
task path: /Users/miwi/ansible/roles/test/main.yml:32
ok: [mysqls5slave] =>
"slave_repli.stdout_lines": [
"3698"
]

ok: [mysqls5master] =>
"slave_repli.stdout_lines": [
"3699"
]



my question is now how do I pass it over to my var file



slave_server_id: slave_server_id










share|improve this question
















bumped to the homepage by Community♦ 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















    up vote
    1
    down vote

    favorite












    I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.



    my idea was to do something like this:



     - name: generate repli-id
    shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
    register: slave_repli

    - debug: var=slave_repli.stdout_lines

    - name: rewrite
    template: src=templates/root.j2 dest=/root/test.conf


    so I get the id generated



    TASK [debug] *******************************************************************
    task path: /Users/miwi/ansible/roles/test/main.yml:32
    ok: [mysqls5slave] =>
    "slave_repli.stdout_lines": [
    "3698"
    ]

    ok: [mysqls5master] =>
    "slave_repli.stdout_lines": [
    "3699"
    ]



    my question is now how do I pass it over to my var file



    slave_server_id: slave_server_id










    share|improve this question
















    bumped to the homepage by Community♦ 6 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.



      my idea was to do something like this:



       - name: generate repli-id
      shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
      register: slave_repli

      - debug: var=slave_repli.stdout_lines

      - name: rewrite
      template: src=templates/root.j2 dest=/root/test.conf


      so I get the id generated



      TASK [debug] *******************************************************************
      task path: /Users/miwi/ansible/roles/test/main.yml:32
      ok: [mysqls5slave] =>
      "slave_repli.stdout_lines": [
      "3698"
      ]

      ok: [mysqls5master] =>
      "slave_repli.stdout_lines": [
      "3699"
      ]



      my question is now how do I pass it over to my var file



      slave_server_id: slave_server_id










      share|improve this question















      I stuck a bit with ansible and autogenerated id, I have multiple mysql master - slaves server for that I need to generate a server-id.



      my idea was to do something like this:



       - name: generate repli-id
      shell: hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'
      register: slave_repli

      - debug: var=slave_repli.stdout_lines

      - name: rewrite
      template: src=templates/root.j2 dest=/root/test.conf


      so I get the id generated



      TASK [debug] *******************************************************************
      task path: /Users/miwi/ansible/roles/test/main.yml:32
      ok: [mysqls5slave] =>
      "slave_repli.stdout_lines": [
      "3698"
      ]

      ok: [mysqls5master] =>
      "slave_repli.stdout_lines": [
      "3699"
      ]



      my question is now how do I pass it over to my var file



      slave_server_id: slave_server_id







      ansible






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 17 '16 at 10:10









      Jeff Schaller

      33.8k851113




      33.8k851113










      asked Oct 17 '16 at 9:56









      Andre Lange

      612




      612





      bumped to the homepage by Community♦ 6 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community♦ 6 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:



          mkdir -p /etc/ansible/facts.d
          cat <<EOF >/etc/ansible/facts.d/mysql.fact
          #!/bin/sh
          echo "[mysql]"
          echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
          EOF
          chmod +x /etc/ansible/facts.d/mysql.fact


          Run ansible -m setup your.mysql.ip.address to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id'] defined.



          Say you'ld want ansible to deploy this fact, you may do something like:



          - name: install fact
          copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
          register: fact_installed
          - name: reload facts
          setup: filter=ansible_local
          when: fact_installed is defined and fact_installed.changed == True


          After which, you may include your template, referring to your server_id variable.






          share|improve this answer



























            up vote
            0
            down vote













            You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:



            - local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file





            share|improve this answer



























              up vote
              0
              down vote













              You have two options to use the generated value in your template:



              Option 1: Use slave_repli.stdout_lines in your template:



              # in templates/root.j2:
              ...
              slave_server_id: slave_repli.stdout_lines[0]
              ...


              Option 2: Assign a variable



              In your playbook:



              - name: rewrite
              vars:
              slave_server_id: " slave_repli.stdout_lines[0] "
              template:
              src: templates/root.j2
              dest: /root/test.conf


              In your template:



              # in templates/root.j2:
              ...
              slave_server_id: slave_server_id
              ...


              I prefer the second approach as it's much cleaner.






              share|improve this answer




















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "106"
                ;
                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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f316927%2fansible-pass-shell-command-result-to-variable%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                0
                down vote













                Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:



                mkdir -p /etc/ansible/facts.d
                cat <<EOF >/etc/ansible/facts.d/mysql.fact
                #!/bin/sh
                echo "[mysql]"
                echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
                EOF
                chmod +x /etc/ansible/facts.d/mysql.fact


                Run ansible -m setup your.mysql.ip.address to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id'] defined.



                Say you'ld want ansible to deploy this fact, you may do something like:



                - name: install fact
                copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
                register: fact_installed
                - name: reload facts
                setup: filter=ansible_local
                when: fact_installed is defined and fact_installed.changed == True


                After which, you may include your template, referring to your server_id variable.






                share|improve this answer
























                  up vote
                  0
                  down vote













                  Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:



                  mkdir -p /etc/ansible/facts.d
                  cat <<EOF >/etc/ansible/facts.d/mysql.fact
                  #!/bin/sh
                  echo "[mysql]"
                  echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
                  EOF
                  chmod +x /etc/ansible/facts.d/mysql.fact


                  Run ansible -m setup your.mysql.ip.address to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id'] defined.



                  Say you'ld want ansible to deploy this fact, you may do something like:



                  - name: install fact
                  copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
                  register: fact_installed
                  - name: reload facts
                  setup: filter=ansible_local
                  when: fact_installed is defined and fact_installed.changed == True


                  After which, you may include your template, referring to your server_id variable.






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:



                    mkdir -p /etc/ansible/facts.d
                    cat <<EOF >/etc/ansible/facts.d/mysql.fact
                    #!/bin/sh
                    echo "[mysql]"
                    echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
                    EOF
                    chmod +x /etc/ansible/facts.d/mysql.fact


                    Run ansible -m setup your.mysql.ip.address to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id'] defined.



                    Say you'ld want ansible to deploy this fact, you may do something like:



                    - name: install fact
                    copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
                    register: fact_installed
                    - name: reload facts
                    setup: filter=ansible_local
                    when: fact_installed is defined and fact_installed.changed == True


                    After which, you may include your template, referring to your server_id variable.






                    share|improve this answer












                    Instead of generating your ID from within your playbook, this is where you may want to define a custom fact. On your mysql server, you'ld do something like this:



                    mkdir -p /etc/ansible/facts.d
                    cat <<EOF >/etc/ansible/facts.d/mysql.fact
                    #!/bin/sh
                    echo "[mysql]"
                    echo server_id=`hostname -I | sed -e 's/ +([a-z0-9]+:)+[a-z0-9]+//' | sed -e 's/ /n/' | grep -v '^$' | tail -1 | awk -F. 'print $3 * 256 + $4'`
                    EOF
                    chmod +x /etc/ansible/facts.d/mysql.fact


                    Run ansible -m setup your.mysql.ip.address to make sure your fact is properly executed. You should have some ansible_local['mysql']['mysql']['server_id'] defined.



                    Say you'ld want ansible to deploy this fact, you may do something like:



                    - name: install fact
                    copy: src=myfact dest=/etc/ansible/facts.d/mysql.fact owner=root group=root mode=0755
                    register: fact_installed
                    - name: reload facts
                    setup: filter=ansible_local
                    when: fact_installed is defined and fact_installed.changed == True


                    After which, you may include your template, referring to your server_id variable.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 17 '16 at 1:29









                    SYN

                    1,814414




                    1,814414






















                        up vote
                        0
                        down vote













                        You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:



                        - local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:



                          - local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:



                            - local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file





                            share|improve this answer












                            You could use the copy module in addition with local_action to save the variable to a local file on your control machine and use it later on your next playbook:



                            - local_action: copy content="slave_server_id: slave_repli.stdout_lines[0] " dest=/path/to/var/file






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 5 at 5:50









                            Pedreiro

                            84




                            84




















                                up vote
                                0
                                down vote













                                You have two options to use the generated value in your template:



                                Option 1: Use slave_repli.stdout_lines in your template:



                                # in templates/root.j2:
                                ...
                                slave_server_id: slave_repli.stdout_lines[0]
                                ...


                                Option 2: Assign a variable



                                In your playbook:



                                - name: rewrite
                                vars:
                                slave_server_id: " slave_repli.stdout_lines[0] "
                                template:
                                src: templates/root.j2
                                dest: /root/test.conf


                                In your template:



                                # in templates/root.j2:
                                ...
                                slave_server_id: slave_server_id
                                ...


                                I prefer the second approach as it's much cleaner.






                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  You have two options to use the generated value in your template:



                                  Option 1: Use slave_repli.stdout_lines in your template:



                                  # in templates/root.j2:
                                  ...
                                  slave_server_id: slave_repli.stdout_lines[0]
                                  ...


                                  Option 2: Assign a variable



                                  In your playbook:



                                  - name: rewrite
                                  vars:
                                  slave_server_id: " slave_repli.stdout_lines[0] "
                                  template:
                                  src: templates/root.j2
                                  dest: /root/test.conf


                                  In your template:



                                  # in templates/root.j2:
                                  ...
                                  slave_server_id: slave_server_id
                                  ...


                                  I prefer the second approach as it's much cleaner.






                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    You have two options to use the generated value in your template:



                                    Option 1: Use slave_repli.stdout_lines in your template:



                                    # in templates/root.j2:
                                    ...
                                    slave_server_id: slave_repli.stdout_lines[0]
                                    ...


                                    Option 2: Assign a variable



                                    In your playbook:



                                    - name: rewrite
                                    vars:
                                    slave_server_id: " slave_repli.stdout_lines[0] "
                                    template:
                                    src: templates/root.j2
                                    dest: /root/test.conf


                                    In your template:



                                    # in templates/root.j2:
                                    ...
                                    slave_server_id: slave_server_id
                                    ...


                                    I prefer the second approach as it's much cleaner.






                                    share|improve this answer












                                    You have two options to use the generated value in your template:



                                    Option 1: Use slave_repli.stdout_lines in your template:



                                    # in templates/root.j2:
                                    ...
                                    slave_server_id: slave_repli.stdout_lines[0]
                                    ...


                                    Option 2: Assign a variable



                                    In your playbook:



                                    - name: rewrite
                                    vars:
                                    slave_server_id: " slave_repli.stdout_lines[0] "
                                    template:
                                    src: templates/root.j2
                                    dest: /root/test.conf


                                    In your template:



                                    # in templates/root.j2:
                                    ...
                                    slave_server_id: slave_server_id
                                    ...


                                    I prefer the second approach as it's much cleaner.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 11 at 16:22









                                    mhutter

                                    557210




                                    557210



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f316927%2fansible-pass-shell-command-result-to-variable%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay