如何在厨师食谱中自动化用户交互命令

时间:2015-07-03 10:01:41

标签: chef recipe

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

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

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

1 个答案:

答案 0 :(得分:1)

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

    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_ifnot_if)进行一些保护,以便在需要时仅运行命令:

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