servers:management_tools:ansible
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| servers:management_tools:ansible [2025/02/11 14:45] – jmbargallo | servers:management_tools:ansible [2025/02/11 14:53] (current) – jmbargallo | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Ansible | + | ====== 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' |
| - | * **Simple playbook: | + | ===== Install |
| - | ~~~ | + | |
| - | --- | + | |
| - | - name: Install | + | |
| - | hosts: web | + | |
| - | tasks: | + | |
| - | - name: Install nginx | + | |
| - | ansible.builtin.yum: | + | |
| - | name: nginx | + | |
| - | state: present | + | |
| - | ~~~ | + | |
| - | | + | |
| - | ~~~ | + | |
| - | --- | + | Next, we need to create a file `/ |
| - | - name: Install package | + | |
| - | hosts: | + | sudo nano / |
| - | | + | |
| - | | + | [control] |
| - | | + | master01 |
| - | - name: Install nginx | + | |
| - | | + | [workers] |
| - | name: " | + | worker01 |
| - | state: present | + | worker02 |
| - | ~~~ | + | worker03 |
| + | |||
| + | [cube:children] | ||
| + | | ||
| + | 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`: | ||
| + | |||
| + | 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-copy-id -i ~/ | ||
| + | ssh-copy-id -i ~/ | ||
| + | |||
| + | After this, we are ready for some mass settings | ||
| + | |||
| + | ===== 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 " | ||
| + | # And if you remember this will execute the command on all nodes. | ||
| + | # -m mean we are going to use module, in our case module: ping | ||
| + | |||
| + | ubuntu@ubuntu: | ||
| + | |||
| + | # Result should be: | ||
| + | ubuntu@ubuntu: | ||
| + | master01 | SUCCESS => { | ||
| + | | ||
| + | " | ||
| + | | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | worker01 | SUCCESS => { | ||
| + | " | ||
| + | | ||
| + | }, | ||
| + | " | ||
| + | | ||
| + | } | ||
| + | worker02 | SUCCESS => { | ||
| + | " | ||
| + | " | ||
| + | | ||
| + | | ||
| + | " | ||
| + | } | ||
| + | worker03 | SUCCESS => { | ||
| + | |||
| + | ==== Resources ==== | ||
| + | |||
| + | * [Ansible Documentation](https:// | ||
| + | * [Ansible GitHub Repository](https:// | ||
servers/management_tools/ansible.1739285140.txt.gz · Last modified: 2025/02/11 14:45 by jmbargallo
