| 变量 | 1 | 2 | 3 | 4 | 5 | 6 | ||
|---|---|---|---|---|---|---|---|---|
| jinja2模板 | 1 | 2 | 3 | 4 | ||||
| include | 1 | 2 | ||||||
| 循环 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 过滤器 | 1 | 2 | 3 | |||||
| 条件判断 | 错误处理 | block | tests | 条件判断 | ||||
| 模块 | 基本使用 | 文件操作 | 文件操作2 | 命令类 | 包管理 | 系统类2 | 系统类 | |
| 其他 | 角色 | handlers | playbook1 | playbook2 | tags | 清单 | 基本概念 | |
| lookup插件 | 常用技巧 | 常用技巧2 | 加密 | 角色 |
变量优先级
extra vars (-e 选项指定的变量)最高
inventory 主机清单中定义的变量(ansible_ssh_user等)
play剧本中vars、vars_files定义的变量
系统的facts变量
角色定义的默认变量 最低
从上到下优先级逐渐降低,高优先级会覆盖掉低优先级的变量
常用模块
command
shell
安装
master节点安装Ansible
$ # Redhat/CentOS Linux上,Ansible目前放在的epel源中$ # Fedora默认源中包含ansible,直接安装包既可$ sudo yum install epel-release$ sudo yum install ansible -y
配置master节点与其他主机的连接
$ # 生成ssh key$ ssh-keygen$ # 拷贝ssh key到远程主机,ssh的时候就不需要输入密码了$ ssh-copy-id remoteuser@remoteserver$ # ssh的时候不会提示是否保存key$ ssh-keyscan remote_servers >> ~/.ssh/known_hosts
Inventory
usa的子组有southeast, northeast, southwest, northwest,southeast的子组有atlanta, raleigh,atlanta的子组是host1, host2,raleigh子组有host2, host3
[atlanta]host1host2[raleigh]host2host3[southeast:children]atlantaraleigh[usa:children]southeastnortheastsouthwestnorthwest[atlanta:vars]ntp_server=ntp.atlanta.example.comproxy=proxy.atlanta.example.com
Module
module就是Ansible的“命令”,常用的Ansible module有yum、copy、template等
脚本管理——playbook
为了避免重复输入命令,Ansible提供脚本功能。Ansible脚本的名字叫Playbook,使用的是YAML的格式,文件以yml结尾。
语法
- 在什么机器上以什么身份执行
hosts
users
…
- 执行的任务是都有什么
tasks
- 善后的任务都有什么
handlers
---- hosts: webvars:http_port: 80max_clients: 200remote_user: roottasks:- name: ensure apache is at the latest versionyum: pkg=httpd state=latest- name: Write the configuration filetemplate: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.confnotify:- restart apache- name: Write the default index.html filetemplate: src=templates/index.html.j2 dest=/var/www/html/index.html- name: ensure apache is runningservice: name=httpd state=startedhandlers:- name: restart apacheservice: name=httpd state=restarted
task执行状态
task中每个action会调用一个module,在module中会去检查当前系统状态是否需要重新执行。
- 如果本次执行了,那么action会得到返回值changed;
-
handler
Handlers里面的每一个handler,也是对module的一次调用。而handlers与tasks不同,tasks会默认的按定义顺序执行每一个task,handlers则不会,它需要在tasks中被调用(notify),才有可能被执行。
一个handler最多执行一次,若被多个task调用,也只执行一次
- 只有task的状态是changed,handler才会被调用
- handler按照定义的顺序执行,与notify的顺序无关
变量
playbook中定义的变量
{{}}引用 ```yaml - hosts: webservers vars: http_port: 80 ```
定义在文件中变量
YAML陷阱
facts
facts为系统信息,可直接作为变量使用
查看能被引用的facts
ansible all -m setup -i ~/all-in-one
关闭facts
- hosts: whatevergather_facts: no
注册变量
- hosts: web_serverstasks:- shell: /usr/bin/foo #执行后的结果返回给foo_resultregister: foo_resultignore_errors: True- shell: /usr/bin/barwhen: foo_result.rc == 5
命令行传递变量
- yaml文件定义变量
---- hosts: '\{\{ hosts \}\}'remote_user: '\{\{ user \}\}'tasks:- ...
- 命令行使用变量
ansible-playbook e33_var_in_command.yml --extra-vars "hosts=web user=root"ansible-playbook e33_var_in_command.yml --extra-vars "{'hosts':'vm-rhel7-1', 'user':'root'}"ansible-playbook e33_var_in_command.yml --extra-vars "@vars.json"
逻辑控制语句
- when: 条件判断语句,类似于变成语言中的if
- loop: 循环语句,类似于编程语言的中的while
- block: 把几个tasks组成一块代码,便于针对一组操作的异常处理等操作。
