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

How to automate user interactive command in chef recipe

我正在尝试编写代码以自动执行 /etc/init。d/oracleasm 配置命令,因为它需要 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

一般来说,实现此目标的最佳代码编写方式是什么。请建议我。

如果您有一个可以 运行 非交互的命令会更好,但是您可以 运行 使用 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