【发布时间】:2017-02-27 02:36:29
【问题描述】:
我有一个创建 ec2 实例的剧本,将一些文件复制到该实例,然后在该实例上运行一些 shell 命令。
问题是我希望能够指定 ssh 密钥 ansible 用于我正在运行的复制和 shell 任务,并确保它不会尝试将此密钥用于在本地主机上运行的其他任务。这是我的剧本:
---
- hosts: localhost
connection: local
gather_facts: false
vars:
# CentOS 7 x86_64 Devel AtomicHost EBS HVM 20150306_01 (ami-07e6c437)
# for us-west-2
- ami: 'ami-07e6c437'
- key_pair: 'my-key'
tasks:
- name: Create a centos server
ec2:
region: 'us-west-2'
key_name: '{{ key_pair }}'
group: default
instance_type: t2.micro
image: '{{ ami }}'
wait: true
exact_count: 1
count_tag:
Name: my-instance
instance_tags:
Name: my-instance
register: ec2
# shows the json data for the instances created
- name: Show ec2 instance json data
debug:
msg: "{{ ec2['tagged_instances'] }}"
- name: Wait for SSH to come up
wait_for: host={{ ec2['tagged_instances'][0]['public_ip'] }} port=22 delay=1 timeout=480 state=started
- name: Accept new ssh fingerprints
shell: ssh-keyscan -H "{{ ec2['tagged_instances'][0]['public_ip'] }}" >> ~/.ssh/known_hosts
# THE TASKS I NEED HELP ON
- name: Copy files over to ec2 instance
remote_user: centos
copy: src={{ item }} dest=/home/centos/ mode=600
with_fileglob:
- my-files/*
delegate_to: "{{ ec2['tagged_instances'][0]['public_ip'] }}"
# THE TASKS I NEED HELP ON
- name: run commands
remote_user: centos
shell: "{{ item }}"
delegate_to: "{{ ec2['tagged_instances'][0]['public_ip'] }}"
with_items:
- "sudo yum update -y"
- "sudo yum install nmap ruby"
ignore_errors: true
【问题讨论】:
-
你为什么使用
delegate_to?我假设你想在新启动的机器上运行任务。 -
是的,我想将文件复制到新创建的实例上并在其上运行 shell 命令
-
为什么需要使用不同的 SSH 密钥?
-
对不起,想要会是一个比需要更好的词。另一个主要原因是我工作的环境有一个非常复杂的 ssh 密钥管理系统,并且可以灵活地选择我在某些任务中使用的 ssh 密钥。
标签: ssh amazon-ec2 ansible