2.0 KiB
2.0 KiB
EX294 1.2: Variables and facts
Give a group and one host their own value of a variable, print facts from a playbook, and add a custom fact to a node.
Before you start
This VM is the control node. The lab installs ansible-core and creates
the user greg (password Lab-Pass-1), who has an SSH key. Work as greg
(su - greg as root), in /home/greg/ansible: its ansible.cfg reads the
inventory inventory, which has node1 and node2 in the group
webservers, and logs in as ansible. There are no variable files yet.
The managed nodes node1 (10.99.1.11) and node2 (10.99.1.12) are
AlmaLinux 10 containers with their own users and packages, reached over
SSH by name. On each, the user ansible accepts greg's key and may use
sudo without a password. For a root shell on a node,
sudo podman exec -it node1 bash.
Tasks
- Set
http_portto 80 for the webservers group and to 8080 for node2 alone, and print it from a playbook,/home/greg/ansible/port.yml, that runs on the webservers. node1 prints 80 and node2 8080; with-e http_port=9000both print 9000. - Write
/home/greg/ansible/facts.yml, which prints each managed node's distribution, its version and its memory in MiB from facts. Each host's numbers matchansible HOST -m setup -a 'filter=ansible_memtotal_mb'(andansible_distribution*). - Write a custom fact on node1 in
/etc/ansible/facts.d/role.fact, with the section[general]androle=web.ansible node1 -m setup -a 'filter=ansible_local'shows"role": "web"undergeneral.
Hints
group_vars/webservers.ymlandhost_vars/node2.ymlnext to the inventory;ansible-inventory --host node2shows what a host gets.ansible.builtin.debugwithmsg: "{{ http_port }}"orvar: http_port.ansible_facts['distribution'],['distribution_version']and['memtotal_mb'].- The fact file is plain INI and must not be executable (an executable
.factis run as a program). Write it withansible.builtin.copyandbecome: true, or by hand on the node.