Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services Ansible EC2-对实例集执行操作_Amazon Web Services_Amazon Ec2_Ansible - Fatal编程技术网

Amazon web services Ansible EC2-对实例集执行操作

Amazon web services Ansible EC2-对实例集执行操作,amazon-web-services,amazon-ec2,ansible,Amazon Web Services,Amazon Ec2,Ansible,这可能很明显,但是如何对Ansible中的一组服务器执行操作(这是EC2插件) 我可以创建我的实例: --- - hosts: 127.0.0.1 connection: local - name: Launch instances local_action: module: ec2 region: us-west-1 group: cassandra keypair: cassandra inst

这可能很明显,但是如何对Ansible中的一组服务器执行操作(这是EC2插件)

我可以创建我的实例:

---
- hosts: 127.0.0.1
  connection: local
 - name: Launch instances
      local_action:
        module: ec2
        region: us-west-1
        group: cassandra
        keypair: cassandra
        instance_type: t2.micro
        image: ami-4b6f650e
        count: 1
        wait: yes
      register: cass_ec2
我可以将实例放入标记中:

   - name: Add tag to instances
      local_action: ec2_tag resource={{ item.id }} region=us-west-1 state=present
      with_items: cass_ec2.instances
      args:
        tags:
          Name: cassandra
现在,假设我想在每台服务器上运行一个操作:

# This does not work - It runs the command on localhost
- name: TEST - touch file
  file: path=/test.txt state=touch
  with_items: cass_ec2.instances

如何对刚刚创建的远程实例运行该命令

为了只针对新创建的服务器运行,我使用了一个临时组名,并通过在同一剧本中使用第二个剧本来执行如下操作:

- hosts: localhost
  tasks:
    - name: run your ec2 create a server code here
      ...
      register: cass_ec2

    - name: add host to inventory
      add_host: name={{ item.private_ip }} groups=newinstances
      with_items: cas_ec2.instances

- hosts: newinstances
  tasks:
    - name: do some fun stuff on the new instances here
---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t1.micro
      security_group: cassandra
      image: ami-4b6f650e
      region: us-west-1
      keypair: cassandra
      count: 1

    # Task that will be used to Launch/Create an EC2 Instance
    tasks:

      - name: Launch the new EC2 Instance
        local_action: ec2 
                      group={{ security_group }} 
                      instance_type={{ instance_type}} 
                      image={{ image }} 
                      wait=true 
                      region={{ region }} 
                      keypair={{ keypair }}
                      count={{count}}
        register: ec2

      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
        local_action: lineinfile 
                      dest="./hosts" 
                      regexp={{ item.public_ip }} 
                      insertafter="[cassandra]" line={{ item.public_ip }}
        with_items: ec2.instances


      - name: Wait for SSH to come up
        local_action: wait_for 
                      host={{ item.public_ip }} 
                      port=22 
                      state=started
        with_items: ec2.instances

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
        with_items: ec2.instances
        args:
          tags:
            Name: cassandra

      - name: SSH to the EC2 Instance(s)
        add_host: hostname={{ item.public_ip }} groupname=cassandra
        with_items: ec2.instances

  - name: Install these things on Newly created EC2 Instance(s)
    hosts: cassandra
    sudo: True 
    remote_user: ubuntu # Please change the username here,like root or ec2-user, as I am supposing that you are lauching ubuntu instance
    gather_facts: True
    # Run these tasks  
    tasks:
      - name: TEST - touch file
        file: path=/test.txt state=touch
或者,如果您始终标记所有服务器(如果您还必须区分生产和开发,并使用ec2.py作为动态资源清册脚本,并且在第二次playbook运行中针对所有服务器运行此脚本,那么您可以轻松执行以下操作:

- hosts: tag_Name_cassandra
  tasks:
    - name: run your cassandra specific tasks here
- hosts: tag_Name_cassandra:&tag_mode_development
就我个人而言,我在上面也使用了一个模式标签(tag_mode_production vs tag_mode_development),并强制Ansible在特定模式(development)下仅在特定类型的服务器上运行(在您的案例中,名称为cassandra)。如下所示:

- hosts: tag_Name_cassandra
  tasks:
    - name: run your cassandra specific tasks here
- hosts: tag_Name_cassandra:&tag_mode_development

只需确保正确指定标记名和值-它区分大小写…

请使用以下playbook模式在单个playbook中执行这两个操作(意味着启动ec2实例并在其上执行特定任务)

这是正在工作的playbook,它执行以下任务,此playbook假设您在运行playbook的同一目录中有hosts文件:

- hosts: localhost
  tasks:
    - name: run your ec2 create a server code here
      ...
      register: cass_ec2

    - name: add host to inventory
      add_host: name={{ item.private_ip }} groups=newinstances
      with_items: cas_ec2.instances

- hosts: newinstances
  tasks:
    - name: do some fun stuff on the new instances here
---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t1.micro
      security_group: cassandra
      image: ami-4b6f650e
      region: us-west-1
      keypair: cassandra
      count: 1

    # Task that will be used to Launch/Create an EC2 Instance
    tasks:

      - name: Launch the new EC2 Instance
        local_action: ec2 
                      group={{ security_group }} 
                      instance_type={{ instance_type}} 
                      image={{ image }} 
                      wait=true 
                      region={{ region }} 
                      keypair={{ keypair }}
                      count={{count}}
        register: ec2

      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
        local_action: lineinfile 
                      dest="./hosts" 
                      regexp={{ item.public_ip }} 
                      insertafter="[cassandra]" line={{ item.public_ip }}
        with_items: ec2.instances


      - name: Wait for SSH to come up
        local_action: wait_for 
                      host={{ item.public_ip }} 
                      port=22 
                      state=started
        with_items: ec2.instances

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
        with_items: ec2.instances
        args:
          tags:
            Name: cassandra

      - name: SSH to the EC2 Instance(s)
        add_host: hostname={{ item.public_ip }} groupname=cassandra
        with_items: ec2.instances

  - name: Install these things on Newly created EC2 Instance(s)
    hosts: cassandra
    sudo: True 
    remote_user: ubuntu # Please change the username here,like root or ec2-user, as I am supposing that you are lauching ubuntu instance
    gather_facts: True
    # Run these tasks  
    tasks:
      - name: TEST - touch file
        file: path=/test.txt state=touch
您的主机文件应如下所示:

[local]
localhost

[cassandra]
ansible-playbook -i hosts ec2_launch.yml
现在,您可以按如下方式运行此剧本:

[local]
localhost

[cassandra]
ansible-playbook -i hosts ec2_launch.yml

哇,回答得很好,谢谢。顺便说一句,你是如何设置标记模式的?我个人在ec2模块操作期间会这样做,但你也可以在ec2标记操作期间这样做,只需在args/tags部分添加另一个“key:value”对,如“mode:Development”——现在我看到的文档中也有这样的例子: