2014-12-05 16 views
2

这可能很明显,但是如何针对Ansible中的一组服务器执行操作(这与EC2插件有关)?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 

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

回答

6

为了针对刚刚新创建的服务器上运行,我用的是临时组的名称,并完成类似下面通过在相同的剧本使用第二打法:

- 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 

或者,如果你一直标记了所有服务器(如果您还必须区分生产和开发,并且使用多个标签;并且您还使用ec2.py作为动态库存脚本;并且您在第二次剧本运行中针对所有服务器运行此操作,则可以轻松做到以下几点:

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

我个人使用模式标签(tag_mode_production vs tag_mode_development),并强制Ansible仅在特定类型(开发版)中的特定类型的服务器上运行(在您的情况下为Name = cassandra)。这看起来像下面这样:

- hosts: tag_Name_cassandra:&tag_mode_development 

只要确保你正确指定标签名称和值 - 它是区分大小写...

+0

哇,大的谢谢,谢谢。顺便说一句,你如何设置tag_mode? – mtyson 2014-12-06 00:47:29

+1

我个人在ec2模块操作期间执行此操作,但您也可以在ec2_tag操作期间执行此操作,只需在args/tags部分中添加另一个'key:value'对(如“模式:开发”) - 文档将其作为现在我看一个例子:http://docs.ansible.com/ec2_tag_module.html – PhillipHolmes 2014-12-08 16:37:16

0

请使用下面的剧本模式,以在单一执行两种操作playbook(指的是一个ec2实例,并在其上执行某些任务)。

这里是工作的剧本,执行以下任务,这个剧本假设您拥有的主机在同一目录中的文件,其中正在运行的剧本:

--- 
    - 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 

你的hosts文件应该被看像这样:

[local] 
localhost 

[cassandra] 

现在你可以运行这个剧本是这样的:

ansible-playbook -i hosts ec2_launch.yml