[root@afei ~]# vim inventory
192.168.240.134[root@afei ~]# cd /opt/weixin/host_vars/[root@afei host_vars]# vim 192.168.240.134
ansible_password:199127[root@afei weixin]# ansible 192.168.240.134-i inventory -m ping
192.168.240.134| SUCCESS =>{"ansible_facts":{"discovered_interpreter_python":"/usr/libexec/platform-python"},"changed": false,"ping":"pong"}
4.从命令行覆盖变量 -e
[root@afei weixin]# ansible all -i inventory -e ansible_password=199127-m ping
192.168.240.134| SUCCESS =>{"ansible_facts":{"discovered_interpreter_python":"/usr/libexec/platform-python"},"changed": false,"ping":"pong"}[root@afei weixin]# vim password
[root@afei weixin]# ansible all -i inventory -e @password -m ping
192.168.240.134| SUCCESS =>{"ansible_facts":{"discovered_interpreter_python":"/usr/libexec/platform-python"},"changed": false,"ping":"pong"}
5.使用数组作为变量
[root@afei weixin]# vim password
myhosts:192.168.240.134
ansible_password:199127192.168.240.135
ansible_password:123456*[注]: 此时再用同样的方式ping发现不通
[root@afei weixin]# ansible all -i inventory -e @password -m ping
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1(char0)*[注:]: 使用正确的变量方式
6.使用已经注册的变量捕获命令输出
[root@afei weixin]# vim playbook.yml
---- host: all
tasks:- name: install httpd and print result
yum:
name: httpd
state: present
register: result
- debug: var=result
~[root@afei weixin]# ansible-playbook -C playbook.yml
PLAY [all]****************************************************************************
TASK [Gathering Facts]****************************************************************
ok:[192.168.240.134]
TASK [install httpd and print result]*************************************************
ok:[192.168.240.134]
TASK [debug]**************************************************************************
ok:[192.168.240.134]=>{"result":{"changed": false,"failed": false,"msg":"Nothing to do","rc":0,"results":[]}}
PLAY RECAP ****************************************************************************192.168.240.134: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7.创建加密文件
[root@afei weixin]# cd group_vars/[root@afei group_vars]# ls
[root@afei group_vars]# ansible-vault create wbservers
New Vault password:
Confirm New Vault password:
ansible_password:199127*[注:]此时直接看wbserers文件时看不了的
[root@afei group_vars]# cat wbservers
$ANSIBLE_VAULT;1.1;AES256
316339666435386364393961643165336232303565316436386333396361613532366363616336363932613738316462363661373032616162656663356464330a313865376262376130336263376464
633836396236386466663339326561613064336431393565326265626333616562313635376138383530383936356265350a386235626633356363386335656531373733353930613330303537623465
63653265366537633039353164623939333333333731626664333131343033333866