2016-04-02 5 views
2

У меня есть консоль amazon с несколькими экземплярами. Все экземпляры имеют меткиAnsible AWS EC2 tags

, например: - имя тега: Дженкинс - тег Имя: Nginx - тег Имя: Artifactory

Я хочу, чтобы запустить анзибль пьес против хозяев, которые помечены, как Nginx.

Я использую динамический инвентарь, но как ограничить, где работает проигрыватель?

Мой сборник пьес выглядит следующим образом:

- name: Provision an EC2 node 
    hosts: local 
    connection: local 
    gather_facts: False 
    vars: 
     instance_type: t2.micro 
     security_group: somegroup 
     #image: ami-a73264ce 
     image: ami-9abea4fb 
     region: us-west-2 
     keypair: ansible_ec2 
    tasks: 
     - name: Step 1 Create a new AWS EC2 Ubuntu Instance 
     local_action: ec2 instance_tags="Name=nginx" group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }} 
     register: ec2 
     - name: Step 2 Add new instance to local host group 
     local_action: lineinfile dest=hosts regexp="{{ item.public_dns_name }}" insertafter="[launched]" line="{{ item.public_dns_name }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem" 
     with_items: ec2.instances 
     - name: Step 3 Wait for SSH to come up delay 180 sec timeout 600 sec 
     local_action: wait_for host={{ item.public_dns_name }} port=22 delay=180 timeout=600 state=started 
     with_items: ec2.instances 

    - name: Step 5 Install nginx steps 
    hosts: launched 
    sudo: yes 
    remote_user: ubuntu 
    gather_facts: True 
    roles: 
     - motd 
     - javaubuntu 
     - apt-get 
     - nginx 

ответ

2

Попробуйте с:

роли/создания инстанции/по умолчанию/main.yml

quantity_instance: 1 
key_pem: "ansible_ec2" 
instance_type: "t2.micro" 
image_base: "ami-9abea4fb" 
sec_group_id: "somegroup" 
tag_Name: "Nginx" 
tag_Service: "reverseproxy" 
aws_region: "us-west-2" 
aws_subnet: "somesubnet" 
root_size: "20" 

--- 
- hosts: 127.0.0.1 
    connection: local 
    gather_facts: False 
    tasks: 
    - name: Adding Vars 
     include_vars: roles/create-instance/defaults/main.yml 

    - name: run instance 
     ec2: 
     key_name: "{{ key_pem }}" 
     instance_type: "{{ instance_type }}" 
     image: "{{ image_base }}" 
     wait: yes 
     group_id: "{{ sec_group_id }}" 
     wait_timeout: 500 
     count: "{{ quantity_instance }}" 
     instance_tags: 
      Name: "{{ tag_Name }}" 
      Service: "{{ tag_Service }}" 
     vpc_subnet_id: "{{ aws_subnet }}" 
     region: "{{ aws_region }}" 
     volumes: 
      - device_name: /dev/xvda 
      volume_size: "{{ root_size }}" 
      delete_on_termination: true 
     assign_public_ip: yes 
     register: ec2 

    - name: Add new instance to host group 
     add_host: hostname={{ item.public_ip }} groupname=launched 
     with_items: ec2.instances 

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

- hosts: launched 
    vars: 
    ansible_ssh_private_key_file: ~/.ssh/ansible_ec2.pem 
    gather_facts: true 
    user: ubuntu 
    become: yes 
    become_method: sudo 
    become_user: root 
    roles: 
    - motd 
    - javaubuntu 
    - apt-get 
    - nginx 

Чтобы избежать добавления в качестве переменной ansible_ssh_private_key_file: ~/.ssh/ansible_ec2.pem, используйте файл .ssh/config и добавьте следующее:

IdentityFile ~/.ssh/ansible_ec2.pem 

Помните, что в файле конфигурации требуется chmod 600.

Если вы не хотите создавать экземпляры снова.

запуск другой сборник пьес, как это:

- hosts: tag_Name_Nginx 
    vars: 
    ansible_ssh_private_key_file: ~/.ssh/ansible_ec2.pem 
    gather_facts: true 
    user: ubuntu 
    become: yes 
    become_method: sudo 
    become_user: root 
    roles: 
    - motd 
    - javaubuntu 
    - apt-get 
    - nginx 

И отмечает, как мы называем конкретные tag_Name_Nginx.

+0

последний сборник пьес вы объявляете переменную ansible_ssh_private_key_file но где вы используете эту переменную? и можете ли вы рассказать мне больше о файле идентификации? Я этого не понимаю. –

+0

Этот var используется для подключения к экземплярам, ​​но лучший способ - создать файл конфигурации ssh. vim .ssh/config, добавьте эту строку «IdentityFile ~/.ssh/ansible_ec2.pem», затем вы можете перейти на серверы без пароля. –

+0

приятно! У вас есть пример в форме плей-листа. Я пытаюсь автоматизировать это. –

2

Все теги становятся группы в динамическом инвентаре, так что вы можете указать тег в параметре «хозяева»

- name: Provision an EC2 node 
    hosts: local 
    connection: local 
    gather_facts: False 
    vars: 
     instance_type: t2.micro 
     security_group: somegroup 
     #image: ami-a73264ce 
     image: ami-9abea4fb 
     region: us-west-2 
     keypair: ansible_ec2 
    tasks: 
    - name: Step 1 Create a new AWS EC2 Ubuntu Instance 
     local_action: ec2 instance_tags="Name=nginx" group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }} 
     register: ec2 
    - name: Step 2 Add new instance to local host group 
     local_action: lineinfile dest=hosts regexp="{{ item.public_dns_name }}" insertafter="[launched]" line="{{ item.public_dns_name }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem" 
     with_items: ec2.instances 
    - name: Step 3 Wait for SSH to come up delay 180 sec timeout 600 sec 
     local_action: wait_for host={{ item.public_dns_name }} port=22 delay=180 timeout=600 state=started 
     with_items: ec2.instances 

- name: Step 5 Install nginx steps 
    hosts: tag_Name_Nginx 
    sudo: yes 
    remote_user: ubuntu 
    gather_facts: True 
    roles: 
    - motd 
    - javaubuntu 
    - apt-get 
    - nginx