DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest DevOps and CI/CD Topics

article thumbnail
An Introduction to Ansible Inventory
In this post, you will learn how to set up a basic Ansible Inventory. Besides that, you will learn how to encrypt sensitive information by means of Ansible Vault. Enjoy! 1. Introduction In a previous post, you learned how to set up an Ansible test environment. In this post, you will start using the test environment. Just as a reminder, the environment consists of one Controller and two Target machines. The Controller and Target machines run in a VirtualBox VM. Development of the Ansible scripts is done with IntelliJ on the host machine. The files are synchronized from the host machine to the Controller by means of a script. In this blog, you will create an inventory file. The inventory file contains information about the Target machines in order for the Controller to locate and access the machines for executing tasks. The inventory file will also contain sensitive information such as the password being used for accessing the Target machines. In a second part of this blog you will solve this security problem by means of Ansible Vault. The files being used in this blog are available in the corresponding git repository at GitHub. 2. Prerequisites The following prerequisites apply to this blog: You need an Ansible test environment, see a previous blog how to set up a test environment; If you use your own environment, you should know that Ubuntu 22.04 LTS is used for the Controller and Target machines and Ansible version 2.13.3; Basic Linux knowledge. 3. Create an Inventory File The Ansible Controller will need to know some information about the Targets in order to be able to execute tasks. This information can be easily provided by means of an inventory file. Within an inventory, you will specify the name of the Target, its IP address, how to connect to the Target, etc. Take a look at the Ansible documentation for all the details. In this section, you will experiment with some of the inventory features. By default, Ansible will search for the inventory in /etc/ansible/hosts but you can also provide a custom location for the inventory when executing Ansible. That is what you will do in this section. Create in the root of the repository a directory inventory and create an inventory.ini file. Add the following content to the file: Plain Text target1 target2 [targets] target1 target2 [target1_group] target1 [target2_group] target2 [target_groups:children] target1_group target2_group The first two lines contain the names for the Target machines. You can give this any name you would like, but in this case, you just call them target1 and target2. When you want to address several machines at once, you can create groups. A group is defined between square brackets followed by the list of machines belonging to this group. In the inventory above, you can recognize group targets which contains target1 and target2. This group is not really necessary, because by default a group all exists which is equal to the group targets in this case. The groups target1_group and target2_group are for illustrative purposes and do not make much sense because they contain only one machine. However, in real life, you can imagine to have groups for application machines, database machines, etc. or you might want to group machines by region for example. You can also define a group of groups like target_groups. You need to add :children to the definition and then you can combine several groups into a new group. The group target_groups consists of the group target1_group and target2_group. This actually means that group target_groups consists of machines target1 and target2. 4. Define Variables The inventory file you created just contains names of machines and groups. But this information is not enough for Ansible to be able to locate and connect to the machines. One approach is to add variables in the inventory file containing this information. A better approach is to define a directory host_vars containing subdirectories for each machine containing the variables. Ansible will scan these directories in order to find the variables for each machine. You can also define variables for the groups. In this case, you create a directory group_vars. Create in directory inventory a directory host_vars containing the directories target1 and target2. The directory tree of directory inventory looks as follows: Plain Text ├── host_vars │ ├── target1 │ └── target2 └── inventory.ini Create in directory target1 a file vars with the following contents: YAML ansible_host: 192.168.2.12 ansible_connection: ssh ansible_user: osboxes ansible_ssh_pass: osboxes.org The variables defined here are some special variables for Ansible to be able to locate and connect to the machine: ansible_host: the IP address of the target1 machine; ansible_connection: the way you want to connect to target1; ansible_user: the system user Ansible can use to execute tasks onto the machine; ansible_ssh_pass: the password of the ansible_user. Do not store passwords in plain text in real life! This is only done for testing purposes and a proper solution is provided later on this post. Note that you can also define these variables in the inventory file on the same line as where you define the name of the machine. In this case, the variables need to be defined as key=value (with an equal sign and not with a colon). Add a vars file to directory target2 with similar contents but with the connection values for target2. 5. Test Inventory Settings Now it is time to do some testing in order to verify whether it works. Start the Controller and the two Target machines. Synchronize the files you created to the Controller machine and navigate in a terminal window to the MyAnsiblePlanet directory. Connect once manually to both Target machines so that the SSH fingerprint is available onto the Controller machine, otherwise you will get an error message when Ansible tries to connect to the Target machines. Shell $ ssh [email protected] $ ssh [email protected] With the following command, you will ping the target1 machine. The command consists of the following items: ansible: The Ansible executable; target1: The name of the machine where you want to execute the task. This corresponds to the name in the inventory; -m ping: Execute the ping command; -i inventory/inventory.ini: The path of the inventory file. The command to execute: Shell $ ansible target1 -m ping -i inventory/inventory.ini target1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } The response indicates a success. Execute the same command but for the target2 machine. The result should also be a success response. Just like you can execute a task on a single machine, you can also execute a task on a group. Execute the command for the targets group: Shell $ ansible targets -m ping -i inventory/inventory.ini target2 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } target1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } As you can see, the command is executed on both Target machines, as expected. Execute the command for the other groups as well. 6. Encrypt Password Now that you know that the inventory configuration is working as expected, it is time to get back to the password in plain text problem. This can be solved by using Ansible Vault. Ansible Vault probably deserves its own blog, but in this section you just going to apply one way of encrypting sensitive information. The encryption will be done for the target1 machine. Create in directory inventory/target1 a file vault and copy the ansible_ssh_pass variable to this vault file. Change the variable name from ansible_ssh_pass into vault_ansible_ssh_pass. YAML vault_ansible_ssh_pass: osboxes.org In the vars file, you replace the plain text password with a reference to this new vault_ansible_ssh_pass variable using Jinja2 syntax. Note that it is also required to add double quotes around the reference. YAML ansible_host: 192.168.2.12 ansible_connection: ssh ansible_user: osboxes ansible_ssh_pass: "{{ vault_ansible_ssh_pass }" Encrypt the vault file with password itisniceweather (or whatever password you would like). Shell $ ansible-vault encrypt inventory/host_vars/target1/vault New Vault password: Confirm New Vault password: Encryption successful The vault file contents is now encrypted. Plain Text $ANSIBLE_VAULT;1.1;AES256 34353662643861663663363161366239343633636561663564653030663134623266323363353433 6233383939396335343639623165306330393031383836320a616430336132643638333862363965 36303837313239386566633332326165663336363464623437383638333936613038663366343833 3737316665323230620a343163356138656535363837646566643962393366353266613462616437 32346531613637396666623864333330643261366139306162373038633636633934326165616438 6565363034333137623539643539666234386339393965663362 The password you have used for encrypting the file should be saved in a password manager. Ansible will need it to decrypt the password. Try to execute the ping command for target1 like you did before. Shell $ ansible target1 -m ping -i inventory/inventory.ini ERROR! Attempting to decrypt but no vault secrets found This fails because Ansible cannot decrypt the password field. Add the parameter --ask-vault-pass to the command in order that Ansible asks you for the vault password. Shell $ ansible target1 -m ping -i inventory/inventory.ini --ask-vault-pass Vault password: target1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } And now it works again! This is a better way for handling sensitive information in your Ansible files. There are several more ways of handling sensitive information. As said before, Ansible Vault deserves its own blog. In the meanwhile, more information can be found in the Ansible documentation. 7. Conclusion In this post, you learned the basics of an Ansible Inventory file and you learned how to encrypt sensitive information in the inventory file. You have gained the basic skills to start setting up an inventory file yourself for your environment.
September 27, 2022
by Gunter Rotsaert DZone Core CORE
· 4,106 Views · 1 Like
article thumbnail
Using Dynamic Build Agents to Automate Scaling in Jenkins
In this post, we look at 2 popular ways to set up dynamic scaling from start to finish, with Kubernetes and Amazon Web Services (AWS).
September 27, 2022
by Andy Corrigan
· 4,207 Views · 2 Likes
article thumbnail
Google Cloud - For AWS Professionals
Learning a cloud platform takes a long time. If you are familiar with AWS, this is the overview you need to get started quickly to understand Google Cloud.
September 27, 2022
by Ranga Karanam
· 5,475 Views · 1 Like
article thumbnail
How to Set Jenkins Pipeline Environment Variables
This is an extensive guide to Jenkins pipeline environment variables. Find out how Jenkins set environment variables for all your projects.
September 27, 2022
by Praveen Mishra
· 3,868 Views · 14 Likes
article thumbnail
Jenkins Security Tips
This post looks at some methods and tools to keep your Jenkins instance safe, secure and protect those using it. For an open, customizable platform.
September 27, 2022
by Andy Corrigan
· 5,638 Views · 1 Like
article thumbnail
What Is Kubernetes HPA and How Can It Help You Save on the Cloud?
Guide to Kubernetes HPA with a real-life example showing how this autoscaling feature works and how to use it for cost-saving.
September 26, 2022
by Valdas Rakutis
· 3,854 Views · 2 Likes
article thumbnail
Architecture of Kubernetes
This article will discuss all about the Architecture and the working of Kubernetes, and you will understand things related to Kubernetes.
September 26, 2022
by Jaydeep Patil
· 4,916 Views · 3 Likes
article thumbnail
When Should You Go For Microservice Architecture?
It's the purpose that drives action and not the path that's chosen. Let's revisit some core concepts of Microservice Architecture.
September 26, 2022
by Komal J Prabhakar
· 8,416 Views · 1 Like
article thumbnail
Data Management Patterns for Microservices
Learn common database patterns for microservices, explore CQRS (including how it differs from CRUD), and, finally, how it can be combined with event sourcing.
September 25, 2022
by Abhishek Gupta DZone Core CORE
· 25,283 Views · 4 Likes
article thumbnail
Why You Should Be Deploying Postgres on Kubernetes
Here's why Kubernetes is the best platform to make Postgres production-ready.
September 24, 2022
by Sylvain Kalache
· 5,336 Views · 2 Likes
article thumbnail
Kubernetes Architecture Diagram
This article will explain each Kubernetes architecture example step, the entire structure, what it’s used for, and how to use it.
September 23, 2022
by Alfonso Valdes
· 17,499 Views · 18 Likes
article thumbnail
Everything You Need to Know About CI/CD Pipeline
This article explains what continuous integration in DevOps is, what CI/CD pipelines are, CI/CD tools, their benefits, and best practices.
September 23, 2022
by Susmitha Vakkalanka
· 7,553 Views · 1 Like
article thumbnail
Data Migration from AWS DocumentDB to Atlas on AWS
AWS Database Migration Service provides heterogeneous migrations between different platforms. See the migration scenario between DocumentDB and MongoDB Atlas on AWS.
Updated September 22, 2022
by Travis Van
· 9,285 Views · 2 Likes
article thumbnail
A Service Mesh for Kubernetes
You can use Linkerd as a service mesh with Kubernetes, helping to manage communications for cloud-native apps and services while also feeding you data.
Updated September 22, 2022
by Alex Leong
· 14,825 Views · 5 Likes
article thumbnail
The Benefits of Containerization
Learn what containers are, the key benefits of containers for software development, and why you might consider adding them to your DevOps processes.
September 22, 2022
by Terence Wong
· 7,072 Views · 1 Like
article thumbnail
How to Set Up Trivy Scanner in GitLab CI: The Complete Guide
Trivy is an open-source container image scanner used by DevOps and security teams known for its reliability and ease of use.
September 22, 2022
by Florian Pialoux
· 4,794 Views · 1 Like
article thumbnail
Using Hazelcast in Spring Boot Running on Kubernetes
A simple Spring Boot application with Data JPA (Hibernate) using embedded Hazelcast for the second-level cache with the support of the Kubernetes cluster.
September 22, 2022
by Ruslan Appazov
· 12,984 Views · 1 Like
article thumbnail
Microservices vs APIs: Understand the Difference
Many of them have confusion between Microservices and APIs. In this article, you will get a clear understanding of both.
September 22, 2022
by Krunal Vyas
· 11,609 Views · 13 Likes
article thumbnail
An Introduction to DevOps
This post takes a surface-level look at the parts that make up DevOps. We explore the concepts, tools, and unique roles.
September 22, 2022
by Andy Corrigan
· 5,560 Views · 1 Like
article thumbnail
How to Solo Stake on Ethereum After The Merge
Read about how to solo stake on Ethereum after The Merge, considerations and things to be aware of leading up to The Merge, and how to stay up to date.
September 21, 2022
by Michael Bogan DZone Core CORE
· 5,031 Views · 2 Likes
  • Previous
  • ...
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×