DevOps Classroomnotes 09/Jan/2023

Ansible Contd…

Ansible loops and variables in inventory

  • we are supposed to install the following softwares into a node
    • git
    • tree
    • nano
    • net-tools
  • Adding lists to ini inventory formats and executing playbook
  • inventory
[local]
localhost packages='["git", "nano", "tree", "net-tools"]'
  • playbook
---
- 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
    Preview
  • 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
    Preview

Ansible Special Variables

  • ansible while executing has lot of special variables defined by ansible Refer Here
    Preview
  • after fixing packages Refer Here
    Preview

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Wordpress Development Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube