Ansible官方文档open in new window

软件安装

需要安装epel源, 参考阿里云epel源open in new window

# 安装epel源(两种方式)
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install epel-release

# yum安装Ansible
yum install ansible -y

# 验证安装, 并查看版本
ansible --version

配置详解

配置文件模版

# 主配置文件模版
/etc/ansible/ansible.cfg 

# 主机清单模版
/etc/ansible/hosts

# 存放角色目录
/etc/ansible/roles/

配置文件详解

  • Ansible配置文件查找顺序

      1. ANSIBLE_COMFIG 变量定义的配置文件
      1. 当前目录下 ./ansible.cfg
      1. 当前用户家目录下 ~/ansible.cfg
      1. /etc/ansible/ansible.cfg
  • ansible.cfg文件详解

[defaults]
# 主机列表配置文件
inventory     = /etc/ansible/hosts  

# 库文件存放目录
library       = /usr/share/my_modules/ 

# 临时py命令文件存放在远程主机目录
remote_tmp    = $HOME/.ansible/tmp     

# 本机的临时命令执行目录  
local_tmp     = $HOME/.ansible/tmp

# 默认并发数,同时可以执行5次
forks         = 5

# 默认sudo 用户
sudo_user     = root

# 每次执行ansible命令是否询问ssh密码
ask_sudo_pass = True

# 每次执行ansible命令是否询问ssh口令   
ask_pass      = True

# 远程主机的端口号(默认22)
remote_port   = 22        

# 检查对应服务器的指纹密钥,建议取消
host_key_checking = False 

# 日志文件,建议取消注释
log_path=/var/log/ansible.log

# 默认模块
module_name   = command
[privilege_escalation]
# 连接后是否切换用户
become = true
# 切换用户方式 默认sudo
become_method = sudo
# 切换用户名
become_user = root
# 是否需要输入密码
become_ask_pass = false      
  • hosts文件详解

# []为组名
[test1]
node02
192.168.3.43

[test2]
# 连续用:分隔
node[02:03]

# 嵌套组 test3为组名 嵌套test1
[test1:test3]
# 密码认证格式
node03 ansible_port=22 ansible_user=root ansible_password='142857'
# 定义test4组的公共变量批量设置密码登录信息
[test4:vars]
ansible_port=22
ansible_user=root
ansible_passowrd='142857'

[test4]
node[02:03]

Ansible命令详解

Ansible 导出服务器信息为json
Ansible ad-hoc 命令集51CTOopen in new window

  • Ansible常用模块

模块作用模块名模块参数
检查节点连通性ping
执行命令 默认模块command
执行命令 支持特殊符号shellchdir=/目录名 : 切换工作目录
removes=/文件名 : 文件存在不执行
分发脚本并执行script
文件目录管理filepath=/路径 : 目标路径
state=touch : 创建文件
state=directory : 创建文件夹
state=link : 创建软链接
state=hard : 创建硬链接
state=absent : 删除
owner=用户名 : 设置所有者
group=组名 : 设置所属组
mode=777 : 设置权限
拷贝模块copysrc=/主机文件 : 设置源文件
dest=/tmp/ : 设置目标位置
backup=yes : 如果存在则先备份
服务管理模块systemdname=nginx : 服务名
state=started : 启动服务
state=stoped : 关闭服务
state=restarted : 重启服务
state=reloaded : 重新加载配置文件
YUM包管理模块yumname=软件名\软件包路径 : 指定包
state=installed : 安装软件
state=removed : 卸载软件
state=lastest : 更新软件
文件下载模块git_urlurl='链接' : 文件下载链接
dest=/路径 : 文件下载路径
  • Ansible返回状态

    • 绿色: 命令已执行 状态没有发生改变
    • 黄色: 命令已执行 状态发生改变
    • 紫色: 命令已执行 建议使用规范模块
    • 蓝色: 命令已执行 打印详细执行过程
    • 红色: 命令执行失败
  • Ansible命令实例

ansiblesAnsible 基本操作命令

# 格式
ansible all/主机集合 -m 模块名 -a "命令"
    -k : 使用密码远程
    -i : 指定主机列表文件

# 查看主机列表
ansible all/主机集合 --list-hosts

# 使用ping模块
ansible 主机集合 -m ping

# 使用command默认模块 不支持 管道符 重定向
ansible 主机集合 -a "ip a s ens33"
ansible 主机集合 -m command -a "ip a s ens33"

# 使用shell模块
ansible 主机集合 -m shell -a "ps ef | grep anon"
# 使用chdir切换工作目录 chdir
ansible 主机集合 -m shell -a "chdir=/tmp touch my.txt"
# 文件存在不执行 create
ansible 主机集合 -m shell -a "touch /tmp/abc.txt create=/tmp/abc.txt" 
# 文件不存在不执行 removes
ansible 主机集合 -m shell -a "chdir=/tmpunzip abc.zip removes=/tmp/abc.txt" 

# 使用script模块 分发脚本并执行
ansible 主机集合 -m script -a "/控制端主机脚本名"

# 使用file模块
ansible 主机集合 -m file -a "/"
# 新建文件 state类别 touch创建文件
ansible 主机集合 -m file -a "path=/tmp/abc.txt state=touch"
# 新建文件夹 directory创建文件夹
ansible 主机集合 -m file -a "path=/tmp/dir state=directory"
# 修改权限 owner所有者 group所属组 mode权限代码
ansible 主机集合 -m file -a "path=/tmp/dir owner=anon group=anon mode=777"
# 删除文件/目录 absent 删除
ansible 主机集合 -m file -a "path=/tmp/文件目录名 state=absent"
# 创建软链接 src源文件 path链接地址 link创建软链接 hard创建硬链接
ansible 主机集合 -m file -a "src=/etc/hosts path=/tmp/hosts state=link"

