sapser's blog

Standing on the shoulders of Giants

ansible学习之十一:Prompts

Jul 22, 2014 • ansible


用在执行playbook的过程中,提示用户输入,并接收用户的输入

---
- hosts: 127.0.0.1
  vars_prompt:
    name: "what is your name?"
    quest: "what is your quest?"
    favcolor: "what is your favorite color?"
  tasks:
    - debug: msg="name - {{name}}   quest - {{quest}}    favcolor - {{favcolor}}"

执行:

$ ansible-playbook prompt.yml
what is your favorite color?: : red                #注意问题提示的顺序是反过来的
what is your quest?: : ansible
what is your name?: : sapser

PLAY [127.0.0.1] **************************************************************

TASK: [debug msg="name - sapser   quest - ansible    favcolor - red"] *********
ok: [127.0.0.1] => {
    "msg": "name - sapser   quest - ansible    favcolor - red"
}

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0   


设置默认值

---
- hosts: 127.0.0.1
  gather_facts: no
  vars_prompt:
    - name: "release_version"          #变量名
      prompt: "product release version"          #提示
      default: "1.0"          #默认值
  tasks:
    - debug: msg="release_version - {{release_version}}"

执行:

$ ansible-playbook prompt.yml
product release version [1.0]: 1.2 

PLAY [127.0.0.1] **************************************************************

TASK: [debug msg="release_version - 1.2"] *************************************
ok: [127.0.0.1] => {
    "msg": "release_version - 1.2"
}

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=0  


用户输入不可见

---
- hosts: 127.0.0.1
  gather_facts: no
  vars_prompt:
    - name: "release_version"
      prompt: "product release version"
      default: "1.0"
      private: no
    - name: "passwd"
      prompt: "Enter password"
      private: yes
  tasks:
    - debug: msg="release_version - {{release_version}}      passwd - {{passwd}}"

执行:

$ ansible-playbook prompt.yml
product release version [1.0]: 2.5
Enter password:

PLAY [127.0.0.1] **************************************************************

TASK: [debug msg="release_version - 2.5      passwd - sdjflskdfjsd"] **********
ok: [127.0.0.1] => {
    "msg": "release_version - 2.5      passwd - sdjflskdfjsd"
}

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=0   


加密用户的输入,需要python第三方模块Passlib

vars_prompt:
  - name: "my_password2"
    prompt: "Enter password2"
    private: yes
    encrypt: "md5_crypt"
    confirm: yes
    salt_size: 7

Passlib支持多种加密方式:

  • `des_crypt` - DES Crypt
  • `bsdi_crypt` - BSDi Crypt
  • `bigcrypt` - BigCrypt
  • `crypt16` - Crypt16
  • `md5_crypt` - MD5 Crypt
  • `bcrypt` - BCrypt
  • `sha1_crypt` - SHA-1 Crypt
  • `sun_md5_crypt` - Sun MD5 Crypt
  • `sha256_crypt` - SHA-256 Crypt
  • `sha512_crypt` - SHA-512 Crypt
  • `apr_md5_crypt` - Apache’s MD5-Crypt variant
  • `phpass` - PHPass’ Portable Hash
  • `pbkdf2_digest` - Generic PBKDF2 Hashes
  • `cta_pbkdf2_sha1` - Cryptacular’s PBKDF2 hash
  • `dlitz_pbkdf2_sha1` - Dwayne Litzenberger’s PBKDF2 hash
  • `scram` - SCRAM Hash
  • `bsd_nthash` - FreeBSD’s MCF-compatible nthash encoding


上一篇:ansible学习之十:Error Handling In Playbooks

下一篇:ansible学习之十二:Using Lookups