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:50] – jmbargallo | servers:management_tools:ansible [2025/02/11 14:53] (current) – jmbargallo | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Ansible | + | ====== Ansible ====== |
| - | ==== Basics ==== | + | 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' |
| - | Run a playbook: | + | ===== Install Ansible ===== |
| - | + | ||
| - | ansible-playbook playbook.yml | + | |
| - | + | ||
| - | | + | |
| - | + | ||
| - | | + | |
| - | + | ||
| - | * **List available modules: | + | 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. |
| - | + | ||
| - | | + | |
| - | + | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | master01 |
| + | |||
| + | [workers] | ||
| + | worker01 | ||
| + | worker02 | ||
| + | | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | + | ||
| - | ==== Inventory ==== | + | 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. |
| - | * **Specify inventory file:** | + | Variable: `ansible_connection`: |
| - | + | ||
| - | ansible-playbook -i inventory_file playbook.yml | + | |
| - | + | ||
| - | * **Host group example | + | 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. |
| - | + | ||
| - | [web] | + | |
| - | webserver1 | + | |
| - | webserver2 | + | |
| - | + | ||
| - | | + | |
| - | | + | |
| - | | + | 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 | ||
| + | | ||
| + | ssh-copy-id -i ~/ | ||
| - | ==== Playbook Structure ==== | + | After this, we are ready for some mass settings with Ansible. |
| - | * **Simple playbook: | + | ===== First Ansible commands ===== |
| - | | + | |
| - | --- | + | |
| - | - name: Install a package | + | |
| - | hosts: web | + | |
| - | tasks: | + | |
| - | - name: Install nginx | + | |
| - | ansible.builtin.yum: | + | |
| - | name: nginx | + | |
| - | state: present | + | |
| - | + | ||
| - | * **Playbook with variables: | + | 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: |
| - | | + | |
| - | --- | + | |
| - | - name: Install package with variable | + | |
| - | hosts: web | + | |
| - | vars: | + | |
| - | package_name: | + | |
| - | tasks: | + | |
| - | - name: Install nginx | + | |
| - | ansible.builtin.yum: | + | |
| - | name: "{{ package_name }}" | + | |
| - | state: present | + | |
| - | + | ||
| - | | + | |
| - | yaml | + | # We are going to execute ping via ansible, the " |
| - | --- | + | # And if you remember this will execute the command on all nodes. |
| - | - name: Install and configure web server | + | # |
| - | hosts: web | + | |
| - | roles: | + | |
| - | - webserver | + | |
| - | + | ||
| - | ==== Common Modules ==== | + | ubuntu@ubuntu: |
| - | | + | |
| - | | + | |
| - | - name: Install package | + | |
| - | ansible.builtin.yum: | + | |
| - | name: nginx | + | " |
| - | state: present | + | |
| - | + | | |
| - | + | | |
| - | | + | |
| - | | + | |
| - | - name: Copy file | + | |
| - | | + | " |
| - | src: / | + | |
| - | dest: / | + | |
| - | + | | |
| - | + | } | |
| - | * **Command (Run a command): | + | |
| - | | + | "ansible_facts": |
| - | | + | |
| - | ansible.builtin.command: | + | |
| - | + | " | |
| - | + | " | |
| - | | + | |
| - | | + | |
| - | - name: Change file permissions | + | |
| - | | + | |
| - | path: /path/to/file | + | |
| - | mode: ' | + | |
| - | + | ||
| - | + | ||
| - | * **Service (Manage services): | + | |
| - | | + | |
| - | - name: Start nginx service | + | |
| - | | + | |
| - | name: nginx | + | |
| - | state: started | + | |
| - | + | ||
| - | + | ||
| - | ==== Variables ==== | + | |
| - | + | ||
| - | * **Define variables in playbook: | + | |
| - | | + | |
| - | vars: | + | |
| - | | + | |
| - | + | ||
| - | + | ||
| - | * **Use variables in tasks:** | + | |
| - | | + | |
| - | - name: Install package | + | |
| - | | + | |
| - | name: "{{ var_name }}" | + | |
| - | state: present | + | |
| - | + | ||
| - | + | ||
| - | | + | |
| - | + | ||
| - | [web] | + | |
| - | webserver1 ansible_ssh_user=ubuntu | + | |
| - | + | ||
| - | + | ||
| - | | + | |
| - | | + | |
| - | - name: Print host IP | + | |
| - | debug: | + | |
| - | var: ansible_default_ipv4.address | + | |
| - | + | ||
| - | + | ||
| - | ==== Loops and Conditionals ==== | + | |
| - | + | ||
| - | * **Loop over items:** | + | |
| - | | + | |
| - | - name: Install multiple packages | + | |
| - | | + | |
| - | name: "{{ item }}" | + | |
| - | state: present | + | |
| - | | + | |
| - | - nginx | + | |
| - | - vim | + | |
| - | + | ||
| - | + | ||
| - | * **When conditional: | + | |
| - | | + | |
| - | - name: Install nginx if variable is true | + | |
| - | | + | |
| - | name: nginx | + | |
| - | state: present | + | |
| - | | + | |
| - | + | ||
| - | + | ||
| - | | + | |
| - | | + | |
| - | - name: Print index of items | + | |
| - | | + | |
| - | msg: "Item {{ item }} is at index {{ ansible_loop.index }}" | + | |
| - | loop: | + | |
| - | - one | + | |
| - | - two | + | |
| - | - three | + | |
| - | + | ||
| - | + | ||
| - | ==== Handlers ==== | + | |
| - | + | ||
| - | * **Define a handler: | + | |
| - | | + | |
| - | handlers: | + | |
| - | - name: Restart nginx | + | |
| - | ansible.builtin.systemd: | + | |
| - | | + | |
| - | state: restarted | + | |
| - | + | ||
| - | + | ||
| - | * **Notify a handler: | + | |
| - | | + | |
| - | - name: Modify nginx config | + | |
| - | ansible.builtin.copy: | + | |
| - | src: nginx.conf | + | |
| - | dest: /etc/nginx/nginx.conf | + | |
| - | | + | |
| - | + | ||
| - | + | ||
| - | ==== Ansible Vault ==== | + | |
| - | + | ||
| - | * **Create a new encrypted file:** | + | |
| - | + | ||
| - | ansible-vault create secret.yml | + | |
| - | + | ||
| - | + | ||
| - | * **Edit an encrypted file:** | + | |
| - | + | ||
| - | ansible-vault edit secret.yml | + | |
| - | + | ||
| - | + | ||
| - | | + | |
| - | + | ||
| - | ansible-vault encrypt existing_file.yml | + | |
| - | + | ||
| - | + | ||
| - | | + | |
| - | + | ||
| - | ansible-vault decrypt secret.yml | + | |
| - | + | ||
| - | + | ||
| - | * **Run a playbook with vault password: | + | |
| - | + | ||
| - | ansible-playbook --ask-vault-pass playbook.yml | + | |
| - | + | ||
| - | + | ||
| - | ==== Useful Commands ==== | + | |
| - | + | ||
| - | * **Check syntax of a playbook: | + | |
| - | + | ||
| - | ansible-playbook --syntax-check playbook.yml | + | |
| - | + | ||
| - | + | ||
| - | * **Run playbook in check mode (dry-run): | + | |
| - | + | ||
| - | ansible-playbook --check playbook.yml | + | |
| - | + | ||
| - | + | ||
| - | * **Run playbook with verbose output:** | + | |
| - | + | ||
| - | ansible-playbook -v playbook.yml | + | |
| - | + | ||
| - | + | ||
| - | * **Display detailed information about a module:** | + | |
| - | + | ||
| - | ansible-doc < | + | |
| - | + | ||
| ==== Resources ==== | ==== Resources ==== | ||
servers/management_tools/ansible.1739285421.txt.gz · Last modified: 2025/02/11 14:50 by jmbargallo
