Ansible to create aws security group and add to instance





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am creating an EC2 instance. My creation flow is,




  1. Create Ami


  2. Create security group


  3. Create new Instance from AMI and add to security group



My new security group should only contain private ip of a new instance with appropriate ports open



My concern is, How can I create security group and add EC2 Instance private IP if that instance is yet to be created



Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?



If you have any suggestions please let me know



Thanks










share|improve this question





























    0















    I am creating an EC2 instance. My creation flow is,




    1. Create Ami


    2. Create security group


    3. Create new Instance from AMI and add to security group



    My new security group should only contain private ip of a new instance with appropriate ports open



    My concern is, How can I create security group and add EC2 Instance private IP if that instance is yet to be created



    Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?



    If you have any suggestions please let me know



    Thanks










    share|improve this question

























      0












      0








      0








      I am creating an EC2 instance. My creation flow is,




      1. Create Ami


      2. Create security group


      3. Create new Instance from AMI and add to security group



      My new security group should only contain private ip of a new instance with appropriate ports open



      My concern is, How can I create security group and add EC2 Instance private IP if that instance is yet to be created



      Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?



      If you have any suggestions please let me know



      Thanks










      share|improve this question














      I am creating an EC2 instance. My creation flow is,




      1. Create Ami


      2. Create security group


      3. Create new Instance from AMI and add to security group



      My new security group should only contain private ip of a new instance with appropriate ports open



      My concern is, How can I create security group and add EC2 Instance private IP if that instance is yet to be created



      Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?



      If you have any suggestions please let me know



      Thanks







      amazon-web-services ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 3:24









      DevendraDevendra

      337




      337
























          1 Answer
          1






          active

          oldest

          votes


















          1















          Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?




          Sure, that's exactly what ec2: (or the newer ec2_instance:) will do when given the wait: yes argument. By using register: ec2_result you can then use ec2_result.instances[0].private_ip_address (as seen here) inside the ec2_group: module to define the rules for the SG.






          share|improve this answer
























          • Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

            – Devendra
            Nov 25 '18 at 5:56








          • 1





            Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

            – Matthew L Daniel
            Nov 25 '18 at 18:16












          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%2f53464372%2fansible-to-create-aws-security-group-and-add-to-instance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?




          Sure, that's exactly what ec2: (or the newer ec2_instance:) will do when given the wait: yes argument. By using register: ec2_result you can then use ec2_result.instances[0].private_ip_address (as seen here) inside the ec2_group: module to define the rules for the SG.






          share|improve this answer
























          • Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

            – Devendra
            Nov 25 '18 at 5:56








          • 1





            Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

            – Matthew L Daniel
            Nov 25 '18 at 18:16
















          1















          Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?




          Sure, that's exactly what ec2: (or the newer ec2_instance:) will do when given the wait: yes argument. By using register: ec2_result you can then use ec2_result.instances[0].private_ip_address (as seen here) inside the ec2_group: module to define the rules for the SG.






          share|improve this answer
























          • Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

            – Devendra
            Nov 25 '18 at 5:56








          • 1





            Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

            – Matthew L Daniel
            Nov 25 '18 at 18:16














          1












          1








          1








          Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?




          Sure, that's exactly what ec2: (or the newer ec2_instance:) will do when given the wait: yes argument. By using register: ec2_result you can then use ec2_result.instances[0].private_ip_address (as seen here) inside the ec2_group: module to define the rules for the SG.






          share|improve this answer














          Is there any way that I can wait for the Instance creation task first and after it is done I fetch the private IP and use that in above task which is "Security group Creation"?




          Sure, that's exactly what ec2: (or the newer ec2_instance:) will do when given the wait: yes argument. By using register: ec2_result you can then use ec2_result.instances[0].private_ip_address (as seen here) inside the ec2_group: module to define the rules for the SG.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 5:48









          Matthew L DanielMatthew L Daniel

          9,78112729




          9,78112729













          • Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

            – Devendra
            Nov 25 '18 at 5:56








          • 1





            Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

            – Matthew L Daniel
            Nov 25 '18 at 18:16



















          • Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

            – Devendra
            Nov 25 '18 at 5:56








          • 1





            Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

            – Matthew L Daniel
            Nov 25 '18 at 18:16

















          Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

          – Devendra
          Nov 25 '18 at 5:56







          Thanks for the response but I need to first create an instance, fetch private ip and add it to SG group which can be done by your answer. whereas I also need to attach that SG to an AWS Instance, In short my flow is, 1. Create instance 2. Create SG 3. Add Private Ip of Instance to SG 4. Add SG to that Instance again

          – Devendra
          Nov 25 '18 at 5:56






          1




          1





          Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

          – Matthew L Daniel
          Nov 25 '18 at 18:16





          Ok, great; I am about 90% certain that one - ec2: wait=yes followed by - ec2_group: followed by a second - ec2: instance_ids: ... security_groups: {{ existing_sg + new_sg_id }} will do what you described

          – Matthew L Daniel
          Nov 25 '18 at 18:16




















          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%2f53464372%2fansible-to-create-aws-security-group-and-add-to-instance%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini