[英]Unable to run Python Script from within an Ansible Playbook
我正在嘗試編寫一個 ansible playbook 來抓取網站,然后將其內容存儲到 aws s3 存儲桶下的靜態文件中。 這是爬蟲代碼:
"""
Handling pages with the Next button
"""
import sys
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup
url = "https://xyz.co.uk/"
file_name = "web_content.txt"
while True:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
raw_html = soup.prettify()
file = open(file_name, 'wb')
print('Collecting the website contents')
file.write(raw_html.encode())
file.close()
print('Saved to %s' % file_name)
#print(type(raw_html))
# Finding next page
next_page_element = soup.select_one('li.next > a')
if next_page_element:
next_page_url = next_page_element.get('href')
url = urljoin(url, next_page_url)
else:
break
這是我的 ansible-playbook:
---
- name: create s3 bucket and upload static website content into it
hosts: localhost
connection: local
tasks:
- name: create a s3 bucket
amazon.aws.aws_s3:
bucket: testbucket393647914679149
region: ap-south-1
mode: create
- name: create a folder in the bucket
amazon.aws.aws_s3:
bucket: testbucket393647914679149
object: /my/directory/path
mode: create
- name: Upgrade pip
pip:
name: pip
version: 21.1.3
- name: install virtualenv via pip
pip:
requirements: /root/ansible/requirements.txt
virtualenv: /root/ansible/myvenv
virtualenv_python: python3.6
environment:
PATH: "{{ ansible_env.PATH }}:{{ ansible_user_dir }}/.local/bin"
- name: Run script to crawl the website
script: /root/ansible/beautiful_crawl.py
- name: copy file into bucket folder
amazon.aws.aws_s3:
bucket: testbucket393647914679149
object: /my/directory/path/web_content.text
src: web_content.text
mode: put
問題是當我運行它時,它在任務名稱之前運行良好:通過 pip 安裝 virtualenv然后在執行任務名稱時拋出以下錯誤:運行腳本來抓取網站:
致命:[本地主機]:失敗! => {“已更改”:true,“msg”:“非零返回碼”,“rc”:2,“stderr”:“/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-9798 3643645466 /beautiful_crawl.py: line 1: import: command not found\\n/root/.ansible /tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py: line 2: from/root command not found /.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 3 行:導入:未找到命令\\n/roo t/.ansible/tmp/ansible-tmp-16864604604604846045465465465465465465465465465546546554655465465465465465465463643645463章電話wl.py: line 4: from: command not found\\n/root/.ansible/tmp/ansible-tmp-162513770 0.8854306-13026-97983643645466/beautiful_crawl.py: line 6: url: command not foun d /.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beauti ful_crawl.py:第 7 行:文件名:命令未找到\\n/root/.ansible/tmp/ansible-t mp-16-164636564604604634604364605 beautiful_crawl.py:第 10 行:意外標記附近的語法錯誤
('\\n/root/.ansible/tmp/ansible-tmp-1625137700.885430 6-13026-97983643645466/beautiful_crawl.py: line 10:
('\\n/root/.ansible/tmp/ansible-tmp-1625137700.885430 6-13026-97983643645466/beautiful_crawl.py: line 10:
response = requests.get (url)'\\n", "stderr_lines": ["/root/.ansible/tmp/ansible-2301638 1 3026-97983643645466/beautiful_crawl.py:第 1 行:導入:未找到命令”,“/ro ot/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-9798464364 來自:alinepy:commandi.未找到", "/root/.ansible/tmp/ansible-tmp-162513 7700.8854306-13026-97983643645466/beautiful_crawl.py:第 3 行:導入:找不到命令","/root/.ansible/tmp/ansible tmp-1625137700.8854306-13026-9798364364546 6/beautiful_crawl.py:第 4 行:來自:未找到命令”,“/root/.ansible/tmp/ansi ble-tmp-1625137436454637700.666137700.66137700.66137700.86437700.86437700.86137700.86137700.86137377700.8854306-1625137700.8854306-13026-9798364364546 6/beautiful_crawl.py:第 4 行:來自:未找到命令” url: 命令未找到", "/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97 983643645466/beautiful_crawl.py: line 7: file_name: command not found", "/root/. ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl。 py: 第 10 行:意外標記附近的語法錯誤('", "/root/.ansible/tmp/ansibl e-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py: line 10:
響應 = requests.get(url)' "], "stdout": "", "stdout_lines": []}
我在這里做錯了什么?
您有多個問題。
檢查文檔。
第一: script
模塊默認運行bash
腳本,而不是python
腳本。 如果要運行python腳本,則需要在腳本的第一行添加像#!/usr/bin/env python3
這樣的shebang或使用executable
參數。
否 2 :您創建了一個venv
,所以我假設您想在該venv
運行腳本。 你不能用script
模塊開箱即用,所以你需要解決這個問題。
這應該對你有用(你不需要shebang,因為你告訴腳本模塊使用executable
參數在venv中使用python運行它):
- name: Run script to crawl the website
script: /root/ansible/beautiful_crawl.py
executable: /root/ansible/myvenv/bin/python
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.