User Tools

Site Tools


servers:management_tools:ansible

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
servers:management_tools:ansible [2025/02/11 14:45] jmbargalloservers:management_tools:ansible [2025/02/11 14:53] (current) jmbargallo
Line 1: Line 1:
-====== Ansible Cheatsheet ======+====== Ansible ======
  
-==== Playbook Structure ====+Ansible is an open source, command-line IT automation software application written in Python. It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more. Ansible's main strengths are simplicity and ease of use.
  
-  * **Simple playbook:** +===== Install Ansible =====
-    ~~~ +
-    --- +
-    - name: Install a package +
-      hosts: web +
-      tasks: +
-        - name: Install nginx +
-          ansible.builtin.yum: +
-            name: nginx +
-            state: present +
-    ~~~+
  
-  * **Playbook with variables:** +  apt install ansible 
-    ~~~ + 
-    --- +Next, we need to create a file `/etc/ansible/hosts`, and add our hosts. In essence, here we define hosts and groups of hosts that Ansible will try to manage. 
-    name: Install package with variable + 
-      hosts: web +  sudo nano /etc/ansible/hosts 
-      vars+   
-        package_namenginx +  [control] 
-      tasks+  master01  ansible_connection=local 
-        - nameInstall nginx +   
-          ansible.builtin.yum+  [workers] 
-            name: "{{ package_name }}" +  worker01  ansible_connection=ssh 
-            statepresent +  worker02  ansible_connection=ssh 
-    ~~~+  worker03  ansible_connection=ssh 
 + 
 +  [cube:children] 
 +  control 
 +  workers 
 + 
 +Above, you can see I have added 3 groups: control, workers and cube. Name of the group is the one in between [ ]. This was split so that if I want to execute some actions only on control server, I use the “control” group. Group “cube” has children. This basically means that it’s a group of groups, and when I’m using cube I’m targeting every single node from the listed groups. 
 + 
 +Variable: `ansible_connection`: we are telling Ansible how to connect to that host. The primary method is ssh, but I specified “local” for control01, because this is the node that we are running Ansible from. This way, it won’t try to ssh to itself. 
 + 
 +Lastly, we are going to make it so that user root will be able to log in to other nodes from control01 without the password using an ssh key. This step is optional, but after this you won’t need to type the password every time you run Ansible. 
 + 
 +  # Make sure you are user root 
 +   
 +  cd 
 +  mkdir -p ~/.ssh 
 +  chmod 700 ~/.ssh 
 +   
 +  # Do not fill anything in next command just enter 
 +   
 +  ssh-keygen -t rsa 
 +   
 +  # Copy keys to each node, for example: 
 +   
 +  ssh-copy-id -i ~/.ssh/id_rsa.pub root@worker01 
 +  ssh-copy-id -i ~/.ssh/id_rsa.pub root@worker02 
 +  ssh-copy-id -i ~/.ssh/id_rsa.pub root@worker03 
 + 
 +After this, we are ready for some mass settings with Ansible. 
 + 
 +===== First Ansible commands ===== 
 + 
 +This is the last thing before we head on to the next article. We are going to check if Ansible is working fine and can connect to all nodes: 
 + 
 +  # Run following as root user 
 +  # We are going to execute ping via ansible, the "cube" is group we specified in /etc/ansible/hosts 
 +  # And if you remember this will execute the command on all nodes. 
 +  # -m mean we are going to use module, in our case moduleping 
 + 
 +  ubuntu@ubuntu:~$ ansible cube -m ping 
 + 
 +  # Result should be: 
 +  ubuntu@ubuntu:~$ ansible cube -m ping 
 +  master01 | SUCCESS => { 
 +      "ansible_facts"{ 
 +          "discovered_interpreter_python""/usr/bin/python3" 
 +      }, 
 +      "changed"false, 
 +      "ping""pong" 
 +  } 
 +  worker01 | SUCCESS => { 
 +      "ansible_facts": { 
 +          "discovered_interpreter_python""/usr/bin/python3" 
 +      }, 
 +      "changed"false, 
 +      "ping": "pong" 
 +  } 
 +  worker02 | SUCCESS => { 
 +      "ansible_facts": { 
 +          "discovered_interpreter_python": "/usr/bin/python3" 
 +      }
 +      "changed": false, 
 +      "ping""pong" 
 +  } 
 +  worker03 | SUCCESS => { 
 + 
 +==== Resources ==== 
 + 
 +  * [Ansible Documentation](https://docs.ansible.com/
 +  * [Ansible GitHub Repository](https://github.com/ansible/ansible)
  
servers/management_tools/ansible.1739285140.txt.gz · Last modified: 2025/02/11 14:45 by jmbargallo