Ansible自动化工具的使用

Linux大区 1年前 (2022) 导航君
6 0 0

Ansible介绍与安装

介绍ansible

  1. Ansible目前是运维自动化工具中最简单(2021),已经被红帽收购(2015)。
  2. 使用ansible可以轻松对服务器进行初始化配置,安全基线配置,更新等操作。
  3. 它是基于SSH远程会话协议,不需要客户端程序。
  4. Ansible专用术语
  5. Control node 控制节点(控制端)
  6. Managed node 受控节点(被控制的)
  7. Inventory 主机清单(受控节点的列表,即是ip或域名或主机名)
  8. Modules 模块(特定的功能代码)
  9. Task 任务(在受控节点上执行的操作)
  10. Playbook 剧本(通过YAML语言编写的重复执行的任务列表)
  11. Roles 角色(从Ansible 1.2版本开始引入的新特性,做复杂的剧本任务,会有yaml例子文件)

Centos7下安装ansible

yum install epel-release -y
yum install  ansible  -y

查看版本

[root@master ~]# ansible  --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

设置主机清单

Ansible服务的主配置文件

优先级 文件位置
高 ./ansible.cfg
中 ~/.ansible.cfg
低 /etc/ansible/ansible.cfg #默认存在

受控主机的列表

cat /etc/ansible/hosts
[test01]          #平台、环境
192.168.31.161    #centos7-game-01
[test02]          #平台、环境
192.168.31.162    #centos6-game-01

初步配置

默认是使用公钥方式连接受控主机
关闭SSH协议的指纹验证和使用root用户连接
vi /etc/ansible/ansible.cfg ##打开以下两个注释
host_key_checking = False #关闭SSH协议的指纹验证
remote_port = 22 ##可以自定义受控主机ssh端口
remote_user = root ##使用root用户连接

显示出受管主机的信息

# ansible-inventory --graph
@all:
  |--@test01:
  |  |--192.168.31.161
  |--@test02:
  |  |--192.168.31.162
  |--@ungrouped:

运行临时命令

使用ansible-doc -l 命令列出所有的模块信息

[root@master ~]# ansible-doc  -l
fortios_router_community_list                                 Configure community lists in Fortinet's FortiOS and FortiGate              
azure_rm_devtestlab_info                                      Get Azure DevTest Lab facts                                                
ecs_taskdefinition                                            register a task definition in ecs                                          
avi_alertscriptconfig                                         Module for setup of AlertScriptConfig Avi RESTful Object                   
tower_receive                                                 Receive assets from Ansible Tower                                          
netapp_e_iscsi_target                                         NetApp E-Series manage iSCSI target configuration                          
azure_rm_acs                                                  Manage an Azure Container Service(ACS) instance                            
fortios_log_syslogd2_filter                                   Filters for remote system server in Fortinet's FortiOS and FortiGate       
junos_rpc                                                     Runs an arbitrary RPC over NetConf on an Juniper JUNOS device              
na_elementsw_vlan                                             NetApp Element Software Manage VLAN                                        
pn_ospf                                                       CLI command to add/remove ospf protocol to a vRouter                       
pn_snmp_vacm                                                  CLI command to create/modify/delete snmp-vacm                              
cp_mgmt_service_sctp                                          Manages service-sctp objects on Check Point over Web Services API          
onyx_ospf                                                     Manage OSPF protocol on Mellanox ONYX network devices                      
icx_command                                                   Run arbitrary commands on remote Ruckus ICX 7000 series switches

常用模块

模块名称 模块作用
ping 检查受管节点主机网络是否能够联通。
yum 安装、更新及卸载软件包。
yum_repository 管理主机的软件仓库配置文件。
template 复制模板文件到受管节点主机。
copy 新建、修改及复制文件。
user 创建、修改及删除用户。
group 创建、修改及删除用户组。
service 启动、关闭及查看服务状态。
get_url 从网络中下载文件。
file 设置文件权限及创建快捷方式。
cron 添加、修改及删除计划任务。
command 直接执行用户指定的命令。
shell 直接执行用户指定的命令(支持特殊字符)。
debug 输出调试或报错信息。
mount 挂载硬盘设备文件。
filesystem 格式化硬盘设备文件。
lineinfile 通过正则表达式修改文件内容。
setup 收集受管节点主机上的系统及变量信息。
firewalld 添加、修改及删除防火墙策略。
lvg 管理主机的物理卷及卷组设备。
lvol 管理主机的逻辑卷设备。

  • ansible命令常用的语法
    ansible 受管主机节点 -m 模块名称 [-a 模块参数]
    ansible命令常用参数
    参数 作用
    -k 手动输入SSH协议密码
    -i 指定主机清单文件
    -m 指定要使用的模块名
    -M 指定要使用的模块路径
    -S 使用su命令
    -T 设置SSH协议连接超时时间
    -a 设置传递给模块的参数
    —version 查看版本信息
    -h 帮助信息
  • 示例:简单ping受控主机
    # ansible all -m ping   ##全部机器,即是主机清单里面的机器
    # ansible test01 -m ping ##按平台或环境
    # ansible 192.168.31.161 -m ping ##按ip单个机器

