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

默认使用 command
不支持 $

shell

支持比 command 要全面

安装

master节点安装Ansible

  1. $ # Redhat/CentOS Linux上,Ansible目前放在的epel源中
  2. $ # Fedora默认源中包含ansible,直接安装包既可
  3. $ sudo yum install epel-release
  4. $ sudo yum install ansible -y

配置master节点与其他主机的连接

  1. $ # 生成ssh key
  2. $ ssh-keygen
  3. $ # 拷贝ssh key到远程主机,ssh的时候就不需要输入密码了
  4. $ ssh-copy-id remoteuser@remoteserver
  5. $ # ssh的时候不会提示是否保存key
  6. $ ssh-keyscan remote_servers >> ~/.ssh/known_hosts

Inventory

usa的子组有southeast, northeast, southwest, northwest,southeast的子组有atlanta, raleigh,atlanta的子组是host1, host2,raleigh子组有host2, host3

  1. [atlanta]
  2. host1
  3. host2
  4. [raleigh]
  5. host2
  6. host3
  7. [southeast:children]
  8. atlanta
  9. raleigh
  10. [usa:children]
  11. southeast
  12. northeast
  13. southwest
  14. northwest
  15. [atlanta:vars]
  16. ntp_server=ntp.atlanta.example.com
  17. proxy=proxy.atlanta.example.com

Module

module就是Ansible的“命令”,常用的Ansible module有yum、copy、template等

脚本管理——playbook

为了避免重复输入命令,Ansible提供脚本功能。Ansible脚本的名字叫Playbook,使用的是YAML的格式,文件以yml结尾。

语法

  1. 在什么机器上以什么身份执行

hosts
users

  1. 执行的任务是都有什么

tasks

  1. 善后的任务都有什么

handlers

  1. ---
  2. - hosts: web
  3. vars:
  4. http_port: 80
  5. max_clients: 200
  6. remote_user: root
  7. tasks:
  8. - name: ensure apache is at the latest version
  9. yum: pkg=httpd state=latest
  10. - name: Write the configuration file
  11. template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  12. notify:
  13. - restart apache
  14. - name: Write the default index.html file
  15. template: src=templates/index.html.j2 dest=/var/www/html/index.html
  16. - name: ensure apache is running
  17. service: name=httpd state=started
  18. handlers:
  19. - name: restart apache
  20. service: name=httpd state=restarted

task执行状态

task中每个action会调用一个module,在module中会去检查当前系统状态是否需要重新执行。

  • 如果本次执行了,那么action会得到返回值changed;
  • 如果不需要执行,那么action得到返回值ok

    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

  1. ansible all -m setup -i ~/all-in-one

关闭facts

  1. - hosts: whatever
  2. gather_facts: no

注册变量

  1. - hosts: web_servers
  2. tasks:
  3. - shell: /usr/bin/foo #执行后的结果返回给foo_result
  4. register: foo_result
  5. ignore_errors: True
  6. - shell: /usr/bin/bar
  7. when: foo_result.rc == 5

命令行传递变量

  1. yaml文件定义变量
  1. ---
  2. - hosts: '\{\{ hosts \}\}'
  3. remote_user: '\{\{ user \}\}'
  4. tasks:
  5. - ...
  1. 命令行使用变量
  1. ansible-playbook e33_var_in_command.yml --extra-vars "hosts=web user=root"
  2. ansible-playbook e33_var_in_command.yml --extra-vars "{'hosts':'vm-rhel7-1', 'user':'root'}"
  3. ansible-playbook e33_var_in_command.yml --extra-vars "@vars.json"

逻辑控制语句

  • when: 条件判断语句,类似于变成语言中的if
  • loop: 循环语句,类似于编程语言的中的while
  • block: 把几个tasks组成一块代码,便于针对一组操作的异常处理等操作。

reference

  1. https://getansible.com/