Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用ansible启动ec2实例时出错_Python_Ansible_Ansible Playbook - Fatal编程技术网

Python 使用ansible启动ec2实例时出错

Python 使用ansible启动ec2实例时出错,python,ansible,ansible-playbook,Python,Ansible,Ansible Playbook,我正试图从ansible开始,特别是使用ansible剧本来部署ec2实例,但我一直遇到一个错误 我遵循了在该线程中找到的代码: 我已经在我自己的细节中替换了以下内容 主机文件: [local] localhost [webserver] 创建_instance.yml --- - name: Provision an EC2 Instance hosts: local connection: local gather_facts: False tags:

我正试图从ansible开始,特别是使用ansible剧本来部署ec2实例,但我一直遇到一个错误

我遵循了在该线程中找到的代码:

我已经在我自己的细节中替换了以下内容

主机文件:

[local]
localhost

[webserver]
创建_instance.yml

---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t2.micro
      security_group: webserver # Change the security group name here
      image: ami-f95ef58a # Change the AMI, from which you want to launch     the server
      region: eu-west-1 # Change the Region
      keypair: MyKeyPair # Change the keypair name
      count: 1

      # Task that will be used to Launch/Create an EC2 Instance
        tasks:

    - name: Create a security group
      local_action: 
      module: ec2_group
      name: "{{ security_group }}"
      description: Security Group for webserver Servers
      region: "{{ region }}"
      rules:
        - proto: tcp
          type: ssh
          from_port: 22
          to_port: 22
          cidr_ip: 0.0.0.0/0
        - proto: tcp
          from_port: 80
          to_port: 80
          cidr_ip: 0.0.0.0/0
      rules_egress:
        - proto: all
          type: all
          cidr_ip: 0.0.0.0/0


  - name: Launch the new EC2 Instance
    local_action: ec2 
                  group={{ security_group }} 
                  instance_type={{ instance_type}} 
                  image={{ image }} 
                  wait=true 
                  region={{ region }} 
                  keypair={{ keypair }}
                  count={{count}}
    register: ec2

  - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile 
                  dest="./hosts" 
                  regexp={{ item.public_ip }} 
                  insertafter="[webserver]" line={{ item.public_ip }}
    with_items: ec2.instances


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

  - name: Add tag to Instance(s)
    local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
    with_items: ec2.instances
    args:
      tags:
        Name: webserver    
然后,我为AWS密钥创建环境变量,如下所示:

export AWS_ACCESS_KEY=my aws key
export AWS_SECRET_KEY=my aws secret key
当我用 sudo ansible playbook-i主机创建_instance.yml 我得到以下错误:

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

TASK: [make one instance] *****************************************************
failed: [localhost] => {"failed": true}
msg: No handler was ready to authenticate. 1 handlers were checked.     ['HmacAuthV4Handler'] Check your credentials

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/home/ubuntu/create_instance.retry

localhost                  : ok=0    changed=0    unreachable=0    failed=1

有人能告诉我哪里出了问题吗?

当您的ansible主机无法连接到您的AWS帐户时,会出现此错误。为此,您需要确保正确设置了访问密钥,并且有足够的权限创建实例

Ansible使用python并选择python目录。因此,请确保您使用pip安装了awscli,而不是安装awscli。使用
sudo pip安装awscli

在文件~/.aws/credentials中指定您的访问密钥

还要确保安装了更新版本的boto和python。
参考这个。这里很好地介绍了配置密钥的所有方法。

不要使用sudo。根用户(大概)没有加载您的环境变量感谢您的建议,但仍然没有运气。为了确认我正确设置了密钥对,我的yml文件中的“keypair”变量是否应该设置为已上载到AWS的名称作为我的密钥对?这里我指的是我用来通过AWS控制台创建新ec2实例的密钥对的名称?这就是我目前正在使用的,但我只是想检查一下它是否正确。它还没有发展到那个程度。它失败是因为您没有正确设置aws A连接变量。如果您使用sudo,那么它不会加载您在当前shell中导出的变量。另一种方法是在剧本或清单中指定
aws\u访问密钥
aws\u秘密密钥
vars。谢谢。我遵循了你的步骤,但我仍然有一个问题。我得到的错误如下…身份验证或权限失败。在某些情况下,您可能已经能够进行身份验证,但对远程目录没有权限。考虑将ANSILBE.CFG中的远程临时路径更改为“/tMP”中的路径。失败的命令是:mkdir-p$HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080&&chmod a+rx$HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080&&echo$HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080,退出,结果为1Oh。这意味着您没有在ansible框上创建目录的权限。能否确保在$HOME上创建的/.ansible/tmp/目录的写入权限和所有权?你在用同一个用户运行这个ansible playbook?