forked from linux985/linux985.github.io
-
Notifications
You must be signed in to change notification settings - Fork 1
ansible
linux211 edited this page Jul 18, 2017
·
12 revisions
9.变量优先级
* extra vars(命令中-e)最优先
* inventory 主机清单中连接变量(ansible_ssh_user 等) * play 中 vars、vars_files 等
* 剩余的在 inventory 中定义的变量
* 系统的 facts 变量
* 角色定义的默认变量(roles/rolesname/defaults/main.yml)
注释:子组会覆盖父组,主机总是覆盖组定义的变量
{{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }}
获取组中第一个主机的ip地址
{{ hostvars[groups['webservers'][0]]['ansible_eth0']['ipv4']['address'] }}
Python加密ansible 用户密码(使用python设置用户的密码)
python -c 'import crypt; print crypt.crypt("APIGateway@123", "$5$Bs59BYzF$")'
ansible支持主机列表的正则匹配
全量: all/*
逻辑或: :
逻辑非: !
逻辑与: &
切片: []
正则匹配: 以~开头
ansible all -m ping #所有默认inventory文件中的机器
ansible "*" -m ping #同上
ansible 121.28.13.* -m ping #所有122.28.13.X机器
ansible web1:web2 -m ping #所有属于组web1或属于web2的机器
ansible web1:!web2 -m ping #属于组web1,但不属于web2的机器
ansible web1&web2 -m ping #属于组web1又属于web2的机器
ansible webserver[0] -m ping #属于组webserver的第1台机器
ansible webserver[0:5] -m ping #属于组webserver的第1到4台机器
ansible "~(beta|web)\.example\.(com|org)" -m ping
郭波(gwx441205) 15:55
A. 某个playbook中多处用到
在执行的shell脚本里导入环境变量:export ANSIBLE_GATHERING=implicit
dmk@I-DMK02-98:/pkg_repo/playbook/DMKTest/1.0.3> cat test_sudo.sh
#!/bin/bash
export ANSIBLE_GATHERING=implicit
ansible-playbook -i hosts test_sudo.yml -k -K