1 介绍

       Ansible 是一个系统自动化工具,用来做系统配管理,批量对远程主机执行操作指令。

 

2 实验环境

      

ip

角色

192.168.40.71

ansible管控端

192.168.40.72

远程机器A

192.168.40.73

远程机器B

 

在管控端安装ansible:

       yum install epel-release

  yum install ansible

配置管控端可无密登陆A/B机器

       在管控端生成key并复制到A、B机器

  ssh-keygen -t rsa

  ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.40.72

  ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.40.73

验证可无密登陆即配置成功

 

配置管控端的管理远程主机ip

vim /etc/ansible/hosts 添加如下配置,中括号的内容为你自定义的名字。

 [web]

192.168.40.72

192.168.40.73

 

 

至此配置完成,执行:

[

root@localhost ~]# ansible web -m ping
192.168.40.73 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.40.72 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

ansible相关的命令:

  ansible  用来执行ansible管理命令

  ansible-playbook 当有众多任务时,可编写成playbook来运行

  ansible-doc 用来获取模块的帮助文档

 

 

ansible的简单使用格式:

  ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS

 

获取模块列表

  ansible-doc -l 里面有众多模块,掌握一些常用的即可满足日常工作

  ansible-doc -s modulename # 获取模块简要使用说明

示例:

[root@localhost ~]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
  ping:
      data:                  # Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception.

编写第一个playbook:

       playbook基于YAML语法来编写,基本语法规则如下:

  1.大小写敏感

  2.使用缩进表示层级关系

  3.缩进时不允许使用Tab键,只允许使用空格

  4.缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

  5. # 表示注释,从这个字符一直到行尾,都会被解析器忽略。

       6. 用---表示开头

 

YAML 支持三种数据结构:

  对象:键值对的集合,用冒号:作为键值分隔

  数组:一组按次序排列的值,用减号-作为标识

  纯量:单个的、不可再分的值,如字符串,数值,日期等

 

 

例子:

---
- hosts: web
  remote_user: root
  tasks:
    - name: ping test
      ping: null

这个playbook表示去标识为web机的远程主机上,用root用户去执行名为ping test的任务,它使用ping模块。其中ping test为自己定义的任务名,会在稍后的执行输出中展示出来。

 

其对应的json结构为:

[
    {
        "hosts": "web",
        "remote_user": "root",
        "tasks": [
            {
                "name": "ping test",
                "ping": null
            }
        ]
    }
]

运行:

root@localhost playbook]# ansible-playbook ping.yml
 
PLAY [web] ********************************************************************************
 
TASK [Gathering Facts] ********************************************************************
ok: [192.168.40.72]
ok: [192.168.40.73]
 
TASK [ping test] **************************************************************************
ok: [192.168.40.73]
ok: [192.168.40.72]
 
PLAY RECAP ********************************************************************************
192.168.40.72              : ok=2    changed=0    unreachable=0    failed=0  
192.168.40.73              : ok=2    changed=0    unreachable=0    failed=0

来写一个更实用的playbook:

[root@localhost playbook]# cat add_user.yml
---
- hosts: web
  remote_user: root
  gather_facts: true
  tasks:
  - name: Add users
    user: name={{ item }} state=present groups=wheel
    with_items:
      - testuser1
      - testuser2

它表示标识为web的远程主机执行名为:Add users的任务,它使用user模块,这里面还用到了变量的用法,{{ item }},它最后会被值testuser1 和 testuser2 替换,总共添加两个用户。这个对应json结构如下:

[
    {
        "hosts": "web",
        "remote_user": "root",
        "gather_facts": true,
        "tasks": [
            {
                "name": "Add users",
                "user": "name={{ item }} state=present groups=wheel",
                "with_items": [
                    "testuser1",
                    "testuser2"
                ]
            }
        ]
    }
]
 
 
[root@localhost playbook]# ansible-playbook add_user.yml
 
PLAY [web] ********************************************************************************
 
TASK [Gathering Facts] ********************************************************************
ok: [192.168.40.73]
ok: [192.168.40.72]
 
TASK [Add users] **************************************************************************
changed: [192.168.40.73] => (item=testuser1)
changed: [192.168.40.72] => (item=testuser1)
changed: [192.168.40.72] => (item=testuser2)
changed: [192.168.40.73] => (item=testuser2)
 
PLAY RECAP ********************************************************************************
192.168.40.72              : ok=2    changed=1    unreachable=0    failed=0  
192.168.40.73              : ok=2    changed=1    unreachable=0    failed=0

再执行一遍,观察其输出与第一遍的差别:

[root@localhost playbook]# ansible-playbook add_user.yml
 
PLAY [web] ********************************************************************************
 
TASK [Gathering Facts] ********************************************************************
ok: [192.168.40.73]
ok: [192.168.40.72]
 
TASK [Add users] **************************************************************************
ok: [192.168.40.73] => (item=testuser1)
ok: [192.168.40.72] => (item=testuser1)
ok: [192.168.40.73] => (item=testuser2)
ok: [192.168.40.72] => (item=testuser2)
 
PLAY RECAP ********************************************************************************
192.168.40.72              : ok=2    changed=0    unreachable=0    failed=0  
192.168.40.73              : ok=2    changed=0    unreachable=0    failed=0

其中第二次的changed=0。

ansible是很多模块的执行是具有幂等性的,即ansible检测到远程主机已经满足了最终执行完的条件就不再执行命令。