Ansible的基础

Ansible

ansible安装

在Redhat8 上安装ansible

使用epel-release库

1.使用阿里的镜像源
[root@ansible ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2595  100  2595    0     0  13657      0 --:--:-- --:--:-- --:--:-- 13586
​
2. 安装epel-release库
[root@anasible ~]# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
​
3. 安装ansible
[root@anasible ~]# yum install -y ansible
​
3. 确认安装:
[root@ansible ~]# ansible --version 
ansible 2.9.25
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

 

ansible配置文件

配置文件

**/etc/ansible/ansible.cfg** 主配置文件,配置ansible工作特性(一般无需修改)

**/etc/ansible/hosts** 主机清单(将被管理的主机放到此文件)

**/etc/ansible/roles/** 存放角色的目录

程序

  • /usr/bin/ansible 主程序,临时命令执行工具

  • /usr/bin/ansible-doc 查看配置文档,模块功能查看工具

  • /usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台

  • /usr/bin/ansible-playbook 定制自动化任务,编排剧本工具

  • /usr/bin/ansible-pull 远程执行命令的工具

  • /usr/bin/ansible-vault 文件加密工具

  • /usr/bin/ansible-console 基于Console界面与用户交互的执行工具

 

ansible主要配置文件

  • Ansible 配置文件/etc/ansible/ansible.cfg (一般保持默认)

[root@ansible ~]# vim  /etc/ansible/ansible.cfg  
​
[defaults]
​
#inventory     = /etc/ansible/hosts      # 主机列表配置文件
#library       = /usr/share/my_modules/  # 库文件存放目录
#remote_tmp    = $HOME/.ansible/tmp      # 临时py命令文件存放在远程主机目录
#local_tmp     = $HOME/.ansible/tmp      # 本机的临时命令执行目录  
#forks         = 5                       # 默认并发数,同时可以执行5次
#sudo_user     = root                    # 默认sudo 用户
#ask_sudo_pass = True                    # 每次执行ansible命令是否询问ssh密码
#ask_pass      = True                    # 每次执行ansible命令是否询问ssh口令
#remote_port   = 22                      # 远程主机的端口号(默认22)
​


建议优化项:

host_key_checking = False               # 检查对应服务器的host_key,建议取消注释
log_path=/var/log/ansible.log           # 日志文件,建议取消注释
module_name   = command                 # 默认模块

 

ansible主机清单

  1. Inventory 主机清单

  2. ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名

  3. 默认的inventory file为/etc/ansible/hosts

  4. inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成

  • /etc/ansible/hosts文件格式

  • inventory文件遵循INI文件风格,中括号中的字符为组名。

  • 可以将同一个主机同时归并到多个不同的组中;

  • 此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明

  • ntp.magedu.com 不分组,直接加

例如:

   [webservers]     webservers组
​
   www1.magedu.com:2222  可以指定端口
   www2.magedu.com
​
   
​
   [dbservers]
​
   db1.magedu.com
   db2.magedu.com
   db3.magedu.com

如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机

示例:

   [websrvs]
​
   www[1:100].example.com   ip: 1-100
​
​
   [dbsrvs]
​
   db-[a:f].example.com     dba-dbff

 

Ansible系列命令

Ansible系列命令

$ ansible ansible-doc ansible-playbook ansible-vault ansible-console ansible-galaxy ansible-pull
​
$ ansible-doc: 显示模块帮助
   ansible-doc [options] [module...]
       -a            显示所有模块的文档
       -l, --list    列出可用模块
       -s, --snippet 显示指定模块的playbook片段(简化版,便于查找语法)


示例:
  ansible-doc -l     列出所有模块
  ansible-doc ping   查看指定模块帮助用法
  ansible-doc -s ping 查看指定模块帮助用法  

ansible通过ssh实现配置管理、应用部署、任务执行等功能,建议配置ansible端能基于密钥认证的方式联系各被管理节点

 

ansible命令语法

$ ansible <host-pattern> [-m module_name] [-a args]
​
ansible +被管理的主机(ALL) +模块  +参数
​
   --version                显示版本
   -m module                指定模块,默认为command
   -v                       详细过程 –vv -vvv更详细
   --list-hosts             显示主机列表,可简写 --list
   -k, --ask-pass            提示输入ssh连接密码,默认Key验证
   -C, --check               检查,并不执行
   -T, --timeout=TIMEOUT      执行命令的超时时间,默认10s
   -u, --user=REMOTE_USER     执行远程执行的用户
   -b, --become               代替旧版的sudo切换
    --become-user=USERNAME    指定sudo的runas用户,默认为root
   -K, --ask-become-pass      提示输入sudo时的口令

 

ansible的Host-pattern

ansible的Host-pattern
​
匹配主机的列表
   All :表示所有Inventory中的所有主机
​
       ansible all –m ping
​
   * :通配符
       ansible "*" -m ping  (*表示所有主机)
       ansible 192.168.1.* -m ping
       ansible "*srvs" -m ping       
​
   或关系 ":"
​
       ansible "websrvs:appsrvs" -m ping
       ansible “192.168.1.10:192.168.1.20” -m ping
​
   逻辑与 ":&"
       ansible "websrvs:&dbsrvs" –m ping   
       在websrvs组并且在dbsrvs组中的主机
​
   逻辑非 ":!"
       ansible 'websrvs:!dbsrvs' –m ping
       在websrvs组,但不在dbsrvs组中的主机
       注意:此处为单引号
​
   综合逻辑
       ansible 'websrvs:dbsrvs:&appsrvs:!ftpsrvs' –m ping
​
   正则表达式
       ansible "websrvs:&dbsrvs" –m ping
       ansible "~(web|db).*\.magedu\.com" –m ping

 

ansible命令执行过程

ansible命令执行过程

  1. 加载自己的配置文件 默认/etc/ansible/ansible.cfg

  2. 加载自己对应的模块文件,如command

  3. 通过ansible将模块或命令生成对应的临时py文件,

  4. 并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件

  5. 给文件+x执行

  6. 执行并返回结果

  7. 删除临时py文件,sleep 0退出

执行状态:

  • 绿色:执行成功并且不需要做改变的操作

  • 黄色:执行成功并且对目标主机做变更

  • 红色:执行失败

ansible使用示例

示例
   以wang用户执行ping存活检测
       ansible all -m ping -u wang -k
​
   以wang sudo至root执行ping存活检测
       ansible all -m ping -u wang -k -b
​
   以wang sudo至mage用户执行ping存活检测
       ansible all -m ping -u wang -k -b --become-user=mage
​
   以wang sudo至root用户执行ls
       ansible all -m command -u wang -a 'ls /root' -b --become-user=root -k -K
​
   ansible ping模块测试连接
   ansible 192.168.38.126,192.168.38.127 -m ping -k 

 

ansible的免密

针对部署少量的服务,可以使用SSH免密的方式让ansible母机可以直接远程操控子机

配置SSH免密

# 生成密钥
[root@ansible ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YrOLZcZcLd8XaRavKFoT+iA60z0k5S2F0+hAmZH0gC8 root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|     o+=         |
|    . =o         |
|     o  .+    .  |
|    E o =.o    + |
|     .+=S+o   = .|
|     +.==+.o + o |
|     .O+o.= o o  |
|    o*.oo= o .   |
|    oo. ...      |
+----[SHA256]-----+
​
#授权子机
[root@ansible ~]# ssh-copy-id 192.168.1.107
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.107 (192.168.1.107)' can't be established.
ECDSA key fingerprint is SHA256:abAQ3Mvy1GW7gRO1XwIptGILQf2L3G0gJApXVPRFpkE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.107's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh '192.168.1.107'"
and check to make sure that only the key(s) you wanted were added.
posted @ 2021-06-10 10:43  isicman  阅读(73)  评论(0编辑  收藏  举报