Amazon-Web-Services

Ansible 將文件從 S3 下載到 ec2 實例錯誤

  • March 2, 2020

我正在嘗試學習 Ansible。我正在創建一個實例並將文件上傳到其中,我想放入 ec2 實例的文件儲存在 S3 中,但它一直說 c2 內的目標不存在,但它確實存在.

這是失敗的,在此之前的所有其他事情,包括創建實例都可以正常工作:

- name: Deploy war file
       aws_s3:
           bucket: "{{ war_bucket }}"
           object: "{{ war_file }}"
           dest: "{{ war_deploy_path }}/{{ war_file }}"
           mode: get
           overwrite: no
       register: war_downloaded

這就是我聲明變數的方式:

war_file: file.war
war_bucket: ansible-bucket
war_deploy_path: /opt/folder/file.war

這是我得到的錯誤:

[Errno 2] No such file or directory: '/opt/folder/file.war.1f1ccA91'

為什麼要添加這個奇怪的程式碼“1f1cA91”?是不是造成了問題?

更新:我嘗試將目標從“{{ war_deploy_path }}/{{ war_file }}”更改為“{{ war_deploy_path }}”,但同樣的問題仍然存在,現在只有錯誤[Errno 2] No such file or directory: '/opt/folder.Ac2926c3'

***重要更新 2:***好的,所以為了測試,我決定在我的本地機器上創建相同的路徑,令我驚訝的是,這個腳本實際上是在我的本地機器上執行它而不是 ec2 實例大聲笑,所以現在,我如何讓它在 ec2 實例 xD 上執行。

在 ansible 中創建一個主機,然後在同一個 playbook 中處理它是可能的,但需要對清單文件進行一些動態更改並重新讀取 playbook 中的清單。

首先,在您的庫存文件中添加一個佔位符,例如:

[local]
   localhost ansible_connection=local ansible_python_interpreter=python

[new_ones]

其次,在您的劇本中,您將需要兩個部分,一個用於執行本地作業,第二個用於針對您從第一部分創建的主機執行。在第一部分中,您將創建主機,然後將主機 IP 添加到您在上面創建的清單中。然後你會告訴 ansible 用命令重新讀取清單,meta然後等待主機提出pause命令。這是一個例子:

---

- name: Testing Part One
 hosts: local
 become: yes
 tasks:
   - name: create an ec2 instance
     local_action: 
         module: ec2
         aws_secret_key: <redacted>
         aws_access_key: <redacted>
         group_id: sg-1234567
         key_name: my_key
         instance_type: t2.micro
         image: ami-0123456789abcde
         wait: yes
         count: 1
         vpc_subnet_id: subnet-987654321
         assign_public_ip: no
         region: us-east-1
     register: ec2

# This part adds the IP address of the host that was created above to the
#   inventory file

   - name: Add instance to inventory
     local_action:
         module: lineinfile
         path: inv/hosts_default
         regexp: "{{ item.private_ip }}"
         insertafter: "new_ones"
         line: "{{ item.private_ip }}"
     with_items: '{{ ec2.instances }}'

# Have the playbook reread the inventory file
   - meta: refresh_inventory

# Wait for a bit to ensure SSH is enabled
   - pause:
       minutes: 5

然後你在同一個劇本中創建另一個條目來複製你的文件。預設情況下,我沒有在我的主機上安裝 pip,所以補充說以防萬一你在同一條船上:

- name: Testing Part Two
 hosts: new_ones
 become: yes
 tasks:

# Install pip, boto, boto3, and botocore.  You may not need this
   - name: install pip
     easy_install:
       name: pip
       state: latest

   - name: install boto, boto3 and botocore
     pip:
        name: "{{ item }}"
     loop:
       - boto
       - boto3
       - botocore

# Finally we get to what you were trying to do to begin with...

   - name: Deploy war file
     aws_s3:
         aws_secret_key: <redacted>
         aws_access_key: <redacted>
         bucket: "mybucketname"
         object: "blah.txt"
         dest: "/tmp/blah.txt"
         mode: get
         overwrite: no
     register: war_downloaded

如果您仍然感到困惑,這是完整的劇本:

---

- name: Testing Part 1
 hosts: local
 become: yes
 tasks:
   - name: create an ec2 instance
     local_action: 
         module: ec2
         aws_secret_key: <redacted>
         aws_access_key: <redacted>
         group_id: sg-1234567
         key_name: my_key
         instance_type: t2.micro
         image: ami-0123456789abcde
         wait: yes
         count: 1
         vpc_subnet_id: subnet-987654321
         assign_public_ip: no
         region: us-east-1
     register: ec2

   - name: Add instance to inventory
     local_action:
         module: lineinfile
         path: inv/hosts_default
         regexp: "{{ item.private_ip }}"
         insertafter: "new_ones"
         line: "{{ item.private_ip }}"
     with_items: '{{ ec2.instances }}'

   - meta: refresh_inventory

   - pause:
       minutes: 5

- name: Testing Part Two
 hosts: new_ones
 become: yes
 tasks:
   - name: install pip
     easy_install:
       name: pip
       state: latest

   - name: install boto, boto3 and botocore
     pip:
        name: "{{ item }}"
     loop:
       - boto
       - boto3
       - botocore

   - name: Deploy war file
     aws_s3:
         aws_secret_key: <redacted>
         aws_access_key: <redacted>
         bucket: "mybucketname"
         object: "blah.txt"
         dest: "/tmp/blah.txt"
         mode: get
         overwrite: no
     register: war_downloaded

參考

Ansible 元模組

PS我只測試了這個建築一個主機,如果你建造多個你的里程可能會有所不同。

引用自:https://serverfault.com/questions/1005000