Ansible Contd…
Ansible loops and variables in inventory
we are supposed to install the following softwares into a node
Adding lists to ini inventory formats and executing playbook
inventory
[local]
localhost packages='["git", "nano", "tree", "net-tools"]'
---
- name: install utilities
hosts: all
tasks:
- name: install utilities
ansible.builtin.package:
name: "{{ item }}"
state: present
become: yes
loop: "{{ packages }}"
lets try writing the inventory in yaml. Refer Here for the changes
inventory in ini file
[appservers]
20.121.55.193 java_package_name=java-11-openjdk-devel
172.31.24.180 java_package_name=openjdk-11-jdk
[appservers:vars]
user_name=tomcat
group_name=tomcat
user_home=/opt/tomcat
user_shell=/bin/false
tomcat_version="10.1.4"
tomcat_major_version="10"
tomcat_service_name=tomcat.service
same inventory in yaml file
---
all:
children:
appservers:
hosts:
20.121.55.193:
java_package_name: java-11-openjdk-devel
172.31.24.180:
java_package_name: openjdk-11-jdk
vars:
user_name: tomcat
group_name: tomcat
user_home: /opt/tomcat
user_shell: /bin/false
tomcat_version: "10.1.4"
tomcat_major_version: "10"
tomcat_service_name: tomcat.service
It is not a good idea to define variables in inventory, variables should be treated seperately. Optimizing inventory Refer Here and Refer Here for organizing group and host variables.
Refer Here for the changes done and screen shot below for output
Ansible Special Variables
ansible while executing has lot of special variables defined by ansible Refer Here
after fixing packages Refer Here