如何在厨师食谱中自动执行用户交互命令 [英] How to automate user interactive command in chef recipe

查看:73
本文介绍了如何在厨师食谱中自动执行用户交互命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码来自动执行/etc/init.d/oracleasm configure命令,因为它需要4个输入才能继续。

I am trying to write code for automating /etc/init.d/oracleasm configure command where as it needs 4 inputs to proceed.

Default user to own the driver interface [grid]: grid
Default group to own the driver interface [y]: oinstall
Start Oracle ASM library driver on boot (y/n) [y]: y
Scan for Oracle ASM disks on boot (y/n) [y]: y

通常,为达到此目的而编写代码的最佳方法是什么。请建议我。

Generally what is the best way to write code for achieving this. Please suggest me.

推荐答案

如果您有一个可以非交互式运行的命令会更好,但是您可以运行一个期望的交互式命令。根据您的示例,它将是:

It would be better if you had a command you could run non-interactively, but you can run an interactive command using expect. Based in your example it would be:

    bash 'OracleASM' do
        user 'root'
        code <<-EOF
        /usr/bin/expect -c 'spawn /etc/init.d/oracleasm configure
        expect "Default user to own the driver interface [grid]: "
        send "grid\r"
        expect "Default group to own the driver interface [y]: "
        send "oinstall\r"
        expect "Start Oracle ASM library driver on boot (y/n) [y]: "
        send "y\r"
        expect "Scan for Oracle ASM disks on boot (y/n) [y]: "
        send "y\r"
        expect eof'
        EOF
    end

重要的是 /etc/init.d/oracleasm configure 是幂等命令,这意味着您可以运行一次,两次或一百次,并且其行为和结果系统将相同。如果不是这种情况,则需要一些保护命令的命令( only_if not_if )仅运行命令必要时:

It is important /etc/init.d/oracleasm configure to be an idempotent command, this means that you can run it once, twice or one hundred times and its behavior and the result system will be the same. If this is not the case, you need some guards to the command (only_if or not_if) to run only the command when needed:

bash 'OracleASM' do
    user 'root'
    code <<-EOF
    ....
    EOF
    not_if { ::File.exists?('/...') }
end 

这篇关于如何在厨师食谱中自动执行用户交互命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