我正在编写用于在CentOS 7上安装JsReport的ansible脚本。我正在尝试翻译这些操作:https://jsreport.net/learn/centos
进入ansible脚本。
到目前为止我尝试的是:
- hosts: localhost
tasks:
- name: install wget
yum:
name: wget
state: latest
- name: download nvm
get_url: url=https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh dest=/opt/nvm-install.sh
- name: Changing perm of "/opt/nvm-install.sh", adding "+x"
file: dest=/opt/nvm-install.sh mode=a+x
- name: Execute the script
command: sh /opt/nvm-install.sh
- name: Changing perm of "~/.nvm/nvm.sh", adding "+x"
file: dest=~/.nvm/nvm.sh mode=a+x
- name: Make nvm command work in current terminal, Install node and set version, npm install jsreport-cli
shell: |
source ~/.nvm/nvm.sh
nvm install 8.11.3
npm install jsreport-cli -g
mkdir jsreportapp
jsreportapp
jsreport init
jsreport configure
- name: download install-google-chrome.sh
get_url: url=https://intoli.com/install-google-chrome.sh dest=/opt/install-google-chrome.sh
- name: Changing perm of "/opt/install-google-chrome.sh", adding "+x"
file: dest=/opt/install-google-chrome.sh mode=a+x
- name: Execute the script
command: sh /opt/install-google-chrome.sh
通常我用follow命令启动ansible脚本:
sudo ansible-playbook install_jsreport.yml
我不确定在这里使用sudo
是一个好习惯,还是我必须在这里使用shell
?
在上面的脚本中我收到以下错误:
致命:[localhost]:失败! => {“已更改”:true,“cmd”:“source~ / .nvm / nvm.sh \ n nvm install 8.11.3 \ n npm install jsreport-cli -g \ n mkdir jsreportapp \ n jsreportapp \ n jsreport init \ n jsreport configure“,”delta“:”0:01:03.627957“,”end“:”2019-04-19 15:22:26.374742“,”msg“:”非零返回码“,”rc“ : - 2,“start”:“2019-04-19 15:21:22.746785”,“stderr”:“已经安装了v8.11.3。\ nmkdir:无法创建目录'jsreportapp':文件存在\ n / bin / sh:第4行:jsreportapp:命令未找到\ n发生意外错误:命令失败:npm i -S jsreport \ nsh:node:command not found \ nnpm WARN [email protected]需要webpack@^2.0的对等方。 0 || ^ 3.0.0但没有安装。你必须自己安装对等依赖项。\ nnpm WARN [email protected]需要一个webpack@^2.0.0 || ^ 3.0.0 || ^ 4.0的对等体。 0但没有安装。你必须自己安装对等依赖项。\ nnpm WARN jsreport-server @没有描述\ nnpm WARN jsreport-server @没有存储库字段。\ nnpm WARN jsreport-server @没有许可证字段。\ nnpm WARN可选SKIPPING OPTIONAL DEPENDENCY:[email protected](node_modules / fsevents):\ nnpm WARN notsup SKIPPING OPTIONAL DEPENDENCY:不支持的[email protected]平台:想要{\“os \”:\“darwin \”,\“arch \“:\”any \“}(当前:{\”os \“:\”linux \“,\”arch \“:\”x64 \“})\ n \ nnpm ERR!文件sh \ nnpm错误!代码ELIFECYCLE \ nnpm ERR!错误ENOENT \ nnpm ERR!系统调用spawn \ nnpm ERR! [email protected]安装:
node install.js
\ nnpm ERR!产生ENOENT \ nnpm ERR! \ nnpm ERR!在[email protected]安装脚本失败。\ nnpm ERR!这可能不是npm的问题。上面可能有额外的日志记录输出。\ n \ nnpm ERR!可以在以下位置找到此运行的完整日志:\ nnpm ERR! /root/.npm/_logs/2019-04-19T13_22_24_770Z-debug.log\n(1)。 \ ncaused by error(1) - > meta = {\“killed \”:false,\“code \”:1,\“signal \”:null,\“cmd \”:\“npm i -S jsreport \ “},stack =错误:\ n在ChildProcess.exithandler(child_process.js:275:12)\ n,在emitTwo(events.js:126:13)\ n,在ChildProcess.emit(events.js:214:7) \ n at maybeClose(internal / child_process.js:925:16)\ n在Process.ChildProcess._handle.onexit(internal / child_process.js:209:5)“,”stderr_lines“:[”v8.11.3已经安装。“,”mkdir:无法创建目录'jsreportapp':文件存在“,”/ bin / sh:第4行:jsreportapp:命令未找到“,”发生意外错误:命令失败:npm i -S jsreport“,”sh :node:command not found“,”npm WARN [email protected]需要一个webpack@^2.0.0的同行|| ^ 3.0.0但没有安装。您必须自己安装对等依赖项。“,”npm WARN [email protected]需要webpack @^2.0.0 ||的对等方^ 3.0.0 || ^ 4.0.0但没有安装。您必须自己安装对等依赖项。“,”npm WARN jsreport-server @无描述“,”npm WARN jsreport-server @无存储库字段。“,”npm WARN jsreport-server @无许可证字段。“,”npm WARN可选跳过可选的依赖性:[email protected](node_modules / fsevents):“,”npm WARN notsup跳过可选的依赖性:不支持的[email protected]平台:想要{\“os \”:\“darwin \”,\“ arch \“:\”any \“}(当前:{\”os \“:\”linux \“,\”arch \“:\”x64 \“})”,“”,“npm ERR!文件sh“,”npm ERR!代码ELIFECYCLE“,”npm ERR! errno ENOENT“,”npm ERR!系统调用生成“,”npm ERR! [email protected]安装:node install.js
“,”npm ERR!产生ENOENT“,”npm ERR! “,”npm ERR!在[email protected]安装脚本失败。“,”npm ERR!这可能不是npm的问题。上面可能有额外的日志记录输出。“,”“,”npm ERR!可以在以下位置找到此运行的完整日志:“,”npm ERR! /root/.npm/_logs/2019-04-19T13_22_24_770Z-debug.log“,”(1)。 “,”由错误引起(1) - > meta = {\“killed \”:false,\“code \”:1,\“signal \”:null,\“cmd \”:\“npm i -S jsreport \“},stack =错误:”,“,在ChildProcess.exithandler(child_process.js:275:12)”,“,在emitTwo(events.js:126:13)”,“ 在ChildProcess.emit(events.js:214:7)“,”at maybeClose(internal / child_process.js:925:16)“,”在Process.ChildProcess._handle.onexit(internal / child_process.js:209:5) )“],”stdout“:”现在使用节点v8.11.3(npm v5.6.0)\ n / root / .nvm / versions / node / v8.11.3 / bin / jsreport - > /root/.nvm/versions/ node / v8.11.3 / lib / node_modules / jsreport-cli / cli.js \ n + [email protected] \ nadded未找到16.187s \ njsreport安装中的333个软件包,现在安装jsreport最新版本,稍等片刻.. 。\ n吗?要启用Web服务器吗? (是/否)\ u001b [42D \ u001b [42C“,”stdout_lines“:[”现在使用节点v8.11.3(npm v5.6.0)“,”/ root / .nvm /versions / node / v8.11.3 / bin / jsreport - > /root/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/cli.js“,”+ [email protected]“,”在16.187s中添加了333个软件包“,”找不到jsreport安装,现在安装jsreport最新版本,等一下......“,”?要启用Web服务器吗? (是/否)\ u001b [42D \ u001b [42C“]}
编辑(关于larsks答案):
任务[init jsreportapp目录] ******************************************** ************************************************** ***************致命:[localhost]:失败了! => {“changed”:true,“cmd”:“。〜/ .nvm / nvm.sh \ n jsreport init”,“delta”:“0:00:46.367234”,“end”:“2019-04- 23 10:12:46.142142“,”msg“:”非零返回码“,”rc“:1,”开始“:”2019-04-23 10:11:59.774908“,”stderr“:”意外错误发生了:命令失败:npm i -S jsreport \ nsh:node:command not found \ nnpm WARN [email protected]需要webpack@^2.0.0 || ^ 3.0.0的对等体,但没有安装。你没有必须自己安装对等依赖项。\ nnpm WARN [email protected]需要webpack @^2.0.0 || ^ 3.0.0 || ^ 4.0.0的对等体,但是没有安装。您必须自己安装对等依赖项。 \ nnpm WARN jsreport-server @无描述\ nnpm WARN jsreport-server @无存储库字段。\ nnpm WARN jsreport-server @无许可证字段。\ nnpm WARN可选跳过选项依赖性:[email protected](node_modules / fsevents): \ nnpm WARN notsup SKIPPING OPTIONAL DEPENDENCY:不支持的[email protected]平台:想要{\“os \”:\“darwin \”,\“arch \”:\“any \”}(当前:{\“os \ “:\” 的linux \”,\ “拱\”:\ “64 \” } \ n \ nnpm ERR!文件sh \ nnpm错误!代码ELIFECYCLE \ nnpm ERR!错误ENOENT \ nnpm ERR!系统调用spawn \ nnpm ERR! [email protected]安装:
node install.js
\ nnpm ERR!产生ENOENT \ nnpm ERR! \ nnpm ERR!在[email protected]安装脚本失败。\ nnpm ERR!这可能不是npm的问题。上面可能有额外的日志记录输出。\ n \ nnpm ERR!可以在以下位置找到此运行的完整日志:\ nnpm ERR! /root/.npm/_logs/2019-04-23T08_12_46_089Z-debug.log\n(1)。 \ ncaused by error(1) - > meta = {\“killed \”:false,\“code \”:1,\“signal \”:null,\“cmd \”:\“npm i -S jsreport \ “},stack =错误:\ n在ChildProcess.exithandler(child_process.js:275:12)\ n,在emitTwo(events.js:126:13)\ n,在ChildProcess.emit(events.js:214:7) \ n at maybeClose(internal / child_process.js:925:16)\ n在Socket.stream.socket.on(internal / child_process.js:346:11)\ n在emitOne(events.js:116:13)\ n在Socket.emit(events.js:211:7)\ n在Pipe._handle.close [as _onclose](net.js:557:12)“,”stderr_lines“:[”发生意外错误:命令失败: npm i -S jsreport“,”sh:node:command not found“,”npm WARN [email protected]需要一个webpack @^2.0.0的同行|| ^ 3.0.0但没有安装。您必须自己安装对等依赖项。“,”npm WARN [email protected]需要webpack @^2.0.0 ||的对等方^ 3.0.0 || ^ 4.0.0但没有安装。您必须自己安装对等依赖项。“,”npm WARN jsreport-server @无描述“,”npm WARN jsreport-server @无存储库字段。“,”npm WARN jsreport-server @无许可证字段。“,”npm WARN可选跳过可选的依赖性:[email protected](node_modules / fsevents):“,”npm WARN notsup跳过可选的依赖性:不支持的[email protected]平台:想要{\“os \”:\“darwin \”,\“ arch \“:\”any \“}(当前:{\”os \“:\”linux \“,\”arch \“:\”x64 \“})”,“”,“npm ERR!文件sh“,”npm ERR!代码ELIFECYCLE“,”npm ERR! errno ENOENT“,”npm ERR!系统调用生成“,”npm ERR! [email protected]安装:node install.js
“,”npm ERR!产生ENOENT“,”npm ERR! “,”npm ERR!在[email protected]安装脚本失败。“,”npm ERR!这可能不是npm的问题。上面可能有额外的日志记录输出。“,”“,”npm ERR!可以在以下位置找到此运行的完整日志:“,”npm ERR! /root/.npm/_logs/2019-04-23T08_12_46_089Z-debug.log“,”(1)。 “,”由错误引起(1) - > meta = {\“killed \”:false,\“code \”:1,\“signal \”:null,\“cmd \”:\“npm i -S jsreport \“},stack =错误:”,“,在ChildProcess.exithandler(child_process.js:275:12)”,“,在emitTwo(events.js:126:13)”,“ 在ChildProcess.emit(events.js:214:7)“,”在maybeClose(internal / child_process.js:925:16)“,”在Socket.stream.socket.on(internal / child_process.js:346:11) )“,”在emitOne(events.js:116:13)“,”在Socket.emit(events.js:211:7)“,”在Pipe._handle.close [as _onclose](net.js:557) :12)“],”stdout“:”找不到jsreport安装,现在安装jsreport最新版本,等一下......“,”stdout_lines“:[”找不到jsreport安装,现在安装jsreport最新版本,等一下......“]}
Aaditi:
删除temp
并在localhost上发出以下命令后:
sudo chmod 777 -R jsReport/
npm install puppeteer jsreport-chrome-pdf
然后用sudo运行你的剧本,它没有错误地工作。
编辑3:
添加:
- name: install puppeteer
shell: |
. ~/.nvm/nvm.sh
npm install puppeteer -g
args:
creates: "~/.nvm/versions/node/v{{ node_version }}/lib/node_modules/puppeteer "
给出以下错误:
任务[安装木偶] ********************************************* ************************************************** ************************************************** *********************************************致死:[localhost] :失败了! => {“已更改”:true,“cmd”:“。〜/ .nvm / nvm.sh \ n npm install puppeteer -g”,“delta”:“0:00:04.178220”,“end”:“2019 -04-23 11:39:44.413525“,”msg“:”非零返回码“,”rc“:1,”开始“:”2019-04-23 11:39:40.235305“,”stderr“: “sh:node:command not found \ nnpm ERR!file sh \ nnpm ERR!code ELIFECYCLE \ nnpm ERR!errno ENOENT \ nnpm ERR!syscall spawn \ nnpm ERR![email protected] install:
node install.js
\ nnpm ERR!spawn ENOENT \ nnpm ERR!\ nnpm ERR!在[email protected]安装脚本失败。\ nnpm ERR!这可能不是npm的问题。上面可能有额外的日志输出。\ n \ nnpm ERR!完整的日志这个运行可以在:\ nnpm ERR中找到! /root/.npm/_logs/2019-04-23T09_39_44_391Z-debug.log“,”stderr_lines“:[”sh:node:command not found“,”npm ERR!文件sh“,”npm ERR!代码ELIFECYCLE“,”npm ERR! errno ENOENT“,”npm ERR!系统调用生成“,”npm ERR! [email protected]安装:node install.js
“,”npm ERR!产生ENOENT“,”npm ERR! “,”npm ERR!在[email protected]安装脚本失败。“,”npm ERR!这可能不是npm的问题。上面可能有额外的日志记录输出。“,”“,”npm ERR!可以在以下位置找到此运行的完整日志:“,”npm ERR! /root/.npm/_logs/2019-04-23T09_39_44_391Z-debug.log“],”stdout“:”\ n \ n> [email protected] install /root/.nvm/versions/node/v8.11.3/lib/ node_modules / puppeteer \ n> node install.js“,”stdout_lines“:[”“,”> [email protected] install /root/.nvm/versions/node/v8.11.3/lib/node_modules/puppeteer“,” > node install.js“]}
EDIT4
我试过这样的事情:
---
- hosts: localhost
vars:
node_version: 8.11.3
nvm_version: 0.33.11
tasks:
- name: Add the user 'jsreport'
user:
name: jsreport
system: true
- name: install wget
yum:
name: wget
state: latest
become: true
become_user: jsreport
- name: download nvm
get_url:
url: "https://raw.githubusercontent.com/creationix/nvm/v{{ nvm_version }}/install.sh"
dest: /opt/nvm-install.sh
become: true
become_user: jsreport
- name: Changing perm of "/opt/nvm-install.sh", adding "+x"
file:
dest: /opt/nvm-install.sh
mode: "a+x"
become: true
become_user: jsreport
- name: Execute the script
command: /opt/nvm-install.sh
args:
creates: ~/.nvm/nvm.sh
become: true
become_user: jsreport
- name: Changing perm of "~/.nvm/nvm.sh", adding "+x"
file:
dest: ~/.nvm/nvm.sh
mode: "a+x"
become: true
become_user: jsreport
- name: install node
shell: |
. ~/.nvm/nvm.sh
nvm install {{ node_version }}
args:
creates: "~/.nvm/versions/node/v{{ node_version }}"
become: true
become_user: jsreport
- name: install jsreport-cli
shell: |
. ~/.nvm/nvm.sh
npm install jsreport-cli -g
args:
creates: "~/.nvm/versions/node/v{{ node_version }}/lib/node_modules/jsreport-cli"
become: true
become_user: jsreport
- name: install jsreport-chrome-pdf
shell: |
. ~/.nvm/nvm.sh
npm install jsreport-chrome-pdf -g
args:
creates: "~/.nvm/versions/node/v{{ node_version }}/lib/node_modules/jsreport-chrome-pdf"
become: true
become_user: jsreport
- name: install puppeteer
shell: |
. ~/.nvm/nvm.sh
npm install puppeteer -g
become: true
become_user: jsreport
args:
creates: "~.nvm/versions/node/v{{ node_version }}/lib/node_modules/puppeteer "
- name: create jsreportapp directory
file:
path: ./jsreportapp
state: directory
become: true
become_user: jsreport
- name: init jsreportapp directory
shell: |
. ~/.nvm/nvm.sh
jsreport init
args:
chdir: ./jsreportapp
creates: ./package-lock.json
become: true
become_user: jsreport
之后,当我在用户jsreport
我运行jsreport configure
命令然后我编辑此配置设置:
“chrome”:{“launchOptions”:{“args”:[“ - no-sandbox”]}}
然后我运行jsreport start
并得到错误说:
无法找到检查命令是否可用所必需的jsreport安装,如果命令是有效的,请先尝试安装jsreport。 (1)。由错误引起(1) - > stack =错误:在Promise(/home/jsreport/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/lib/instanceHandler.js:55:21)at在getInstance(/ home / jsreport)的Object.find(/home/jsreport/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/lib/instanceHandler.js:47:10)上的新Promise() /s.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/lib/commander.js:768:10)在exports.handler(/home/jsreport/.nvm/versions/node/v8.11.3) /lib/node_modules/jsreport-cli/lib/commands/start.js:56:5)在Commander.executeCommand(/home/jsreport/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/ lib / commander.js:389:28)在Object.handler(/home/jsreport/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/lib/commander.js:559:16)at at parseArgs上的Object.self.runCommand(/home/jsreport/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/node_modules/yargs/lib/command.js:170:22)(/ home / jsreport / .nvm /版本/节点/ v8.11.3 / LIB / node_modules / jsreport-CLI / node_modul es / yargs / yargs.js:920:28)at Object.Yargs.self.parse(/home/jsreport/.nvm/versions/node/v8.11.3/lib/node_modules/jsreport-cli/node_modules/yargs/yargs的.js:499:18)
这里的根本问题是jsreport configure
命令是交互式的。如果您要手动运行它,您会看到:
$ jsreport configure
? Do you want to enable web server? (Y/n)
但是如果你在没有附加终端的情况下运行该命令(这是Ansible运行它的方式),它将失败并返回非零的退出代码:
[root@365f191cdad1 ~]# jsreport configure < /dev/null
? Do you want to enable web server? (Y/n)
[root@365f191cdad1 ~]# echo $?
130
由于这是shell脚本中的最后一个命令,因此它将成为shell脚本的退出代码。由于您的shell脚本以非零错误退出,因此Ansible认为任务失败,并中止该剧本。
如果你要删除对jsreport configure
的调用,那么你的剧本几乎可以工作,除非你的剧本中出现了拼写错误。你有:
mkdir jsreportapp
jsreportapp
我想你在那里错过了一个cd
:
mkdir jsreportapp
cd jsreportapp
换句话说,这运行没有错误:
- hosts: localhost
tasks:
- name: install wget
yum:
name: wget
state: latest
- name: download nvm
get_url: url=https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh dest=/opt/nvm-install.sh
- name: Changing perm of "/opt/nvm-install.sh", adding "+x"
file: dest=/opt/nvm-install.sh mode=a+x
- name: Execute the script
command: sh /opt/nvm-install.sh
- name: Changing perm of "~/.nvm/nvm.sh", adding "+x"
file: dest=~/.nvm/nvm.sh mode=a+x
- name: Make nvm command work in current terminal, Install node and set version, npm install jsreport-cli
shell: |
source ~/.nvm/nvm.sh
nvm install 8.11.3
npm install jsreport-cli -g
mkdir jsreportapp
cd jsreportapp
jsreport init
但仍有许多事情值得修复。首先,您从mkdir jsreportapp
收到错误,如果该目录已存在则会失败。你可以改为调用mkdir -p
,但最好把它移到一个单独的任务中,然后按照OmPrakashP的建议拆分你的最终任务:
- name: install node
shell: |
. ~/.nvm/nvm.sh
nvm install 8.11.3
- name: install jsreport-cli
shell: |
. ~/.nvm/nvm.sh
npm install jsreport-cli -g
- name: create jsreportapp directory
file:
path: ./jsreportapp
state: directory
- name: init jsreportapp directory
shell: |
. ~/.nvm/nvm.sh
jsreport init
args:
chdir: ./jsreportapp
而不是运行jsreport configure
,这是一个交互式命令,考虑只使用Ansible jsreport.config.json
任务复制适当的copy
,或者如果你想能够在剧本运行中动态设置事物,则使用template
模块进行模板化。
我最后的剧本 - 包括一些小的风格变化(总是使用模板选项的yaml语法,而不是key = value)和一些幂等变化(避免重新运行安装任务等) - 看起来像这样:
---
- hosts: localhost
vars:
node_version: 8.11.3
nvm_version: 0.33.11
tasks:
- name: install wget
become: true
yum:
name: wget
state: latest
- name: download nvm
get_url:
url: https://raw.githubusercontent.com/creationix/nvm/v{{ nvm_version }}/install.sh
dest: ~/nvm-install.sh
- name: Changing perm of "/opt/nvm-install.sh", adding "+x"
file:
dest: ~/nvm-install.sh
mode: "a+x"
- name: Execute the script
command: ~/nvm-install.sh
args:
creates: ~/.nvm/nvm.sh
- name: Changing perm of "~/.nvm/nvm.sh", adding "+x"
file:
dest: ~/.nvm/nvm.sh
mode: "a+x"
- name: install node
shell: |
. ~/.nvm/nvm.sh
nvm install {{ node_version }}
args:
creates: "~/.nvm/versions/node/v{{ node_version }}"
- name: install jsreport-cli
shell: |
. ~/.nvm/nvm.sh
npm install jsreport-cli -g
args:
creates: "~/.nvm/versions/node/v{{ node_version }}/lib/node_modules/jsreport-cli"
- name: create jsreportapp directory
file:
path: ./jsreportapp
state: directory
- name: init jsreportapp directory
shell: |
. ~/.nvm/nvm.sh
jsreport init
args:
chdir: ./jsreportapp
creates: ./package-lock.json
你可以在this repository找到上面的剧本。
更新
我编辑了剧本,以便它作为非root用户成功运行。这包括(a)将become: true
添加到wget
安装任务中,以及(b)将nvm-install.sh
安装到当前用户主目录而不是/opt
。
您可以在此处看到此作为非root用户成功运行:
第二次成功运行:
如您所见,您报告的错误不会突然出现,并且可能与您的环境有关。您可能想尝试使用新系统。
在运行jsreport start
之前,请记住您需要在~/.nvm/nvm.sh
脚本中提供源代码。如果你这样做,它似乎成功地工作:
你的剧本在下面的阶段失败了
shell: |
source ~/.nvm/nvm.sh
nvm install 8.11.3
npm install jsreport-cli -g
mkdir jsreportapp
jsreportapp
jsreport init
jsreport configure
您可以将其进一步分解为多个任务。
这是你应该首先尝试https://github.com/geerlingguy/ansible-role-nodejs
另请参阅本指南,了解节点模块安装https://docs.ansible.com/ansible/latest/modules/npm_module.html