mty*_*son 3 amazon-ec2 amazon-web-services 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
instance_type: t2.micro
image: ami-4b6f650e
count: 1
wait: yes
register: cass_ec2
Run Code Online (Sandbox Code Playgroud)
我可以将实例放入标签中:
- 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
Run Code Online (Sandbox Code Playgroud)
现在,假设我想在每台服务器上运行一个操作:
# 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
Run Code Online (Sandbox Code Playgroud)
如何针对刚创建的远程实例运行命令?
为了仅针对新创建的服务器运行,我使用临时组名称并通过在同一个playbook中使用第二个play来执行以下操作:
- 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
Run Code Online (Sandbox Code Playgroud)
或者,如果您始终标记所有服务器(如果您还必须区分生产和开发,则使用多个标签;并且您还使用ec2.py作为动态库存脚本;并且您正在针对所有服务器运行此服务器)第二个剧本运行,然后您可以轻松地执行以下操作:
- hosts: tag_Name_cassandra
tasks:
- name: run your cassandra specific tasks here
Run Code Online (Sandbox Code Playgroud)
我个人也在上面使用模式标记(tag_mode_production vs tag_mode_development)并强制Ansible仅在特定模式(开发)中运行特定类型的服务器(在您的情况下为Name = cassandra).这看起来如下:
- hosts: tag_Name_cassandra:&tag_mode_development
Run Code Online (Sandbox Code Playgroud)
只需确保正确指定标记名称和值 - 它区分大小写......
归档时间: |
|
查看次数: |
3655 次 |
最近记录: |