---
type: archive
area: archive
status: archived
date: 2026-05-03
created: 2026-05-03
updated: 1980-01-01
tags:
  - archive
---
This used to mention that the state of the app should be last version
```
state: latest
```

- This used to remove a specific package
```
state: absent
```
- Package module is used when we want to don't specify package manager name it's a global one that uses the host package manager automatically

![[img-20240731-034445-d1dc8269.png]]
- Here we are managing hosts under groups with some variables and a host can be member of multiple groups in same time .

- This command to run a specific play book with sudo access
```
ansible-playbook <playbook_name> --ask-become-pass
```

**Tags** :  is used to run just a specific part from playbook
Example of a Tag
![[img-20240731-040320-0758ad67.png]]

- Command to run with a specific tag

```
ansible-playbook <playbook_file> --ask-become-pass --tags <tag1_name>,<tag2_name>
```

- We can also manage files like copying using some ansible modules

![[img-20240731-171601-bf282cc7.png]]

- We can create a file in this location **/etc/sudoers.d** that contain desired username to make it the default user of ansible without need password like this example

```
<username> ALL=(ALL) NOPASSWD: ALL
```

- After we can use this specific user + our ssh key to access to all server with out writing sudo password every time.
with the use of copy module to copy the file and authorized_keys module to add the keys to the specific user
- There is also **lineinfile** module that give us the capability to change  a line in a file
- Plus we can also store with **register** . The state stored in a var will serve us to do something based on this state like 
	- if something changed do that 
### Roles
- Directories before roles
![[img-20240801-014145-a0c90ecf.png]]
- After roles
![[img-20240801-023102-f5476e16.png]]
- Like we see that we should create a folder of roles and after create folders with name of roles in the structure in image and after modify the file from where we run this file like that

``` yaml
---
- hosts: all
  become: true
  pre_tasks:
    
    - name: Install update for Fedora
      tags: always
      ansible.builtin.dnf:
        update_only: yes
        update_cache: yes
      when: ansible_distribution == "Fedora"
    
    - name : Install update for Ubuntu
      tags: always
      ansible.builtin.apt:
        upgrade: dist
        update_cache: yes
      when: ansible_distribution == "Ubuntu"

## This part is what we add to call those roles

- hosts: all
  become: true
  roles:
    - base

- hosts: web_servers
  become: true
  roles:
    - web_servers
  
- hosts: db_servers
  become: true
  roles:
    - db_servers
```

### Host vars
- We need to add a directory were to store variables called **host_vars** .
- In this directory we create a file with the specific ip addr of the host or with host name if we are using dns and we store the host variables in it.
Directory structure: 
![[img-20240801-025844-3c34d677.png]]
- What is notify ?
	- Call a task after a specific task like runing a service after installing an app. (It only runs at the end of the process)
	- benefits it will not runs every time it's called it only runs once and at the end of the process
Directory structure
![[img-20240801-031332-5c0d0d9f.png]]

### Secrets
We use it to store some sensitive data and encrypt no one can see it
``` bash
ansible-vault encrypt
ansible-vault decrypt
ansible-vault view
ansible-vault edit
ansible-vault create
```

we can define a very secure password in ~/.vault and create a file that contain a super secure password.

### Ansible Pull
- is used to make servers auto configure them selves using ansible-pull command
- It pull a repository and automatically run the playbook 