# 使用copy模块 src主机文件 dest目标主机目录(加文件名为改名)
# 复制文件
ansible 主机集合 -m copy -a "src=/主机文件 dest=/目标主机位置/"
# 如果目标主机存在先备份
ansible 主机集合 -m copy -a "src=/主机文件 dest=/tmp/ backup=yes"
# 写入内容并拷贝
ansible 主机集合 -m copy -a "content='hello' desc=/tmp/abc.txt"

# 使用fetch模块 将远程文件拷贝到本地
ansible 主机集合 -m fetch -a "src=/tmp/abc.txt dest=/tmp/"

# 使用lineinfile模块 修改单个文件的单行内容
# 在文件中添加一行内容(默认添加到最后)
ansible 主机集合 -m lineinfile -a "path=/tmp/text line='hello'"
# 在文件test行后添加内容
ansible 主机集合 -m lineinfile -a "path=/tmp/text line='hello' insertafter='test'"
# 替换文件中的hello为nihao
ansible 主机集合 -m lineinfile -a "path=/tmp/text regexp='hello' replase='nihao'"

# 使用systemd管理模块
# 启动服务
ansible hosts -m systemd -a "name=nginx state=started"
# 关闭服务
ansible hosts -m systemd -a "name=nginx state=stopped"
# 重新加载配置文件
ansible hosts -m systemd -a "name=nginx state=reloaded"
# 重启服务
ansible hosts -m systemd -a "name=nginx state=restarted"

# 使用yum模块
# yum安装软件
ansible hosts -m yum -a "name=nginx state=installed"
# yum卸载软件
ansible hosts -m yum -a "name=nginx state=removed"
# yum更新软件
ansible hosts -m yum -a "name=nginx state=lastest"

# 使用git_url模块
ansible hosts -m git_url -a "url='https://mirros.aliyun.com' dest=/tmp/tools/"

# 使用mount模块(一般挂载nfs)
# 挂载
ansible hosts -m mount -a "fstype=nfs src=192.168.1.1:/wwwdata path=/mnt state=mountd"
# 卸载不修改fstab
ansible hosts -m mount -a "fstype=nfs src=192.168.1.1:/wwwdata path=/mnt state=umountd"
# 卸载修改fstab
ansible hosts -m mount -a "fstype=nfs src=192.168.1.1:/wwwdata path=/mnt state=absent"

# 使用cron模块
# 添加定时任务
ansible hosts -m cron -a "name=test state=present minute='*/2' job='/bin/ntpdate ntp1.aliyun.com &>/dev/null'"
# 删除定时任务
ansible hosts -m cron -a "name=test state=absent"
# 注释不删除定时任务 disable=no为取消注释
ansible hosts -m cron -a "name=test minute='*/2' job='/bin/ntpdate ntp1.aliyun.com &>/dev/null state=present disable=yes"

# 使用group模块 state=absent为删除组
ansible hosts -m group -a "name=www gid=2000 state=present"

# 使用user模块
ansible hosts -m user -a "name=www uid=1999 group=www shell=/bin/bash create_home=yes state=present"
  • Ansible-doc命令

ansible-doc 是查看 Ansible 模块说明命令

# 列出所有模块
ansible-doc -l

# 查看模块帮助 -为可选项 =为强制选项
ansible-doc yum

Ansible Playbook剧本详解

  • 语法格式YAML

    • hosts(主机)
    • tasks(任务)
    • variables(变量)
    • roles(角色)
  • 变量

剧本变量
---
# 主机集合
- hosts: db
  vars:
    file: /etc/hostname
  tasks:
    - name: test
      # 如果变量开头要加引号
      shell: echo {{ file }}

文件变量
# /tmp/vars_file.yml
file: /etc/hostname
node: /etc/hosts

# 剧本调用
---
- hosts: all
  vars_files: /tmp/vars_file.yml
  tasks:
    - name: test
      shell: echo {{ node }}
分组变量

在Ansible目录下创建目录 自动识别各分组变量

# 创建目录格式 目录名为主机集合分组
group_vars/
├── db
│   └── vars.yml
├── nfs
│   └── vars.yml
├── systemd
│   └── vars.yml
└── web
    └── vars.yml
facts变量 内置变量

Ansible运行剧本时系统收集服务器信息变量

# 查看ansible facts变量内容
ansible -i hosts all -m setup

# 常用fact变量
# 主机名
ansible_hostname
# 内存大小 MB
ansible_memtotal_mb
# cpu数量
ansible_processor_vcpus
# 默认网卡ip
ansible_default_ipv4.assress
# linux发行版本
ansible_distribution
  • 语法实例

---
# 主机集合
- hosts: all,test1
  # 任务(可以为多个)
  tasks:
    # 任务名
    - name: tasks name
    # 调用模块
    ping: 
    # 任务名
    -name: task name
    # 调用模块及命令
    shell: touch /tmp/abc.txt
- hosts: test2
  tasks: 
    -name: tasks name
    shell: touch /tmp/intex.txt
  • 剧本命令

# 运行剧本
ansible-playbook test.yml
# 指定主机集合
ansible-playbook -i hosts test.yml
# 检查语法
ansible-playbook test.yml -C