Ansible - Control commands when need users input when executing

China☆狼群 提交于 2019-12-08 02:04:19

问题


How can i control user inputs when a command is executing and ask me something for example :

sudo apt-get install mariadb-server

when you run this command in ubuntu it asked you please enter new password for mysql user root and then again it ask to enter password again for confirmation . how can i pass a variable for example mariadbpass to this command because everytime ansible run this hangs and failed so i have to login to servers and run manually this

dpkg --configure -a

to enter prompted password and its confirmation.

thank you


回答1:


Here is the Solution : add this before installing mariadb 10 task in your playbook

- name: debconf asking for password
  debconf:
   name: maria-db-10.0
   question: "{{ item }}"
   vtype: password
   value: "{{ mariadb_root_password }}"
  with_items:
   - mysql-server/root_password
   - mysql-server/root_password_again



回答2:


This is less of an ansible question and really a bash question. I'd suggest you take a look at this post and tie some of those into your playbook to do what you need.

The easiest for you in this case, is likely using a here string

tasks:
  - shell: "apt-get install mariadb-server <<< $'password\otherprompts\n'"
    sudo: true


来源:https://stackoverflow.com/questions/32861618/ansible-control-commands-when-need-users-input-when-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!