目录
Please enable Javascript to view the contents

简记 Ansible 使用

 ·  ☕ 3 分钟

1. ubuntu 18.04 install Ansible

software-properties-common package installed. This software will make it easier to manage this and other independent software repositories:

1
2
apt update
apt install software-properties-common

Then add the Ansible PPA by typing the following command:

1
apt-add-repository ppa:ansible/ansible

install Ansible

1
2
apt update
apt install ansible

2. config ssh access to ansible-hosts

3. ansible 注意

  1. ansible 主机需要安装 pip install netaddr,因为 playbook 依赖
  2. ansible 主机需要安装 apt install sshpass ,因为使用密码连接
  3. ansible 主机需要安装 apt install zip unzip 因为 playbook 需要解压文件
  4. ansible 主机需要配置 ansible 的host_key_checking(/etc/ansible/ansible.cfg)为False,因为所有主机都是第一次连接,还没有主机指纹在known_hosts中,需要忽略

4. 测试Ansible-Hosts连通性方法

  1. vi /etc/ansible/hosts追加以下内容,IP地址即为要测试的机器
1
2
[test]
10.8.2.95 ansible_ssh_user=root ansible_ssh_pass=Qloud@123 docker_registry=true
  1. 执行以下命令,用ping模块进行测试。test为上面为hosts配置的主机组名字。
1
ansible -i /etc/ansible/hosts test -m ping

5. Ansible 的一些约定

  1. inventory 主机清单
    -i参数指定主机清单。例如ansible-playbook -i inventory.py nomad.yaml

    1.1 静态inventory,通过静态配置文件inventory.json配置。

    1.2 动态inventory,通过动态生成脚本inventory.py生成。

  2. roles目录必须与playbook文件同级,并且文件夹名必须为roles。playbook文件中通过roles下的各个文件夹名字引用role
    例如

    1
    2
    3
    4
    5
    6
    
    - name: install mirrors and initialization         
      hosts: all                                       
      roles:                                           # roles/
        - yum-mirrors                                  #   |-- yum-mirrors/
        - initialization                               #   |-- initializations/
        - docker-registry                              #   |-- docker-registry/
  3. 单个role目录结构说明。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    .
    ├── install_nginx.yml              # playbook 文件和 roles 目录应该处于平级目录
    └── roles                          # 存放所有角色的的目录
        └── nginx                      # nginx 角色
        │   ├── defaults               # 设定默认变量
        │   │   └── main.yml
        │   ├── files                  # 存放由 copy 或 script 模块等调用的文件
        │   │   └── index.html
        │   ├── handlers               # 触发器任务
        │   │   └── main.yml
        │   ├── meta                   # 定义当前角色的特殊设定及其依赖关系
        │   ├── tasks                  # 定义任务,它是 role 的基本元素
        │   │   └── main.yml
        │   ├── templates              # templates 模块查找所需要模板文件的目录
        │   │   └── nginx.conf.j2
        │   └── vars                   # 定义变量
        │       └── main.yml
        └── php                        # php 角色
            └── ...
  4. playbook文件的定义中,优先级: role > task; 如果task-A必须要在某role-B之前执行,需要将task-A封装为role-A,放到role-B之前。

  5. task中常用的模块

    模块名称作用
    ping检查指定节点机器是否还能连通
    command执行命令模块,可以不填,是ansible默认的模块,命令中无法使用变量,管道。
    shell启动一个子shell执行命令,执行的命令中有管道或者变量,就需要使用shell模块。
    script将本机的shell脚本在被管理主机运行
    copy将本机复制文件到远程位置
    service用于控制远程主机的服务
    cron用于管理定时任务
    file用于远程主机上的文件操作
    yum使用yum包管理器来管理软件包
    user、groupuser、goup分别请求:useradd、userdel、usermod;groupadd、groupdel、groupmod
    filesystem在块设备上创建文件系统
    get_url下载文件
    synchronize同步文件模块

Reference

Install Ansible on Ubuntu 18.04

官方文档
中文文档

ansible Ad-Hoc命令集

金恒博客

分享

Hex
作者
Hex
CloudNative Developer