无法从 Ansible Playbook 中运行 Python 脚本
Posted
技术标签:
【中文标题】无法从 Ansible Playbook 中运行 Python 脚本【英文标题】:Unable to run Python Script from within an Ansible Playbook 【发布时间】:2021-09-13 11:22:03 【问题描述】:我正在尝试编写 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
问题是当我运行它时,它运行良好,直到任务 name: install virtualenv via pip 然后在执行任务时抛出以下错误 name: Run script to crawl the website:
致命:[本地主机]:失败! => “更改”: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: command not found\n/root /.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 3 行:导入:找不到命令\n/roo t/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_cra wl.py:第 4 行:来自:找不到命令\n/root/.ansible/tmp/ansible-tmp-162513770 0.8854306-13026-9798346 /beautiful_crawl.py:第 6 行:url:找不到命令 d\n/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 7 行:file_name:找不到命令\n/根/.ansible/tmp/ansible-t mp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 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-tmp-1625137700.8854306-1 3026-97983643645466/beautiful_crawl.py:第 1 行:导入:找不到命令”,“/ro ot/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-979836436456 : 第 2 行:来回m:找不到命令”,“/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-1625137700.8854306-13026-979836_crawl4566第 6 行:url:找不到命令”、“/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13 026-97 983643645466/beautiful_crawl.py:第 7 行:文件名:找不到命令”、“/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:
response = requests.get(url)'"], "stdout": "", "stdout_lines": []
我在这里做错了什么?
【问题讨论】:
【参考方案1】:您有多个问题。 检查documentation。
没有。 1: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
【讨论】:
现在,我从需求文件本身安装库时遇到以下错误:“stdout:要求已经满足” 当您在该机器(在 venv 中)手动运行脚本时,您会得到这个吗? 我只是在想,我真的需要在虚拟环境中运行这个脚本,还是应该使用 ansible pip 模块在单个任务中简单地安装必要的库?venv
肯定更干净,我会那样做,但这取决于你
我收到了类似的信息,是的。但这并不意味着代码应该中断。以上是关于无法从 Ansible Playbook 中运行 Python 脚本的主要内容,如果未能解决你的问题,请参考以下文章