标签:hosts 结果 处理 代码 check fail grep 交互式 重启nginx
playbook 是ansible的核心组件,使用的是YAML语法.
下面请看简单的playbook代码
[root@LeoDevops playb]# cat nginx.yaml
- hosts: all
tasks:
- name: Install Nginx Package
apt: name=nginx state=present
- name: Copy Nginx.conf
template: src=./nginx.conf.j2 dest=/etc/nginx/nginx.conf owner=root group=root mode=0644
notify:
- Restart Nginx Service
handlers:
- name: Restart Nginx Service
service: name=nginx state=restarted
在编写playbook的时候,我们需要了解这几个重要的关键词:
我们在Inventory里面定义好变量,比如:
[root@LeoDevops playb]# grep -vE "(^$|^#)" /etc/ansible/hosts
192.168.93.132 key=132
192.168.93.137 key=137
[nginx]
192.168.93.132
192.168.93.137
[nginx:vars]
ansible_python_interpreter=/usr/bin/python2.6
[root@LeoDevops playb]# cat check_variable.yml
- hosts: all
gather_facts: False
tasks:
- name: Display Host Variable From Hostfile
debug: msg="The {{ inventory_hostname }} Value is {{ key }}"
[root@LeoDevops playb]# ansible-playbook check_variable.yml
PLAY [all] ************************************************************************************************************************************************************************************
TASK [Display Host Variable From Hostfile] ****************************************************************************************************************************************************
ok: [192.168.93.132] => {
"msg": "The 192.168.93.132 Value is 132"
}
ok: [192.168.93.137] => {
"msg": "The 192.168.93.137 Value is 137"
}
PLAY RECAP ************************************************************************************************************************************************************************************
192.168.93.132 : ok=1 changed=0 unreachable=0 failed=0
192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0
[root@LeoDevops playb]# cat p_vars.yaml
- hosts: all
gather_facts: False #设置为false,那么下面的debug模块会生效
vars: # 先申明vars这个关键字
key: "Ansible" #这种方式定义,key: value的形式
tasks:
- name: display host variables from hostfile
debug: msg="The {{ inventory_hostname }} value is {{ key }}"
[root@LeoDevops playb]# cat p_vars.yaml
- hosts: all
gather_facts: False
vars_files:
- var.json
tasks:
- name: display host variables from hostfile
debug: msg="The {{ inventory_hostname }} value is {{ key }}"
[root@LeoDevops playb]# cat var.json
{"key":"json"}
grep -vE "(^$|^#)" /etc/ansible/hosts
[nginx]
192.168.93.132
192.168.93.137
[nginx:vars]
ansible_python_interpreter=/usr/bin/python2.6
key=nginx
[root@LeoDevops playb]# ansible-playbook check_variable.yml
PLAY [all] ************************************************************************************************************************************************************************************
TASK [Display Host Variable From Hostfile] ****************************************************************************************************************************************************
ok: [192.168.93.132] => {
"msg": "The 192.168.93.132 Value is nginx"
}
ok: [192.168.93.137] => {
"msg": "The 192.168.93.137 Value is nginx"
}
PLAY RECAP ************************************************************************************************************************************************************************************
192.168.93.132 : ok=1 changed=0 unreachable=0 failed=0
192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0
[root@LeoDevops playb]# ansible-playbook check_variable.yml -e "key=hehe"
PLAY [all] ************************************************************************************************************************************************************************************
TASK [Display Host Variable From Hostfile] ****************************************************************************************************************************************************
ok: [192.168.93.132] => {
"msg": "The 192.168.93.132 Value is hehe"
}
ok: [192.168.93.137] => {
"msg": "The 192.168.93.137 Value is hehe"
}
PLAY RECAP ************************************************************************************************************************************************************************************
192.168.93.132 : ok=1 changed=0 unreachable=0 failed=0
192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0
[root@LeoDevops playb]# cat var.json
{"key":"json"}
[root@LeoDevops playb]# ansible-playbook check_variable.yml -e "@var.json"
PLAY [all] ************************************************************************************************************************************************************************************
TASK [Display Host Variable From Hostfile] ****************************************************************************************************************************************************
ok: [192.168.93.132] => {
"msg": "The 192.168.93.132 Value is json"
}
ok: [192.168.93.137] => {
"msg": "The 192.168.93.137 Value is json"
}
PLAY RECAP ************************************************************************************************************************************************************************************
192.168.93.132 : ok=1 changed=0 unreachable=0 failed=0
192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0
ansible playbook内的task之间还可以互相传递数据,比如我们总共有两个tasks,其中第二个task是否执行是需要判断第1个task运行后的结果,这个时候我们就得在task之间传递数据,需要把第一个task执行的结果传递给第二个task,下面看看简单的register的方式
[root@LeoDevops playb]# cat p_register.yaml
- hosts: u12
tasks:
- name: register variable
shell: date
register: info
- name: display variable
debug: msg="the variable is {{ info[‘stdout‘] }}"
ansible 还支持在运行playbook的时候通过交互式的方式给定义好的参数传入变量值,只需要在playbook中定义vars_prompt 的变量名和交互式提示内容即可。当然ansible还可以对传入的变量值进行加密处理。加密处理依赖于passlib python库。请看简单的 vars_prompt的例子
[root@LeoDevops playb]# cat p_prompt.yaml
- hosts: all
gather_facts: False
vars_prompt:
- name: "one"
prompt: "please input your value"
private: no
- name: "two"
prompt: "please input two value"
default: ‘good‘ # 默认显示一个值
private: yes #置为yes的话,那么就是看不见自己输入的什么了
tasks:
- name: display one value
debug: msg="one value is {{ one }}"
- name: display two value
debug: msg="two value is {{ two }}"
执行效果如下:
[root@LeoDevops playb]# ansible-playbook p_prompt.yaml
please input your value: nihao
please input two value [good]:
标签:hosts 结果 处理 代码 check fail grep 交互式 重启nginx
原文地址:https://www.cnblogs.com/liaojiafa/p/9353760.html