Ansible在macOS上的安装部署

一、安装 Ansible(使用 Homebrew)

  1. 安装 Homebrew(如果尚未安装):
bash 复制代码
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. 使用 Homebrew 安装 Ansible:
bash 复制代码
brew install ansible
  1. 安装完成后通过 ansible --version来验证

二、Ansible 配置文件

Ansible 的主要配置文件是 /etc/ansible/ansible.cfg,但也可以在项目目录中创建一个 ansible.cfg 文件来覆盖默认配置。

在配置文件中,可以设置各种选项,如 inventory 的位置、SSH 连接参数、行动插件、回调插件等。

python 复制代码
[defaults]
host_key_checking = False
inventory = ../../inventory
log_path = ansible.log

三、Ansible的脚本Playbook

python 复制代码
- hosts: localhost 或者 需要远程连接的主机名
  gather_facts: no
  vars:
    ansible_become_pass: "XXX"
  tasks:
  - name: delete folder1
    file:
     name: "想要被删除的文件地址1"
     state: absent

  - name: delete folder1
    file:
     name: "想要被删除的文件地址2"
     state: absent
   
# hosts 指定了要管理和配置的主机组
# gather_facts 为 no时,Ansible 不会在执行 play 之前自动运行 setup 模块来收集事实。这可以节省时间,尤其是当你管理的节点很多,或者当你知道你不需要使用这些事实时。
# ansible_become_pass 指定了在执行 command 任务时使用的密码
# tasks 则包含了一组有序的任务列表,其中包括了安装和启动 Apache 两个任务。
# state: absent 用于指示 Ansible 应该删除或移除一个对象。这个参数通常与 file、user、group、service 等模块一起使用,这些模块可以管理文件系统、用户、组和服务。

ansible-playbook的命令

python 复制代码
ansible-playbook playbook.yml

执行一次

以上是localhost 执行结果

四、Ansible 主机清单(Inventory)

ansible_ssh_host # 要连接的主机名

ansible_ssh_port # 端口号,默认22

ansible_ssh_user # ssh连接时默认使用的用户名

ansible_ssh_pass # ssh连接时的密码

ansible_sudo_pass # 使用sudo连接用户时的密码

ansible_ssh_private_key_file # 秘钥文件如果不想使用ssh-agent管理时可以使用此选项

ansible_shell_type # shell类型,默认sh

ansible_connection # SSH连接类型:local、ssh、paramiko在ansible 1.2之前默认paramiko

ansible_python_interpreter # 用来指定Python解释器的路径,同样可以指定ruby、Perl的路径

在搭建Jenkins的机器的 /etc/hosts 中配置远程主机的IP与Host,这样Inventory中就只需要写Host name。

group_vars 下面的yml 内容:

ansible_user:

ansible_connection:

ansible_port:

ansible_password:

ansible_ssh_pass:

host_vars下面的yml内容:

ansible_host:

hosts 内容:

group_vars下面的yml name

host_vars 下面的yml name

下面是远程测试示例

示例test_remote.yaml

bash 复制代码
- hosts: MacBookAir
  become: yes
  become_user: root 
  vars:
    ansible_user: MacTest
    ansible_ssh_password: xxx
    ansible_become_password: xxx
  
  tasks:
    - name: Delete the directory
      file:
        path: /Users/mactest/Desktop/folder1
        state: absent

在远程机器上配置 【允许远程电脑访问您的 Mac】【Setting- General- Sharing- Advanced】

本机验证是否可以连通远程机器

可以连通,再执行ansible命令ansible-playbook test_remote.yml

报错"msg": "rmtree failed: [Errno 1] Operation not permitted: '/Users/mactest/Desktop/folder1'"

提示没权限,因为远程机器上ssh 权限没打开

进入 System -> Privacy&Security -> Full Disk Access给ssh 权限

再次执行ansible 命令 运行OK

相关推荐
wwj888wwj13 小时前
Ansible基础(复习1)
linux·运维·ansible
zhensherlock16 小时前
Protocol Launcher 系列:Agenda 优雅组织你的想法与日程
javascript·macos·ios·typescript·node.js·mac·ipad
ipy2Tf8WP16 小时前
全程用 Claude Code 搓了一个 macOS 原生应用:SkillDeck
macos
eLIN TECE17 小时前
Mac安装Redis步骤
redis·macos·bootstrap
独隅19 小时前
在 MacOS 上部署 Keras 模型的全面指南
人工智能·macos·keras
SPC的存折19 小时前
12、Ansible安全加固
linux·运维·服务器·安全·ansible
Ashmcracker20 小时前
告别手写 Inventory:Terraform 与 Ansible 在 Azure 上的联动,动态清单自动接管云主机
ansible·azure·terraform
wd5i8kA8i1 天前
ansible介绍、按照及配置
ansible
zfoo-framework2 天前
[推荐]ansible在主控机执行实现多个worker机器免密登录
linux·运维·ansible
SPC的存折2 天前
10、Ansible 生产级故障排查与运维最佳实践
linux·运维·ansible