Ansible Variables
-
Variables in Ansible can be declared in
- Playbooks
- Inventory files
- Many other locations
- Lets write playbook to install tomcat Refer Here
Scenario: Installing tomcat9 on ubuntu 20.04
- Lets verify if the manual steps are working or not
- Step -1 : install java
sudo apt update
sudo apt install openjdk-11-jdk -y
- Refer Here for the playbook and inventory created so far.
- Step 2: Create a tomcat user: Refer Here for the manual steps
-
We have create a task using user module to create system user. Refer Here for the changes added.
-
Now lets download the tomcat tar file and extract it and Refer Here for the changes done in the playbook to accomodate tomcat download and extract
-
Now lets try to write an ansible task to create a symbolic linkm
sudo ln -s /opt/tomcat/apache-tomcat-${VERSION} /opt/tomcat/latest
- Refer Here for the changes to create symlink
- This playbook is creating the latest tomcat folder with root permissions Refer Here
- To automate the step
sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
one approach is to use- ansible.builtin.command which executes the linux command
- The problem with this approach is, it is not idempotent. So the command will be executed every time ansible playbook is executed.
- To solve this problem,
- We can use lookups
- Registering varaibles