Main playbook
# ansible/site.yml
---
- name: Configure srv100
hosts: srv100
tasks:
- name: Ansible demo
ansible.builtin.debug:
msg: "Hello from host {{ ansible_fqdn }}!"
Let’s try out the example playbook!
Infrastructure Automation
HOGENT applied
computer science
Bert Van Vreckem & Thomas Parmentier
2024-2025
Run this script twice:
$ sudo ./setup-server.sh
$ sudo ./setup-server.sh
useradd: user 'admin' already exists
Domain Specific Language (DSL)
body common control {
bundlesequence => { "install_packages" };
inputs => { "libraries/cfengine_stdlib.cf" };
}
bundle agent install_packages {
vars:
"desired_packages"
slist => { "httpd", "mod_ssl" };
packages:
"$(desired_packages)"
package_policy => "add",
package_method => generic;
}
package { 'httpd':
ensure => installed,
}
service { 'httpd':
ensure => running,
}
vmlab
environment> cd infra-labs-23-34-USERNAME/vmlab
> vagrant up control
> vagrant ssh control
> cd /vagrant/ansible
In vagrant-hosts.yml
(before the control
node!):
and run vagrant up srv100
Try this:
> ansible -i inventory.yml srv100 -m ping
> ansible -i inventory.yml srv100 -m setup
# ansible/site.yml
---
- name: Configure srv100
hosts: srv100
tasks:
- name: Ansible demo
ansible.builtin.debug:
msg: "Hello from host {{ ansible_fqdn }}!"
Let’s try out the example playbook!
[vagrant@control ansible]$ ansible-playbook -i inventory.yml site.yml
PLAY [Configure srv100] *******************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [srv100]
TASK [Ansible demo] *******************************************************************************
ok: [srv100] => {
"msg": "Hello from host srv100!"
}
PLAY RECAP *******************************************************************************
srv100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
> ansible-galaxy install bertvv.rh-base
Add a section roles:
to site.yml
:
and run the playbook again.
After the first run:
PLAY RECAP *******************************************************************************
srv100 : ok=33 changed=13 unreachable=0 failed=0 skipped=19 rescued=0 ignored=0
After the second one:
PLAY RECAP *******************************************************************************
srv100 : ok=31 changed=0 unreachable=0 failed=0 skipped=19 rescued=0 ignored=0
Idempotency at work!
Role behaviour can be changed by setting (role) variables. See the README!
host_vars/srv001.yml
group_vars/servers.yml
group_vars/all.yml