示例2:远程执行命令

# ansible all -m shell  -a "yum install vim  -y"

剧本文件实战

Ansible服务的剧本(playbook)文件采用YAML语言编写
剧本文件的结构由4部分组成:
target:用于定义要执行剧本的主机范围。
variable:用于定义剧本执行时要用到的变量。
task:用于定义将在远程主机上执行的任务列表。
handler:用于定义执行完成后需要调用的后续任务。

mkdir  /etc/ansible/yaml
cat >> /etc/ansible/yaml/packages.yml <<EOF
---
- name: 安装软件包
  hosts: test01,test02
  tasks:
          - name: one
            yum:
                    name: vim
                    state: latest
EOF
# ansible-playbook /etc/ansible/yaml/packages.yml
ok和changed表示执行及修改成功

创建及使用角色

加载系统内置角色

# yum install -y rhel-system-roles

查看角色以及目录

# ansible-galaxy list
# /usr/share/ansible/roles
- rhel-system-roles.timesync, (unknown version)

角色相关的yaml例子剧本

cp /usr/share/doc/rhel-system-roles-1.0.1/timesync/example-single-pool-playbook.yml /etc/ansible/yaml/timesync.yml
cat /etc/ansible/yaml/timesync.yml
---
- hosts: "{{ targets }}"
  vars:
    timesync_ntp_servers:
      - hostname: 2.pool.ntp.org
        pool: yes
        iburst: yes
  roles:
    - rhel-system-roles.timesync
vi /etc/ansible/yaml/timesync.yml  #修改去掉角色部分
---
- hosts: "{{ targets }}"
  vars:
    timesync_ntp_servers:
      - hostname: 2.pool.ntp.org
        pool: yes
        iburst: yes
ansible-playbook /etc/ansible/yaml/timesync.yml

从外部获取角色(第三方角色)
Ansible Galaxy 官网首页:​ ​https://galaxy.ansible.com​​
示例:# ansible-galaxy install nginxinc.nginx
1.创建新的角色apache

# cd /etc/ansible/roles
# ansible-galaxy init apache
- Role apache was created successfully
# Ansible角色目录结构及含义
# ll apache/
目录    含义
defaults    包含角色变量的默认值(优先级低)。
files    包含角色执行tasks任务时做引用的静态文件。
handlers    包含角色的处理程序定义。
meta    包含角色的作者、许可证、频台和依赖关系等信息。
README.md
tasks    包含角色所执行的任务。
templates    包含角色任务所使用的Jinja2模板。
tests    包含用于测试角色的剧本文件。
vars    包含角色变量的默认值(优先级高)。

使用新的角色

# cat >> /etc/ansible/yaml/roles.yml  <<EOF
---
- name: 调用自建角色
  hosts: all
  roles:
          - apache
EOF
# ansible-playbook /etc/ansible/yaml/roles.yml
小结下:复杂的操作还是远程执行脚本比较好

简单使用

复制
1.批量远程命令执行

# ansible all -m shell  -a "yum install vim  -y"

2.批量远程脚本执行

mkdir /etc/ansible/script/ 
cat > /etc/ansible/script/nginx-install.sh << EOF
echo "开始安装nginx" > /tmp/nginx-install.log
echo "安装nginx成功" >> /tmp/nginx-install.log
EOF
# ansible all -m script -a "/etc/ansible/script/nginx-install.sh"

3.批量分发文件

mkdir /etc/ansible/file
cat > /etc/ansible/file/copy-test-txt.yml << EOF
---
- name: 拷贝本地文件到受控机器上
  hosts: all
  tasks:
    - name:
      copy:
        src: /etc/ansible/file/test.txt
        dest: /root/a/test.txt
        owner: root
        group: root
        mode: 0644
EOF
ansible-playbook  /etc/ansible/file/copy-test-txt.yml
版权声明:导航君 发表于 2022年6月27日 上午8:23。
转载请注明:Ansible自动化工具的使用 | 第八网址导航

相关文章

暂无评论

暂无评论...