毒侠风云 發表於 2025-7-12 00:00:00

在CentOS 7中安装与配置Ansible的详细图文教程

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、引言<ul class="second_class_ul"><li>1、Centos 7.9简介</li><li>2、Ansible简介</li></ul></li><li>二、安装Ansible<ul class="second_class_ul"><li>1、服务器准备</li><li>2、添加yun源</li><li>3、安装Ansible</li></ul></li><li>三、配置Ansible<ul class="second_class_ul"><li>1、配置Ansible主机清单</li><li>2、配置 SSH 免密登录</li><li>3、配置 Ansible 配置文件(可选)</li><li>4、测试 Ansible 配置</li><li>5、使用 Ansible 执行任务</li></ul></li><li>四、常用命令<ul class="second_class_ul"><li>1、ansible &mdash; 执行单条命令</li><li>2、ansible-playbook &mdash; 执行剧本</li><li>3、ansible-galaxy &mdash; 管理 Ansible 角色</li><li>4、ansible-inventory &mdash; 查看库存文件</li><li>5、ansible-vault &mdash; 加密敏感数据</li><li>6、ansible-doc &mdash; 查看模块文档</li></ul></li></ul></div><p class="maodian"></p><h2>一、引言</h2>
<p class="maodian"></p><h3>1、Centos 7.9简介</h3>
<p>CentOS 7.9 是基于 Red Hat Enterprise Linux (RHEL) 7.9 源代码构建的稳定、可靠的企业级 Linux 发行版,提供了最新的安全更新和性能改进,适用于服务器和桌面环境。它支持广泛的硬件平台,并集成了多种网络服务和虚拟化技术,如 KVM 和 Docker,适合用于部署 Web 服务器、数据库和其他企业应用。通过 yum 包管理器和 systemd 系统和服务管理器,CentOS 7.9 提供了简便的软件包管理和系统配置体验,是构建和管理稳定 IT 基础设施的理想选择。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="690" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HF93J.png" width="1200" /></p>
<p class="maodian"></p><h3>2、Ansible简介</h3>
<p>Ansible 是一个简单而强大的自动化工具,用于配置管理、应用部署和任务编排,它通过 SSH 连接远程主机,默认使用基于 YAML 的易读 playbook 来定义自动化任务,无需在客户端安装额外的代理软件,支持广泛的模块和插件以扩展功能,适用于大规模 IT 基础设施的自动化操作。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="636" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HFa41.png" width="1200" /></p>
<p class="maodian"></p><h2>二、安装Ansible</h2>
<p class="maodian"></p><h3>1、服务器准备</h3>
<p>这里准备了三台服务器,一台作为control节点,两台作为node节点</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="480" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG0943.png" width="1200" /></p>
<p class="maodian"></p><h3>2、添加yun源</h3>
<p>在三台机器上分别执行</p>
<div class="dxycode"><pre class="brush:bash;">wget -O /etc/yum.repos.d/CentOs-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache</pre></div>
<p class="maodian"></p><h3>3、安装Ansible</h3>
<p>只在control节点执行</p>
<div class="dxycode"><pre class="brush:bash;"># sudo yum install -y ansible</pre></div>
<p>版本检查</p>
<div class="dxycode"><pre class="brush:bash;"># ansible --version</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="294" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG0433.png" width="1200" /></p>
<p class="maodian"></p><h2>三、配置Ansible</h2>
<p class="maodian"></p><h3>1、配置Ansible主机清单</h3>
<p>Ansible 需要配置目标主机的清单,默认情况下,Ansible 会使用 /etc/ansible/hosts 作为清单文件。</p>
<p>编辑该文件,定义被管理的节点(目标主机)。</p>
<div class="dxycode"><pre class="brush:bash;"># vim /etc/ansible/hosts</pre></div>
<p>在该文件中,添加类似以下内容来配置目标主机(你可以根据实际情况修改主机名或 IP 地址):</p>
<div class="dxycode"><pre class="brush:bash;">
192.168.40.101
192.168.40.102</pre></div>
<p> 是主机组的名称,可以根据实际需要进行命名。 在每个组下,列出被管理的主机或 IP 地址。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="735" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG1326.png" width="1200" /></p>
<p class="maodian"></p><h3>2、配置 SSH 免密登录</h3>
<p>Ansible 与目标主机通过 SSH 协议进行通信。为了避免每次操作时输入密码,通常配置 SSH 免密登录。</p>
<p>在控制节点上,使用 ssh-keygen 生成 SSH 公私钥对:</p>
<p>一路回车即可</p>
<div class="dxycode"><pre class="brush:bash;"># ssh-keygen -t rsa -b 2048</pre></div>
<p>然后,将生成的公钥复制到目标主机:</p>
<div class="dxycode"><pre class="brush:bash;"># ssh-copy-id root@192.168.40.101</pre></div>
<div class="dxycode"><pre class="brush:bash;"># ssh-copy-id root@192.168.40.102</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="685" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG1916.png" width="1200" /></p>
<p>完成后,测试免密登录是否成功:</p>
<div class="dxycode"><pre class="brush:bash;"># ssh 192.168.40.101
# ssh 192.168.40.102</pre></div>
<p>如果能够成功登录并无需输入密码,则 SSH 免密登录配置成功。</p>
<p class="maodian"></p><h3>3、配置 Ansible 配置文件(可选)</h3>
<p>Ansible 的配置文件 /etc/ansible/ansible.cfg 是可选的,可以根据需要进行定制化配置。常见的配置项包括:</p>
<p><strong>inventory:</strong>指定清单文件的路径。 <strong>remote_user:</strong>指定默认的远程用户。 <strong>become:</strong>是否启用 sudo 提权。</p>
<p>例如,编辑配置文件来设置默认的远程用户:</p>
<div class="dxycode"><pre class="brush:bash;"># vim /etc/ansible/ansible.cfg</pre></div>
<p>添加如下内容</p>
<div class="dxycode"><pre class="brush:bash;">
inventory = /etc/ansible/hosts
remote_user = root
become = yes
become_method = sudo</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="216" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG2B9.png" width="517" /></p>
<p class="maodian"></p><h3>4、测试 Ansible 配置</h3>
<p>安装和配置完成后,可以运行一个简单的命令来测试 Ansible 是否能够正确与目标主机通信。</p>
<p>运行 ping 模块,检查目标主机的连接:</p>
<div class="dxycode"><pre class="brush:bash;"># ansible all -m ping</pre></div>
<p>如果一切配置正确,应该返回类似以下的输出:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="376" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG2221.png" width="949" /></p>
<p class="maodian"></p><h3>5、使用 Ansible 执行任务</h3>
<p>使用 Ansible 可以执行简单的命令,比如在所有目标主机上查看系统时间:</p>
<div class="dxycode"><pre class="brush:bash;"># ansible all -m command -a "date"</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="364" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG3228.png" width="1033" /></p>
<p class="maodian"></p><h2>四、常用命令</h2>
<p class="maodian"></p><h3>1、ansible &mdash; 执行单条命令</h3>
<p>用于在单个主机或一组主机上执行简单的命令或任务。</p>
<div class="dxycode"><pre class="brush:bash;">ansible &lt;host-pattern&gt; -m &lt;module&gt; -a "&lt;module-args&gt;"</pre></div>
<p><strong>&lt;host-pattern&gt;:</strong>目标主机或主机组。 <strong>-m &lt;module&gt;:</strong>指定 Ansible 模块(如 ping、shell、copy 等)。 <strong>-a &quot;&lt;module-args&gt;&quot;:</strong>传递给模块的参数。</p>
<p>示例:<br />Ping 主机:</p>
<div class="dxycode"><pre class="brush:bash;">ansible all -m ping</pre></div>
<p>该命令会在所有目标主机上执行 ping 测试,确保它们可以正常访问。</p>
<p>执行命令:</p>
<div class="dxycode"><pre class="brush:bash;">ansible web_servers -m shell -a "uptime"</pre></div>
<p>在 web_servers 主机组上的所有主机执行 uptime 命令。</p>
<p class="maodian"></p><h3>2、ansible-playbook &mdash; 执行剧本</h3>
<p>用于执行定义好的 剧本(Playbook),剧本是由一组任务组成的 YAML 文件。</p>
<div class="dxycode"><pre class="brush:bash;">ansible-playbook &lt;playbook.yml&gt;</pre></div>
<p><strong>&lt;playbook.yml&gt;:</strong>指定要执行的 Playbook 文件。</p>
<p>示例:<br />执行 Playbook:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-playbook site.yml</pre></div>
<p>这会执行 site.yml 文件中定义的所有任务。</p>
<p>指定变量文件:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-playbook -i inventory.ini site.yml --extra-vars "@vars.json"</pre></div>
<p class="maodian"></p><h3>3、ansible-galaxy &mdash; 管理 Ansible 角色</h3>
<p>用于管理 Ansible 角色和集合。可以用来从 Ansible Galaxy 下载角色、创建新角色等。</p>
<div class="dxycode"><pre class="brush:bash;">ansible-galaxy &lt;command&gt; &lt;role-name&gt;</pre></div>
<p>常用子命令:<br />安装角色:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-galaxy install &lt;role-name&gt;</pre></div>
<p>创建角色:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-galaxy init &lt;role-name&gt;</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="1009" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG3232.png" width="1200" /></p>
<p class="maodian"></p><h3>4、ansible-inventory &mdash; 查看库存文件</h3>
<p>用于查看 Ansible 的库存文件(即目标主机列表),或动态生成目标主机清单。</p>
<div class="dxycode"><pre class="brush:bash;">ansible-inventory --list</pre></div>
<p>查看 JSON 格式的库存:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-inventory -i inventory.ini --list</pre></div>
<p>查看主机组列表:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-inventory -i inventory.ini --host &lt;hostname&gt;</pre></div>
<p class="maodian"></p><h3>5、ansible-vault &mdash; 加密敏感数据</h3>
<p>用于加密、解密文件或变量,常用于保护敏感信息(如密码、密钥等)。</p>
<div class="dxycode"><pre class="brush:bash;">ansible-vault &lt;command&gt; &lt;file&gt;</pre></div>
<p><strong>常用子命令:</strong></p>
<p>创建加密文件:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-vault create &lt;file&gt;</pre></div>
<p>编辑加密文件:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-vault edit &lt;file&gt;</pre></div>
<p>加密现有文件:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-vault encrypt &lt;file&gt;</pre></div>
<p>解密文件:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-vault decrypt &lt;file&gt;</pre></div>
<p>查看加密文件内容:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-vault view &lt;file&gt;</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="576" src="https://zhuji.jb51.net/uploads/allimg/20250709/2-250F91HG4a3.png" width="1200" /></p>
<p class="maodian"></p><h3>6、ansible-doc &mdash; 查看模块文档</h3>
<p>用于查看 Ansible 模块的文档,获取模块的使用说明和参数。</p>
<div class="dxycode"><pre class="brush:bash;">ansible-doc &lt;module-name&gt;</pre></div>
<p><strong>示例:</strong></p>
<p>查看 copy 模块的文档:</p>
<div class="dxycode"><pre class="brush:bash;">ansible-doc copy</pre></div>
<p>以上就是在CentOS 7中安装与配置【Ansible】的详细指南的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: 在CentOS 7中安装与配置Ansible的详细图文教程