amazon web services - Ansible EC2 - Perform operation on set of instances -
this obvious, how execute operation against set of servers in ansible (this ec2 plugin)?
i can create instances:
--- - 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
and can put instances tag:
- name: add tag instances local_action: ec2_tag resource={{ item.id }} region=us-west-1 state=present with_items: cass_ec2.instances args: tags: name: cassandra
now, let's want run operation on each server:
# not work - runs command on localhost - name: test - touch file file: path=/test.txt state=touch with_items: cass_ec2.instances
how run command against remote instances created?
for running against newly created servers, use temporary group name , following using second play in same playbook:
- hosts: localhost tasks: - name: run ec2 create server code here ... register: cass_ec2 - name: add host inventory add_host: name={{ item.private_ip }} groups=newinstances with_items: cas_ec2.instances - hosts: newinstances tasks: - name: fun stuff on new instances here
alternatively if have consistently tagged servers (and multiple tags if have differentiate between production , development; , using ec2.py dynamic inventory script; , running against servers in second playbook run, can following:
- hosts: tag_name_cassandra tasks: - name: run cassandra specific tasks here
personally use mode tag (tag_mode_production vs tag_mode_development) in above , force ansible run on servers of specific type (in case name=cassandra) in specific mode (development). looks following:
- hosts: tag_name_cassandra:&tag_mode_development
just make sure specify tag name , value correctly - case sensitive...
Comments
Post a Comment