Ansible部署与实施PlayBook

Ansible部署与实施PlayBook

计算机名称 IP 角色
workstation 192.168.182.130 管理
servera 192.168.182.131 被管理
serverb 192.168.182.132 被管理
serverc 192.168.182.133 被管理

部署

控制节点

官方文档

Ansible易于安装。只需要安装到要运行它的一个或多个控制节点上。由Ansbile管理的主机不需要安装Ansible。

控制节点需要安装Python3(3.5以上)或python2(2.7以上)

bash 复制代码
yum install python36 -y
yum install epel-release -y
yum install ansible -y 
bash 复制代码
#使用setup模块验证版本
[root@workstation ~]# ansible -m setup localhost | grep ansible_python_version
        "ansible_python_version": "2.7.5",

受管主机

官方文档

bash 复制代码
yum install python36 -y

构建清单

定义清单

清单定义ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以时多个组的成员。清单还可以设置应用带它所定义的主机和组的变量。

静态清单指定受管主机

静态清单文件是指定ansible目标受管主机的文本文件(INI/YAML).

ansible相关文章

配置资产文件inventory.ini

bash 复制代码
[root@workstation ~]# cat inventory.ini
[my_servers]
servera
serverb
serverc

主机名解析

bash 复制代码
[root@workstation ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.182.131 servera
192.168.182.132 serverb
192.168.182.133 serverc

实施PlayBook

官方文档

运行PlayBook

ansible-playbook 命令可用于运行playboo。该命令在控制节点运行,要运行的playbook的名称作为参数传递。

实例

yaml 复制代码
[root@workstation ~]# cat webserver.yml
---
- name: setup web server
  hosts: my_servers
  tasks:
  - name: latest httpd
    yum:
      name: httpd
      state: latest

在my_server下的servera安装

bash 复制代码
[root@workstation ~]# ansible-playbook -i inventory.ini  webserver.yml --limit servera

PLAY [setup web server] ************************************************************************************

TASK [Gathering Facts] *************************************************************************************
ok: [servera]

TASK [latest httpd] ****************************************************************************************

changed: [servera]

PLAY RECAP *************************************************************************************************
servera                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

检查

bash 复制代码
[root@servera ~]# systemctl start httpd

[root@servera ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-07-19 06:24:23 EDT; 53s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 15578 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─15578 /usr/sbin/httpd -DFOREGROUND
           ├─15579 /usr/sbin/httpd -DFOREGROUND
           ├─15580 /usr/sbin/httpd -DFOREGROUND
           ├─15581 /usr/sbin/httpd -DFOREGROUND
           ├─15582 /usr/sbin/httpd -DFOREGROUND
           └─15583 /usr/sbin/httpd -DFOREGROUND

Jul 19 06:24:23 servera systemd[1]: Starting The Apache HTTP Server...
Jul 19 06:24:23 servera httpd[15578]: AH00558: httpd: Could not reliably determine the server's fully...sage
Jul 19 06:24:23 servera systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

执行playbook前,最好进行验证,确保内容预防正确无误

bash 复制代码
[root@workstation ~]# ansible-playbook --syntax-check webserver.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: my_servers

playbook: webserver.yml

这样说明格式正确

修改webserver.yml

yaml 复制代码
[root@workstation ~]# cat webserver.yml
---
- name: setup web server
  hosts: my_servers
  tasks:
  - name:latest httpd
    yum:
      name: httpd
      state: latest
bash 复制代码
[root@workstation ~]# ansible-playbook --syntax-check webserver.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/root/webserver.yml': line 6, column 8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - name:latest httpd
    yum:
       ^ here

说明出现问题

-C选项对playbook执行空运行。会使Ansible报告在执行该playbook时将会发生什么更改并不会对受管主机进行任何更改。

bash 复制代码
[root@workstation ~]# ansible-playbook -C webserver.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: my_servers

PLAY [setup web server] ************************************************************************************
skipping: no hosts matched

PLAY RECAP *************************************************************************************************
相关推荐
yuezhilangniao2 小时前
Ansible基础 ansible入门 针对不同python3版本 - 含 Terraform 入门联动
运维·自动化·ansible
Koma_zhe2 天前
【Ansible开源自动化运维工具】别再手动装监控了,Ansible能让上百台机器同时搞定Node Exporter(1)
运维·开源·ansible
.柒宇.4 天前
RedHat10-Ansible部署Docker操作
docker·eureka·ansible
道清茗12 天前
【RH294知识点汇总】第 7 章 《 使用角色和 Ansible 内容集合简化 Playbook 》常见问题
ansible
何中应13 天前
Ansible安装&使用
运维·自动化·ansible·运维开发·自动化运维
王九思13 天前
Ansible 自动化运维基础—模板
运维·自动化·ansible
道清茗14 天前
【RH294知识点汇总】第 7 章 《 使用角色和 Ansible 内容集合简化 Playbook 》
java·前端·ansible
切糕师学AI14 天前
深入解析 Ansible:从入门到实践
ansible
王的宝库14 天前
【Ansible】变量与敏感数据管理:Vault 加密 + Facts 采集详解
笔记·学习·ansible
张32316 天前
Ansible拆分大型Playbook
linux·ansible