简体   繁体   English

如何将密码作为参数传递给shell脚本

[英]How to pass password as an argument to a shell script

I am in process of automating installation of sage through ansible-playbook. 我正在通过ansible-playbook自动安装鼠尾草。 In that I need to run two shell scripts. 在那里我需要运行两个shell脚本。 Here is how the first shell script look: 以下是第一个shell脚本的外观:

#!/bin/bash
# Creating Sage notebook

dir="/root/.sage/sage_notebook.sagenb"
screen -S "Sage_Server" sage -c 'notebook(interface="", directory=$dir, port=80, accounts=true)'

This is the second shell script's code: 这是第二个shell脚本的代码:

#!/bin/bash
# Creating Sage inotebook

address=$(hostname --ip-address)
sage -c "inotebook(interface=" "'$address'" ",port=80,accounts=true)"

And this is how the playbook looks: 这就是剧本的样子:

---
- hosts: localhost
  remote_user: root

  tasks:
   - name : update system
     apt : update_cache=yes    

   - name : install m4
     apt : name=m4 state=present

   - name : install build-essential
     apt : name=build-essential state=present 

   - name : install gcc
     apt : name=gcc state=present

   - name : install gfortran
     apt : name=gfortran state=present

   - name : install libssl-dev
     apt : name=libssl-dev state=present

   - name : install python-software-properties
     apt : name=python-software-properties state=present

   - name : add sage ppa repo
     apt_repository: repo='ppa:aims/sagemath'

   - name : update system
     apt : update_cache=yes

   - name : install dvipng
     apt : name=dvipng state=present

   - name : install sage binary
     apt : name=sagemath-upstream-binary state=present

   - name : invoke create_sagenb script
     command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/create_sagenb -i -y

   - name : invoke start_sage script
     command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/start_sage -i -y

Now when I run the first script, it asks for a new sage password which could be anything. 现在,当我运行第一个脚本时,它会要求输入一个新的sage密码。 But I am not able to pass that password from the playbook. 但是我无法从剧本中传递密码。 Still, if I do 如果我这样做的话

ps -ef | grep sh

I could see that the scripts are running but the sage service is not running. 我可以看到脚本正在运行但是sage服务没有运行。 It needs the password in order to start the service. 它需要密码才能启动服务。

Could anyone please tell me how can I provide password as an argument to the shell scripts through command. 有谁能告诉我如何通过命令提供密码作为shell脚本的参数。

As a rule of thumb you shouldn't run scripts that needs user input in ansible playbooks. 根据经验,您不应该在ansible playbooks中运行需要用户输入的脚本。

You could try to use something like 你可以试着用类似的东西

echo "password" | script.sh

or 要么

Create a sage-password file in /etc containing the password and: /etc中创建一个包含密码的sage-password文件,并且:

script.sh < /etc/sage-password

But this will only work if it is reading from stdin - most applications read password directly from terminal device driver (ie /dev/ttyS # ), in that case this trick won't work. 但这只有在从stdin读取时才会起作用 - 大多数应用程序直接从终端设备驱动程序读取密码(即/dev/ttyS #),在这种情况下,这个技巧将无效。

If that's the case, take a look to the sage docs, they should have a more robust way for non-interactive startup. 如果是这种情况,请查看sage文档,他们应该有一个更强大的非交互式启动方式。

我不知道sage ,也不知道如何以另一种方式提供密码,但是如果程序要求输入密码,那么你可能expect按照这里的建议使用密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM