Ansible技术问答:http://linux.xyz/topic/Ansible

1、简单演示

---
- 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?: : xyz

PLAY [127.0.0.1] *******************************************************
TASK: [debug msg="name - xyz 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

2、设置默认值

---
- 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

3、输入隐藏

---
- 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 - 123123"] **********
ok: [127.0.0.1] => {
 "msg": "release_version - 2.5 passwd - 123123"
}


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

4、加密模块

加密用户的输入,需要 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