Note the uri would change for lxc or remote connections.
After the inventory set up test it
ansible-inventory --inventory kvm.yml --list
You should see the info about your guest. If you get red or an error verify that the guest agent is running and that it has access to the guest-exec and guest-file commands.
A good way to test the guest agent is with a guest execute of ls
virsh qemu-agent-command “Name of your Guest VM” '{"execute": "guest-exec", "arguments": { "path": "/usr/bin/ls", "arg": [ "/" ], "capture-output": true }}'
If you have issues take a look at my previous video where I provisioned the guest https://thenathan.net/2022/09/29/virt-builder/.
Once your inventory is working you can connect with the console.
ansible-console --inventory kvm.yml -l “Name of your Guest VM”
I used -l to limit because I only had the one host running.
Now we are in the console and can run some commands to test
ls
cat /etc/redhat-release
whoami
exit
This is the very simple playbook I used to test playbooks running against the inventory. t
$ cat dnf_update_reboot.yml
---
- hosts: alma8
gather_facts: false
become: true
any_errors_fatal: yes
tasks:
- name: DNF update the system
dnf:
name: "*"
state: latest
- name: Install the latest version of yum-utils
dnf:
name: yum-utils
state: latest
- name: Reboot required
command: "/usr/bin/needs-restarting -r"
register: reboot_required
ignore_errors: True
changed_when: False
failed_when: reboot_required.rc == 2
- name: Rebooting
reboot:
post_reboot_delay: 60
throttle: 1
when: reboot_required.rc == 1 and ansible_facts ['virtualization_role'] != 'host'boot_required.rc == 1 and ansible_facts ['virtualization_role'] != 'host'
You can run the playbook with the ansible-playbook command
ansible-playbook -i kvm.yml dnf_update_reboot.yml
ansible-console –inventory kvm.yml -l “Name of your Guest VM”
In this video I cover some play books I have written to patch my RedHat based CentOS VM’s. The playbooks will enable EPEL, verify some packages/applications I use are installed, run a Yum or DNF update and reboot if a reboot is required.
In this video I will cover what is Ansible. How it works. Pros and Cons and where to find out more information.
So what is Ansible? Ansible is an agentless automation and configuration tool. Ansible can also be used for workflow automation. It can do anything from provision servers to run one off commands on many host or host groups in parallel.
Ansible
is written in python and uses ssh to execute commands on different
systems. Ansible uses a inventory to group and nest systems.
In
Ansible you uses modules to create task that do things. You put task
in playbooks which are written YAML.
An
example of a one off command would be
ansible server1 -a "hostname"
Note: the default module is shell. I also already have my .ansible.cfg and inventory set up which Ill cover in a future video.
Now an example of a playbook I have written todo the same thing
nathan@thenathan:~$ cat ansible/hostname.yml
— – name: Get Hostname gather_facts: False hosts: all tasks: – name: Run Shell command hostname shell: “hostname”
Lets cover some of the pros and cons. A pro and a con is its againtless. Its nice that you can use ssh and not have to for the most part install software or make firewall changes for off ports. Thats also a con because if a system is down or times out the change or command wont be applied unless you rerun it.
Ansible inventory is fantastic. Its very powerfull in the way you can group and nest systems which makes it a great tool if you want to run adhock shell commands. Which also makes Ansible a good fit for running along side other tools like chef.
Ansible does a good job of gathering information (AKA Facts) off systems which you can use to customize commands or configs.
Playbooks use yaml. Its up to you if that’s a pro or con.
Ansible has great documentation which is another pro. I find the documentation and examples better then salt stack and chef. You can find the documentation at https://docs.ansible.com/This is the first in a series of videos I want to do on Ansible. For the code and command I used in this video check out my blog post on thenathan.net Pleas like, share and subscribe.
In this video I install CentOS 7 Minimal from ISO in QEMU/KVM.
After the install I configure the network to start on boot, bring up the network interface do a OS update and install some necessary packages like bind-utils, net-tools and bash-completion.
I also config tuned with the virtual-guest profile, enable sshd at boot and set sudo no password.