这可能很明显,但是如何对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
如何针对刚创建的远程实例运行命令?
答案 0 :(得分:7)
为了仅针对新创建的服务器运行,我使用一个临时组名,并通过在同一个剧本中使用第二个游戏来执行以下操作:
- 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
请确保正确指定标记名称和值 - 它区分大小写......
答案 1 :(得分:1)
请使用以下playbook模式在同一时间在单个playbook中执行这两个操作(意味着很多ec2实例并在其上执行某些任务)。
这是执行以下任务的工作剧本,这个剧本假设您在运行剧本的同一目录中有hosts文件:
---
- 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