!!!! ВНИМАНИЕ
Прежде чем использовать модули shell в ansible убедитесь что нет подходящего модуля что бы решить вашу проблему!!! Прежде чем использовать модули shell в ansible убедитесь что нет подходящего модуля что бы решить вашу проблему!!! Прежде чем использовать модули shell в ansible убедитесь что нет подходящего модуля что бы решить вашу проблему!!!
Сcылки:
https://man7.org/linux/man-pages/man1/expect.1.html https://linux.die.net/man/1/expect https://linux.die.net/man/1/autoexpect https://docs.w3cub.com/ansible~2.9/modules/shell_module#shell-module https://quares.ru/?id=286324Ansible Expecthttps://docs.ansible.com/ansible/latest/collections/ansible/builtin/expect_module.html https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#ansible-collections-ansible-builtin-shell-module
Использование:
!!! expect - является языком сценариев, основные команды: expect(ожидает строку) и send(отправляет ответ) !!! autoexpect - позволяет создать файл сценарий, по умолчанию с именем script.exp cd /root - перешли в каталог root autoexpect mysql_secure_installation - будет создан script.exp сценарий ответов на команду mysql_secure_installation /root/script.exp - будет выполнен скрипт mysql_secure_installation и применятся ответы из script.exp
Пример:
Допустим, у нас есть простой сценарий оболочки test.sh
Он берет IP-адрес и порт, затем запускает команду nc.
cat test.sh
-----------------------------------------------------
#!/bin/bash
echo "IP address:"
read ip_addr
echo "Port:"
read port
nc -vz $ip_addr $port
-----------------------------------------------------
Пример playbook для скрипта
cat ansible-playbook
---------------------------
- hosts: localhost
vars:
send_ip_addr: "1.2.3.4"
send_port: "22"
tasks:
- shell: |
spawn ./test.sh
expect "IP address:"
send -- "{{ send_ip_addr }}
"
expect "Port:"
send -- "{{ send_port }}
"
expect eof
args:
executable: /usr/bin/expect
---------------------------
Пример 0:
# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
shell: |
set timeout 300
spawn ssh admin@{{ cimc_host }}
expect "password:"
send "{{ cimc_password }}\n"
expect "\n{{ cimc_name }}"
send "connect host\n"
expect "pxeboot.n12"
send "\n"
exit 0
args:
executable: /usr/bin/expect
delegate_to: localhost
Пример 1:
- name: Case insensitive password string match
ansible.builtin.expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
# you don't want to show passwords in your logs
no_log: true
Пример 2:
- name: Generic question with multiple different responses
ansible.builtin.expect:
command: /path/to/custom/command
responses:
Question:
- response1
- response2
- response3
Пример 3:
- name: Install Asternic
shell: |
set timeout 60
cd /usr/src/asternic-stats-pro-2.3.5
spawn /bin/bash -c "make install"
expect "Please enter the MySQL root password: "
send "{{ mysql_root_password }}\n"
expect "Select a password for the 'admin' user: "
send "{{ asternic_password }}\n"
expect "Confirm the password for the 'admin' user: "
send "{{ asternic_password }}\n"
sleep 60 # Нужно для того что бы было время установится и терминал не захлопнулся.
exit 0
args:
executable: /usr/bin/expect