If you are trying to learn or use Ansible, here is a hands on Guide.
- Deploy a Vagrant and Ansible environment hosted on Github
- Create a Custom Role
- Share it on Ansible Galaxy
- Deploy via Ansible Galaxy
- Github Actions and Ansible
HasiCorp Vagrant Ansible Provisioning
ansible, where Ansible is executed on the Vagrant hostansible_local, where Ansible is executed on the Vagrant guest
Vagrant.configure("2") do |config|
# Use :ansible or :ansible_local to
# select the provisioner of your choice
config.vm.provision :ansible do |ansible|
ansible.playbook = "playbook.yml"
# ansible.playbook = "provisioning/playbook.yml"
end
end
Vagrant.configure("2") do |config|
config.vm.define "host1"
config.vm.define "host2"
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
"host1" => {"http_port" => 80,
"maxRequestsPerChild" => 808},
"host2" => {"http_port" => 303,
"maxRequestsPerChild" => 909}
}
end
end
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.define "machine1"
config.vm.define "machine2"
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.groups = {
"group1" => ["machine1"],
"group2" => ["machine2"],
"group3" => ["machine[1:2]"],
"group4" => ["other_node-[a:d]"], # silly group definition
"all_groups:children" => ["group1", "group2"],
"group1:vars" => {"variable1" => 9,
"variable2" => "example"}
}
end
end
ansible.groups = {
"group1" => ["machine1"],
"group2" => ["machine2", "machine3"],
"all_groups:children" => ["group1", "group2", "group3"]
}
Ansible on Host
Vagrant.configure("2") do |config|
#
# Run Ansible from the Vagrant Host
#
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
# Vagrant 1.7+ automatically inserts a different
# insecure keypair for each new VM created. The easiest way
# to use the same keypair for all the machines is to disable
# this feature and rely on the legacy insecure key.
# config.ssh.insert_key = false
#
# Note:
# As of Vagrant 1.7.3, it is no longer necessary to disable
# the keypair creation when using the auto-generated inventory.
N = 3
(1..N).each do |machine_id|
config.vm.define "machine#{machine_id}" do |machine|
machine.vm.hostname = "machine#{machine_id}"
machine.vm.network "private_network", ip: "192.168.77.#{20+machine_id}"
# Only execute once the Ansible provisioner,
# when all the machines are up and ready.
if machine_id == N
machine.vm.provision :ansible do |ansible|
# Disable default limit to connect to all the machines
ansible.limit = "all"
ansible.playbook = "playbook.yml"
end
end
end
end
Ansible on Local
Vagrant.configure("2") do |config|
# Run Ansible from the Vagrant VM
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "playbook.yml"
ansible.install_mode = "pip"
ansible.version = "2.2.1.0"
end
end
# Install Galaxy Roles in a path owned by root
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.provision "ansible_local" do |ansible|
ansible.become = true
ansible.playbook = "playbook.yml"
ansible.galaxy_role_file = "requirements.yml"
ansible.galaxy_roles_path = "/etc/ansible/roles"
ansible.galaxy_command = "sudo ansible-galaxy install --role-file=%{role_file} --roles-path=%{roles_path} --force"
end
end
Ansible Parallel Execution from a Guest
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.define "node1" do |machine|
machine.vm.network "private_network", ip: "172.17.177.21"
end
config.vm.define "node2" do |machine|
machine.vm.network "private_network", ip: "172.17.177.22"
end
config.vm.define 'controller' do |machine|
machine.vm.network "private_network", ip: "172.17.177.11"
machine.vm.provision :ansible_local do |ansible|
ansible.playbook = "example.yml"
ansible.verbose = true
ansible.install = true
ansible.limit = "all" # or only "nodes" group, etc.
ansible.inventory_path = "inventory"
end
end
end
The main advantage of the Ansible Local provisioner in comparison to the Ansible (remote) provisioner is that it does not require any additional software on your Vagrant host.
On the other hand, Ansible must obviously be installed on your guest machine(s).
Note: By default, Vagrant will try to automatically install Ansible if it is not yet present on the guest machine (see the install option below for more details).
- Ansible for DevOps: https://www.amazon.com.au/dp/B08FBLVVFG?ref_=mr_referred_us_au_au
- Getting started with Ansible 01 – Introduction – https://www.youtube.com/watch?v=3RiVKs8GHYQ&list=PLT98CRl2KxKEUHie1m24-wkyHpEsa4Y70
- Ansible Basics: Automation Technical Overview (DO007) – https://rhtapps.redhat.com/promo/course/do007?segment=0
- Ansible community documentation – https://docs.ansible.com/
- Ansible Tutorial for Beginners: Ultimate Playbook & Examples – https://spacelift.io/blog/ansible-tutorial
- https://github.com/NVISOsecurity/ansible-caldera
- https://holdmybeersecurity.com/2018/01/13/install-setup-mitre-caldera-the-automated-cyber-adversary-emulation-system/
- https://humble-raptor-f30.notion.site/Ansible-Arsenal-Automating-CIS-Compliance-on-Ubuntu-for-CyberPatriot-Points-3ed2b0d866c84b18bde41c3d19c79a38
- https://www.sans.org/cyber-security-courses/security-automation-offense-defense-cloud/
- https://arxiv.org/html/2407.16928v1
- https://github.com/iknowjason/AutomatedEmulation
- Ansible Vagrant Examples – https://github.com/geerlingguy/ansible-vagrant-examples?tab=readme-ov-file
- Ansible for DevOps – https://github.com/geerlingguy/ansible-for-devops
- Ansible Vagrant Guide – Vagrant Guide – https://docs.ansible.com/ansible/9/scenario_guides/guide_vagrant.html
- A Guide to Ansible with Vagrant – https://medium.com/@alokkavilkar/a-guide-to-ansible-with-vagrant-6fccf9c49591
- ChatGPT – https://chatgpt.com/c/67cc2dc3-a960-800e-b71f-bdd47eefa8d8
- Ansible Demo – https://github.com/RockAfeller2013/AnsibleDemo
- Ansible Tutorial https://spacelift.io/blog/ansible-tutorial#how-to-install-ansible