如何通过Ansible执行`aws ec2 describe-instances`?

时间:2017-06-24 11:48:07

标签: amazon-web-services ansible

我想使用aws ec2 describe-instances > myFile

通过Ansible执行ansible-playbook playbook.yml
---
# playbook.yml
- hosts: localhost
  connection: local
  tasks:
    - name: Testing 
      command: aws ec2 describe-instances > myFile

但是,执行此剧本时出现以下错误:

PLAY [localhost] **************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
ok: [localhost]

TASK [Fetching IPs of ec2 instances ...] **************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["aws", "ec2", "describe-instances", ">", "myFile"], "delta": "0:00:00.563284", "end": "2017-06-24 19:39:10.782995", "failed": true, "rc": 255, "start": "2017-06-24 19:39:10.219711", "stderr": "usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]\nTo see help text, you can run:\n\n  aws help\n  aws <command> help\n  aws <command> <subcommand> help\n\nUnknown options: myFile, >", "stderr_lines": ["usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]", "To see help text, you can run:", "", "  aws help", "  aws <command> help", "  aws <command> <subcommand> help", "", "Unknown options: myFile, >"], "stdout": "", "stdout_lines": []}

如果我在本地计算机上运行aws ec2 describe-instances > myFile,它可以正常运行。

那么剧本中的命令出了什么问题呢?

2 个答案:

答案 0 :(得分:0)

您无法在command模块中使用I / O重定向或管道 - 它从Python调用可执行文件并将所有参数直接传递给它,如错误消息["aws", "ec2", "describe-instances", ">", "myFile"]中所示。 >不是aws命令的正确参数。

当您通过shell > myFile运行相同的命令时,由shell解释,而不是传递给命令。

在Ansible中,您应该使用shell module代替。

答案 1 :(得分:0)

根据您尝试实现的目标,您应该考虑使用AWS EC2 dynamic inventoryec2_remote_facts module而不是使用AWS CLI。