尝试使用Vagrant在本地执行Ansible
首先创建 Vagrantfile
Vagrant.configure("2") do |config|
# Ansible実行用の仮想マシンを作成
config.vm.define "ansible" do |ansible|
ansible.vm.box = "generic/ubuntu2004"
ansible.vm.network "private_network", ip: "192.168.33.10"
ansible.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible-playbook.yml"
ansible.inventory_path = "ansible-inventory.ini"
end
end
# Rails環境構築用の仮想マシンを作成
config.vm.define "rails" do |rails|
rails.vm.box = "generic/ubuntu2004"
rails.vm.network "private_network", ip: "192.168.33.11"
rails.vm.provision "shell", path: "rails-setup.sh"
end
end
启动虚拟机
vagrant up ansible
vagrant up rails
连接到Ansible服务器的SSH
使用vagrant ssh ansible
创建ansible执行文件(以apache作为测试)。
库存.ini
[rails]
192.168.33.11
apache.yml:阿帕奇配置文件
---
- name: Configure web server
hosts: rails
become: true
tasks:
- name: Install Apache web server
apt:
name: apache2
state: present
- name: Start Apache web server
service:
name: apache2
state: started
执行Ansible任务
vagrant@ubuntu2004:~$ ansible-playbook -i i apache.yml
PLAY [Configure web server] ********************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************
ok: [192.168.33.11]
TASK [Install Apache web server] ***************************************************************************************************************************************************************
changed: [192.168.33.11]
TASK [Start Apache web server] *****************************************************************************************************************************************************************
ok: [192.168.33.11]
PLAY RECAP *************************************************************************************************************************************************************************************
192.168.33.11 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0