自动化运维之Ansible
2026/9/8 9:48:15 网站建设 项目流程

所有主机配置解析

Ansible安装

[root@server1 ~]# yum install -y ansible-core

Ansible配置文件

[root@server1 ~]# cd /etc/ansible/
[root@server1 ansible]# ansible-config init --disabled > ansible.cfg

[root@server1 ansible]# vim hosts

[root@server1 ansible]# ansible-inventory --graph

Ansible Ad-hoc

语法

[root@ansible ~]# ansible <pattern> -m <module_name> -a <arguments>

参数详解

pattern: inventory文件里定义的主机组名,主机名,IP,别名等,all表示所有的主机,支持通配符,正则
: -冒号,多个组,组名之间用冒号隔开
web-组名或主机名中含web
webservers[0] - webservers组中的第一台主机, -1表示最后一台主机,支持切片():[1:2]表示组中的第二台和第三台
all所有
-m module_name:模块名称,默认为command
-a arguments:传递给模块的参数
-i file_name:指定inventory文件,默认为/etc/ansible/hosts
-k :询问密码.如果没有传密钥到被管理端,必须要接此选项
配置过程:
1.在控制节点安装ansible
2.编辑主机清单文件,加入被管控节点
3.在控制节点生成密钥,把公钥传到所有的被控制节点(可选)

帮助信息查看

# ansible-doc -l #查看所有模块信息
# ansible-doc module_name #查看模块帮助信息

Ansible Modules

1.主机连通性

[root@server1 ansible]# ansible web -m ping -k

这样就说明我们的主机是连通状态的。接下来的操作才可以正常进行。

2. command模块

这个模块可以直接在远程主机上执行shell命令,并将结果返回本主机。

[root@server1 ansible]# ansible web -m command -a 'ss -ntl' -k

3. shell模块

[root@server1 ~]# ansible web -m shell -a 'cat /etc/passwd |grep "devops"'

4. copy模块

这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。

① 复制文件

[root@server1 ~]# ansible web -m copy -a 'src=~/hello dest=/data/hello'

② 给定内容生成文件,并制定权限

[root@server1 ~]# ansible web -m copy -a 'content="I am keer\n" dest=/root/name mode=666'

我们现在可以去查看一下我们生成的文件及其权限
[root@server1 ~]# ansible web -m shell -a 'ls -l /root/'

③ 关于覆盖

我们把文件的内容修改一下,然后选择覆盖备份
[root@server1 ~]# ansible web -m copy -a 'content="I am keerya\n" backup=yes dest=/root/name mode=666'
现在我们可以去查看一下
[root@server1 ~]# ansible web -m shell -a 'ls -l /root/'
可以看出,我们的源文件已经被备份,我们还可以查看一下name文件的内容
[root@server1 ~]# ansible web -m shell -a 'cat /root/name'

5. file模块

该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等

① 创建目录

[root@server1 ~]# ansible web -m file -a 'path=/data/app state=directory'

② 创建链接文件

[root@server1 ~]# ansible web -m file -a 'path=/data/bbb.jpg src=aaa.jpg state=link'

③ 删除文件

[root@server1 ~]# ansible web -m file -a 'path=/data/aaa.jpg state=absent'

6. fetch模块

该模块用于从远程某主机获取(复制)文件到本地。
有两个选项:
dest#用来存放文件的目录
src#在远程拉取的文件,并且必须是一个file,不能是目录
[root@server1 ~]# ansible web -m fetch -a 'src=/data/hello.txt dest=/root'
我们可以在本机上查看一下文件是否复制成功。要注意,文件保存的路径是我们设置的接收目录下的被管制主机ip目录

7. cron模块

该模块适用于管理cron计划任务的
其使用的语法跟我们的crontab文件中的语法一致
[root@server1 ~]# ansible web -m cron -a 'name="ntp update every 5 min" minute="*/5" job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'
[root@server1 ~]# ansible web -m shell -a "crontab -l"

Ansible Playbook剧本

基本写法

server1上创建devops用户

[root@server1 ~]# useradd devops

[root@server1 ~]# su - devops

[devops@server1 ~]$ mkdir ansible
[devops@server1 ~]$ cd ansible/
[devops@server1 ansible]$ vim apache.yml

server2上创建devops用户

[root@server2 ~]# useradd devops
[root@server2 ~]# passwd devops

配置目标机 sudo 免密

[root@server2 ~]# echo 'devops ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/devops
[root@server2 ~]# chmod 440 /etc/sudoers.d/devops

server1上执行

[devops@server1 ansible]$ ansible-playbook apache.yml -k

[devops@server1 ansible]$ curl server2

前置配置

配置ssh免密

[devops@server1 ~]$ ssh-keygen

[devops@server1 ~]$ ssh-copy-id devops@192.168.247.143

配置主机清单

[devops@server1 ansible]$ vim hosts

生成Ansible 配置模板文件

[devops@server1 ansible]$ ansible-config init --disabled > ansible.cfg

修改配置文件

[devops@server1 ansible]$ vim ansible.cfg

让 Ansible 在执行任务时自动以 root身份运行


将 Ansible 的默认主机清单(Inventory)路径指定为当前工作目录下的./hosts文件

