【问题标题】:Ansible: How to specify an ssh key for a single task?Ansible:如何为单个任务指定 ssh 密钥?
【发布时间】: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


【解决方案1】:

是的,我同意@techraf。但是您发布的问题的答案是,您必须为您配置的新实例动态更改库存,然后在该新主机上运行远程 ansible 播放。因此,您可以将其添加到您的第一个游戏的结尾:

    - local_action:
        module: add_host
        hostname: newhost
        ansible_host: "{{ ec2['tagged_instances'][0]['public_ip'] }}"
        ansible_user: centos
        ansible_ssh_private_key_file: /path/to/keyfile

###### New play
- name: Configure my new instance!
  hosts: newhost
  tasks:
    # THE TASKS I NEED HELP ON
    - name: Copy files over to ec2 instance
      copy: src={{ item }} dest=/home/centos/ mode=600
      with_fileglob:
        - my-files/*
    # Use the yum module here instead, much easier
    - name: run commands
      shell: "{{ item }}"
      with_items:
        - "sudo yum update -y"
        - "sudo yum install nmap ruby"
      ignore_errors: true 

编辑:添加,您始终可以使用以下方法设置 ssh 主机密钥:

- set_fact: ansible_ssh_private_key_file=/path/to/keyfile

需要注意的是,上面的 set_fact 只会更改当前运行主机的 ssh 私钥文件(例如,上面示例中的 localhost )。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2014-11-29
    • 2015-08-31
    相关资源
    最近更新 更多