How to automate user interactive command in chef r

2019-07-13 23:50发布

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.

标签: chef recipe
1条回答
甜甜的少女心
2楼-- · 2019-07-14 00:03

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

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 
查看更多
登录 后发表回答