[devops@server1 ansible]$ ansible-inventory --graph

[devops@server1 ansible]$ ansible-playbook apache.yml

全局指定 Ansible 连接远程主机时使用的 SSH 登录用户为devops

[devops@server1 ansible]$ vim ansible.cfg

修改 Apache 监听端口为 8080

[devops@server1 ansible]$ vim apache.yml

[devops@server1 ansible]$ ansible-playbook apache.yml

handlers

[devops@server1 ansible]$ vim apache.yml

对比项之前的配置现在的配置
端口修改后处理❌ 无后续操作,端口变更不会立即生效✅ 通过notify触发 handler 重启服务
服务重启机制不存在新增handlers段,定义重启任务
幂等性每次运行都执行所有 taskhandler 仅在 notify 被触发时才执行

Ansible 的Facts(事实)机制

[devops@server1 ansible]$ vim apache.yml

ansible_hostname是 Ansible 在连接目标主机时自动采集的内置变量,值为该主机的短主机名

gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到

部署后自动化验证

在 web 服务器部署完成后,从控制节点本地发起 HTTP 请求,验证网站是否可正常访问,并输出页面内容

[devops@server1 ansible]$ vim apache.yml

[devops@server1 ansible]$ ansible-playbook apache.yml

变量定义及使用vars

[devops@server1 ansible]$ vim apache.yml

对不同主机部署不同端口的 Apache 服务

[devops@server1 ansible]$ vim hosts

[devops@server1 ansible]$ vim apache.yml

[devops@server1 ansible]$ ansible-playbook apache.yml

[devops@server1 ansible]$ curl node1:8080

lineinfile改为template

[devops@server1 ansible]$ vim apache.yml

[devops@server1 ansible]$ scp server2:/etc/httpd/conf/httpd.conf .

[devops@server1 ansible]$ mv httpd.conf httpd.conf.j2

[devops@server1 ansible]$ vim httpd.conf.j2

[devops@server1 ansible]$ ansible-playbook apache.yml

group variable(组变量)

[devops@server1 ansible]$ vim hosts

[devops@server1 ansible]$ ansible-playbook apache.yml

部署keepalived

[root@server1 ~]# passwd devops

[root@server1 ~]# su - devops

[devops@server1 ~]$ ssh-copy-id 192.168.247.142

[root@server1 ~]# visudo

[devops@server1 ansible]$ vim hosts

[devops@server1 ansible]$ vim keepalved.yml

[root@server2 ~]# yum install -y keepalived

[devops@server1 ansible]$ scp server2:/etc/keepalived/keepalived.conf .

[devops@server1 ansible]$ mv keepalived.conf keepalived.conf.j2

[devops@server1 ansible]$ ansible-playbook keepalived.yml

[root@server1 ~]# systemctl stop keepalived

[root@server2 ~]# tail -f /var/log/messages

添加用户

[devops@server1 ansible]$ vim user.yml

[devops@server1 ansible]$ ansible-playbook user.yml

添加主机解析

[devops@server1 ansible]$ vim lineinfile.yml

[devops@server1 ansible]$ ansible-playbook lineinfile.yml

魔法变量

[devops@server1 ansible]$ vim hosts

[devops@server1 ansible]$ vim groups.yml

[devops@server1 ansible]$ cp /etc/hosts test.j2
[devops@server1 ansible]$ vim test.j2

[devops@server1 ansible]$ ansible-playbook groups.yml

[devops@server1 ansible]$ cat /etc/hosts

Haproxy

[devops@server1 ansible]$ vim hosts

停掉之前的keepalived服务

[devops@server1 ansible]$ vim keepalived.yml

[devops@server1 ansible]$ ansible-playbook keepalived.yml --tags=t1

[devops@server1 ansible]$ cp /etc/haproxy/haproxy.cfg .

[devops@server1 ansible]$ mv haproxy.cfg haproxy.cfg.j2

[devops@server1 ansible]$ vim haproxy.cfg.j2

[devops@server1 ansible]$ vim haproxy.yml

---
- hosts: haproxy,webservers
tasks:
- name: deploy haproxy
block:
- name: install haproxy
ansible.builtin.yum:
name: haproxy
state: present

- name: configure the haproxy
ansible.builtin.template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
notify: restart service haproxy

- name: enable service haproxy
ansible.builtin.service:
name: haproxy
state: started
enabled: yes
when: "'haproxy' in group_names"

- name: deploy apache
block:
- name: Install the Apache
ansible.builtin.yum:
name: httpd
state: present

- name: Ensure the default Apache port is {{ http_port }}
ansible.builtin.template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart service httpd

- name: Start service httpd
ansible.builtin.service:
name: httpd
state: started
enabled: yes

- name: create index.html
ansible.builtin.copy:
content: "{{ ansible_hostname }}\n"
dest: /var/www/html/index.html
when: "'webservers' in group_names"

handlers:
- name: restart service haproxy
ansible.builtin.service:
name: haproxy
state: restarted

- name: restart service httpd
ansible.builtin.service:
name: httpd
state: restarted

[devops@server1 ansible]$ ansible-playbook haproxy.yml

成功之后可以验证负载均衡

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询