├── docker.md ├── kubernetes.md ├── apt.md ├── snap.md ├── .gitignore ├── .travis.yml ├── SUMMARY.md ├── R.md ├── git.md ├── README.md ├── nginx.md ├── mysql.md ├── vim.md └── ansible.md /docker.md: -------------------------------------------------------------------------------- 1 | # docker 2 | 3 | -------------------------------------------------------------------------------- /kubernetes.md: -------------------------------------------------------------------------------- 1 | # Kubernetes 2 | 3 | #### install 4 | 5 | 6 | 7 | ## will finish this on friday 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /apt.md: -------------------------------------------------------------------------------- 1 | # apt 2 | 3 | * install only security updates 4 | 5 | `sudo unattended-upgrade -d` 6 | 7 | `-d` is for verbose mode. for quiet just remove it. 8 | 9 | -------------------------------------------------------------------------------- /snap.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | *list all installed snap package 4 | 5 | `sudo snap list` 6 | 7 | *remove a snap package 8 | 9 | `sudo snap remove [PACKAGE]` 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/git 3 | 4 | ### Git ### 5 | *.orig 6 | 7 | 8 | # End of https://www.gitignore.io/api/git 9 | 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.5.1 4 | before_install: 5 | - gem install mdl 6 | 7 | script: 8 | - mdl . 9 | 10 | after_success: 11 | 12 | - echo "Test Successful. All syntax and links are valid." 13 | -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Table of contents 2 | 3 | * [Home Page](README.md) 4 | * [Nginx](nginx.md) 5 | * [Git](git.md) 6 | * [Mysql](mysql.md) 7 | * [Kubernetes](kubernetes.md) 8 | * [apt](apt.md) 9 | * [Ansible](ansible.md) 10 | * [docker](docker.md) 11 | 12 | -------------------------------------------------------------------------------- /R.md: -------------------------------------------------------------------------------- 1 | # R 2 | 3 | ### Read `csv` file 4 | 5 | * Use `read.table` with address as first argument and let `sep` equal to seperator character usually is comma (`sep=","`) 6 | ```data = read.table("file_address.csv", sep=",")``` 7 | 8 | `data` object will a a data frame which you can use it. 9 | 10 | * csv file with header 11 | just add `header = TRUE` argument: 12 | ``` data = read.table("file_address.csv", sep=",",header=TRUE) ``` 13 | `data` object will dataframe with header names 14 | 15 | -------------------------------------------------------------------------------- /git.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: Git cheatsheet //in development// 3 | --- 4 | 5 | # Git 6 | 7 | * Initialize Git 8 | 9 | `git init` 10 | 11 | * Clone a repository 12 | 13 | `git clone -v --progress git@gitlab.com:USER/rep.git /var/www/html/customername` 14 | 15 | * Add a new remote pull/push destination 16 | 17 | `git remote add origin git@gitlab.com:USER/rep.git` 18 | 19 | * Create a new branch 20 | 21 | `git branch [branch-name] # Replace branch name` 22 | 23 | * Set branch push/push destination 24 | 25 | `git branch --set-upstream-to origin # Or remote name created beforehand` 26 | 27 | * Stage changes to commit 28 | 29 | `git add -A` 30 | 31 | * Stage changes of specific file 32 | 33 | `git add [path-to-file]` 34 | 35 | * Unstage changes of specific file 36 | 37 | `git reset HEAD [path-to-file]` 38 | 39 | * Commit changes 40 | 41 | `git commit -m "Message"` 42 | 43 | * Add more changes to last commit 44 | 45 | `git commit --amend` 46 | 47 | * Merge branch B to branch A 48 | 49 | ```text 50 | git checkout A 51 | git merge B 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Home Page 2 | 3 | [![Build Status](https://travis-ci.org/numb95/cheatsheet.svg?branch=master)](https://travis-ci.org/numb95/cheatsheet) 4 | 5 | A series of usefull cheatsheet 6 | 7 | ## ToDo 8 | 9 | \[x\] migrate from gitbook to something more useful. \[x\] write it own render tools. \[x\] fix CI 10 | 11 | ## Web Servers 12 | 13 | 1. [Nginx](nginx.md) 14 | 1. [installing](nginx.md#install) 15 | 2. [use as a load balancer](nginx.md#config-as-a-load-balancer) 16 | 17 | ## Version Controling System 18 | 19 | 1. [Git](git.md) 20 | 21 | ## Databases 22 | 23 | 1. [Mysql](mysql.md) 24 | 25 | ## Automation 26 | 27 | 1. [Ansible](ansible.md) 28 | 29 | ## DevOps Tools 30 | 31 | 1. kubernetes 32 | 2. docker 33 | 34 | ## Statistics and Data Mining tools 35 | 36 | 1. [R](https://github.com/numb95/cheatsheet/tree/8afcefef1a1afdca65593f1596f3b5bea898e920/R.md) 37 | 1. [Read `csv` file](https://github.com/numb95/cheatsheet/tree/8afcefef1a1afdca65593f1596f3b5bea898e920/R.md#Read-csv-file) 38 | 39 | ## Utilities 40 | 41 | 1. [Vim](https://github.com/numb95/cheatsheet/tree/8afcefef1a1afdca65593f1596f3b5bea898e920/vim.md) 42 | 43 | ## OS Tools 44 | 45 | ### Debian/Ubuntu 46 | 47 | 1. [apt](apt.md) 48 | 2. [snap](https://github.com/numb95/cheatsheet/tree/8afcefef1a1afdca65593f1596f3b5bea898e920/snap.md) 49 | 50 | -------------------------------------------------------------------------------- /nginx.md: -------------------------------------------------------------------------------- 1 | # Nginx 2 | 3 | ### install 4 | 5 | `sudo apt install nginx` 6 | 7 | ### Config as a load balancer 8 | 9 | #### The default method of nginx in round-robin. 10 | 11 | * edit `/etc/nginx/sites-available/VHOST` and add this to the file. you can also remove other lines 12 | 13 | ```bash 14 | upstream servers_address { 15 | least_conn; #for least connected method 16 | ip_hash; #for hp_hash method 17 | server server1_ip_or_url weight=1 max_fails=4 fail_timeout=30; 18 | server server2_ip_or_url weight=2; 19 | server server3_ip_or_url weight=4; 20 | server server4_ip_or_url weight=8; 21 | } 22 | ``` 23 | 24 | * then add: 25 | 26 | ```text 27 | server { 28 | listen 80; 29 | location / { 30 | proxy_pass http://servers_address; 31 | proxy_set_header HOST $host; 32 | 33 | } 34 | } 35 | ``` 36 | 37 | * restart nginx 38 | 39 | {% hint style="info" %} 40 | default weight is 1. wight 2 will be sent twice traffic as much weight 1. Also wight 8 sent 8 times traffic to a specific server as much weight 1. This sample will also work on weight 2 or 3 41 | {% endhint %} 42 | 43 | {% hint style="info" %} 44 | **ip\_hash** is a method that respond to the clients based on their IP addresses. It's beside **round-robin** and **least connected.** 45 | {% endhint %} 46 | 47 | {% hint style="info" %} 48 | **max\_fails** is a maximum amount of attempts that nginx send data to server if down. **fail\_timeout** is it's timeout :\)\) 49 | {% endhint %} 50 | 51 | {% hint style="info" %} 52 | **least connected** is the method that nginx won't route traffic to a busy server. 53 | {% endhint %} 54 | 55 | -------------------------------------------------------------------------------- /mysql.md: -------------------------------------------------------------------------------- 1 | # Mysql 2 | 3 | ## install 4 | 5 | * for server 6 | * `sudo apt install mysql-server` 7 | * for client 8 | * `sudo apt install mysql-client` 9 | 10 | ## login 11 | 12 | `mysql -u USER -p -h HOSTNAME` 13 | 14 | ## list databases 15 | 16 | `SHOW DATABASES;` 17 | 18 | ## Create DB 19 | 20 | `CREATE DATABASE db_name;` 21 | 22 | ## Create User 23 | 24 | `CREATE USER db_user@localhost;` 25 | 26 | ## Set password to user 27 | 28 | `SET PASSWORD FOR db_user@localhost= PASSWORD("password_of_username");` 29 | 30 | ## grant privilege of a DB to a user 31 | 32 | `GRANT ALL PRIVILEGES ON db_name.* TO db_user@localhost IDENTIFIED BY 'password_of_username';` 33 | 34 | ## grant privilege of a namespace to a user 35 | 36 | ``grant all privileges on `namespace\_%`.* to 'db_user'@'localhost' ;`` 37 | 38 | ## Delete database 39 | 40 | `DROP DATABASE db_name` 41 | 42 | ## Create backup 43 | 44 | `mysqldump -u DB_USER -p DB_NAME > filename.sql` 45 | 46 | ## restore backup 47 | 48 | `mysql -u root -p -h localhost DB_NAME < filename.sql` 49 | 50 | ## Create tabale 51 | 52 | ```text 53 | CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table( 54 | key type(size) NOT NULL PRIMARY KEY AUTO_INCREMENT, 55 | c1 type(size) NOT NULL, 56 | c2 type(size) NULL, 57 | ... 58 | ); 59 | ``` 60 | 61 | ## Add a new column into a table 62 | 63 | `ALTER TABLE table ADD [COLUMN];` 64 | 65 | ## Drop an existing column in a table 66 | 67 | `ALTER TABLE table DROP [COLUMN];` 68 | 69 | ## Add index with a specific name to a table on a column 70 | 71 | `ALTER TABLE table ADD INDEX [name](column, ...);` 72 | 73 | ## Add primary key into a table 74 | 75 | `ALTER TABLE table ADD PRIMARY KEY (column,...)` 76 | 77 | ## Remove primary key from a table 78 | 79 | `ALTER TABLE table DROP PRIMARY KEY` 80 | 81 | ## Deleting table structure and data permanently 82 | 83 | `DROP TABLE [IF EXISTS] table [, name2, ...] [RESTRICT | CASCADE]` 84 | 85 | ## Creating an index with the specified name on a table 86 | 87 | `CREATE [UNIQUE|FULLTEXT] INDEX index_name ON table (column,...)` 88 | 89 | ## Removing a specified index from table 90 | 91 | `DROP INDEX index_name` 92 | 93 | ## Query all data from a table 94 | 95 | `SELECT * FROM table` 96 | 97 | ## Query specified data which is shown in the column list from a table 98 | 99 | `SELECT column, column2 FROM table;` 100 | 101 | ## Query unique records 102 | 103 | `SELECT DISTINCT (column) FROM table;` 104 | 105 | ## Query data with a filter using a WHERE clause 106 | 107 | `SELECT * FROM table WHERE condition;` 108 | 109 | -------------------------------------------------------------------------------- /vim.md: -------------------------------------------------------------------------------- 1 | # VIM 2 | 3 | VIM is a powerful text editor on command line environment which can be found on MacOS and almost all GNU/Linux distributions as a built-in software. 4 | 5 | ## Exit 6 | 7 | OK Babaaa! For exit from vim just press `ESC` button, then press `:` and type `q!` and finally press enter! Why `ESC`? Vim has three modes: 8 | 9 | 1. **Normal Mode:** In this mode you can run commands, delete lines, fold sections and some actions like them. In Normal alphabets have meaning and do some semi-magical actions for you! 10 | 2. **Visual Mode:** In this mode you can select a part of text. 11 | 3. **Insert Mode:** You type on this mode! 12 | 13 | You can switch to normal mode by pressing `ESC` in each two other modes! If you're already in normal mode nothing happens. 14 | 15 | ## Keys Meaning 16 | 17 | As mentioned in previous section, in normal mode keys have meaning! What the mean of this? Well, the long story short: **You map an action to a key and combine them to make more complex actions, this is your real power!**. We discuss about combination of actions on next section. In this section we just have glim on keys and their meaning! 18 | 19 | * `u`: **Undo** it! Don't Worry! 20 | 21 | * `d`: **Delete**! It actually don't delete, it **cut** but if we don't paste it elsewhere, we can suppose that we deleted it! :D. 22 | * `y`: **Yank**! It means **copy**! 23 | * `p`: **Paste**! It paste things which you buffered (had copied or cut) **after** the cursor position. 24 | * `P`: **Paste** but **before** the cursor position. 25 | 26 | * `w`: **Forward** move on **word**s. For example on *hello, world from vim!*, if cursor is on *world* word, by pressing `w` it jumps to beginning of `from` word. 27 | * `b`: **Backward** move on **word**s. With previous step, if now we press `b`, cursor move from *from* to `world`! Also if the cursor is on middle of a word (for example on *o* of *from*) by pressing `b`, it moves the cursor to the beginning of word. 28 | * `l`: Move cursor **right**! I memorized it with this pattern: *I wanna move **r**ight, so I press **l**eft, I know, it doesn't make any sense but it works! 29 | * `k`: Move cursor **top**! 30 | * `j`: Move cursor **bottom**! 31 | * `h`: Move cursor **right**! :D. 32 | * `i`: Change mode to **insert** mode in **cursor position**. 33 | * `I`: Change mode to **insert** mode in the **beginning of the line**. 34 | * `a`: Change to insert mode and **append ** what you type from **next column** of current cursor position. 35 | * `A`: **Append** but to **end of the line**. 36 | * `o`: Change mode to **insert** mode in **next line**! You press `o`, it create a blank line after the current line and move the cursor to that. 37 | * `O`: Change mode to **insert** mode in **previous line**! (this is big `o` not a zero :/). -------------------------------------------------------------------------------- /ansible.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: Ansible Cheatsheet 3 | --- 4 | 5 | # Ansible 6 | 7 | Install ansible linter to check the file is ok or not: 8 | 9 | ```text 10 | sudo apt install ansible-lint 11 | ``` 12 | 13 | then: 14 | 15 | ```text 16 | ansible-lint playbook.yaml 17 | ``` 18 | 19 | Check if the server is running: 20 | 21 | ```bash 22 | ansible all -i hostname, -m ping 23 | ``` 24 | 25 | Expected output: 26 | 27 | ```bash 28 | hostname | SUCCESS => { 29 | "changed": false, 30 | "ping": "pong" 31 | } 32 | ``` 33 | 34 | Run command on host: 35 | 36 | ```bash 37 | ansible all -i hostname, -m shell -a 'echo hello world' 38 | ``` 39 | 40 | Playbook file should name something like `FILENAME.yaml`. 41 | 42 | it's better to describe the name of the file base on the task of that file like `apache.yaml`. 43 | 44 | {% code-tabs %} 45 | {% code-tabs-item title="apache.yaml" %} 46 | ```yaml 47 | --- 48 | - hosts: all 49 | remote_user: root 50 | tasks: 51 | - name: Install Apache2 on server 52 | yum: 53 | name: httpd 54 | state: present 55 | - name: Ensure that apache2 is running 56 | service: 57 | name: httpd 58 | state: started 59 | enable: True 60 | become: True 61 | ``` 62 | {% endcode-tabs-item %} 63 | {% endcode-tabs %} 64 | 65 | Run the playbook: 66 | 67 | ```bash 68 | ansible-playbook -i hostname, apache.yaml 69 | ``` 70 | 71 | Show all configs and variables of a host: 72 | 73 | ```bash 74 | ansible all -i hostname, -m setup 75 | ``` 76 | 77 | Using Ansible setup variable: 78 | 79 | {% code-tabs %} 80 | {% code-tabs-item title="ansible\_var.yaml" %} 81 | ```yaml 82 | --- 83 | - hosts: all 84 | remote_user: root 85 | tasks: 86 | - name: debugging from setup 87 | debug: 88 | msg: "OS version is '{{ ansible_distribution }}' '{{ ansible_distribution_file_variety }}' with IP: '{{ ansible_all_ipv4_addresses }}'" 89 | ``` 90 | {% endcode-tabs-item %} 91 | {% endcode-tabs %} 92 | 93 | Output: 94 | 95 | ```bash 96 | TASK [debugging from setup] ******************************************************************************************************************************************** 97 | ok: [centosvm] => { 98 | "msg": "OS version is 'CentOS' 'RedHat' with IP: '['192.168.43.111', '192.168.43.64']'" 99 | } 100 | ``` 101 | 102 | Inline var: 103 | 104 | {% code-tabs %} 105 | {% code-tabs-item title="var1.yaml" %} 106 | ```yaml 107 | --- 108 | - hosts: all 109 | remote_user: root 110 | tasks: 111 | - name: Variable assigning 112 | set_fact: 113 | pkg1: sqlite 114 | - name: Install Apache2 on server 115 | yum: 116 | name: httpd 117 | name: "{{ pkg1 }}" 118 | state: present 119 | - name: Ensure that apache2 is running 120 | service: 121 | name: httpd 122 | state: started 123 | become: True 124 | ``` 125 | {% endcode-tabs-item %} 126 | {% endcode-tabs %} 127 | 128 | then run the playbook. 129 | 130 | Another way: 131 | 132 | {% code-tabs %} 133 | {% code-tabs-item title="var2.yaml" %} 134 | ```yaml 135 | --- 136 | - hosts: all 137 | remote_user: root 138 | tasks: 139 | - name: Install Apache2 on se 140 | rver 141 | yum: 142 | name: httpd 143 | name: "{{ pkg1 }}" 144 | state: present 145 | - name: Ensure that apache2 is running 146 | service: 147 | name: httpd 148 | state: started 149 | become: True 150 | ``` 151 | {% endcode-tabs-item %} 152 | {% endcode-tabs %} 153 | 154 | and execute this: 155 | 156 | ```bash 157 | ansible-playbook -i hostname, var.yaml -e 'pkg1=vim' 158 | ``` 159 | 160 | on CentOS, enable the EPEL 161 | 162 | {% code-tabs %} 163 | {% code-tabs-item title="epel.yaml" %} 164 | ```yaml 165 | --- 166 | - hosts: all 167 | remote_user: root 168 | tasks: 169 | - name: Install epel 170 | yum: 171 | name: epel-release 172 | state: present 173 | ``` 174 | {% endcode-tabs-item %} 175 | {% endcode-tabs %} 176 | 177 | upgrade a package 178 | 179 | {% code-tabs %} 180 | {% code-tabs-item title="apache\_upgrade.yaml" %} 181 | ```yaml 182 | --- 183 | - hosts: all 184 | remote_user: root 185 | tasks: 186 | - name: Install Apache2 on s 187 | erver 188 | yum: 189 | name: httpd 190 | state: latest 191 | ``` 192 | {% endcode-tabs-item %} 193 | {% endcode-tabs %} 194 | 195 | Or upgrade all: 196 | 197 | {% code-tabs %} 198 | {% code-tabs-item title="upgrade.yaml" %} 199 | ```yaml 200 | --- 201 | - hosts: all 202 | remote_user: root 203 | tasks: 204 | - name: Upgrade all packages 205 | yum: 206 | name: "*" 207 | state: Latest 208 | ``` 209 | {% endcode-tabs-item %} 210 | {% endcode-tabs %} 211 | 212 | to list all tasks: 213 | 214 | ```text 215 | ansible-playbook taskname.yaml --list-tasks 216 | ``` 217 | 218 | Generating static files using jinja2 and ansible 219 | 220 | {% code-tabs %} 221 | {% code-tabs-item title="jinja.yaml" %} 222 | ```yaml 223 | --- 224 | - hosts: all 225 | remote_user: root 226 | tasks: 227 | - name: testing jinja2 228 | template: 229 | src: index.html.j2 230 | dest: /var/www/html/index.html 231 | owner: root 232 | group: root 233 | mode: 0644 234 | ``` 235 | {% endcode-tabs-item %} 236 | {% endcode-tabs %} 237 | 238 | {% code-tabs %} 239 | {% code-tabs-item title="index.html.j2" %} 240 | ```markup 241 | 242 | 243 |

Hello World!

244 |

This page was created on {{ ansible_date_time.date 245 | }}.

246 | {% if ansible_eth0.active == True %} 247 |

eth0 address {{ ansible_eth0.ipv4.address }}.

248 | {% endif %} 249 | 250 | 251 | ``` 252 | {% endcode-tabs-item %} 253 | {% endcode-tabs %} 254 | 255 | [More Doc for Jinja2](https://jinja.pocoo.org/docs/2.10/) 256 | 257 | Working with Hosts 258 | 259 | {% code-tabs %} 260 | {% code-tabs-item title="hostname.txt" %} 261 | ```text 262 | [general] 263 | 192.168.1.100 264 | ex.sample.com 265 | ex[1:2].sample.com 266 | db.sample.com 267 | 268 | [webserver] 269 | 192.168.1.100 270 | ex2.sample.com 271 | 272 | [database] 273 | db.sample.com 274 | ``` 275 | {% endcode-tabs-item %} 276 | {% endcode-tabs %} 277 | 278 | Sample of a playbook which only run tasks on webserver group 279 | 280 | {% code-tabs %} 281 | {% code-tabs-item title="apache.yaml" %} 282 | ```yaml 283 | --- 284 | - hosts: webserver 285 | remote_user: root 286 | tasks: 287 | - name: Install Apache2 on server 288 | yum: 289 | name: httpd 290 | state: present 291 | - name: Ensure that apache2 is running 292 | service: 293 | name: httpd 294 | state: started 295 | enable: True 296 | become: True 297 | ``` 298 | {% endcode-tabs-item %} 299 | {% endcode-tabs %} 300 | 301 | for running: 302 | 303 | ```text 304 | ansible-playbook -i hostname.txt, apache2.yaml 305 | ``` 306 | 307 | In this section, I decided to write more about variables. but i think i should refer you to the [Official Documents](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html). 308 | 309 | please note that you should also read the documents of ansible for each modules's variable for better performance. 310 | 311 | for working with iteration use `with_items` 312 | 313 | {% code-tabs %} 314 | {% code-tabs-item title="general.yaml" %} 315 | ```yaml 316 | --- 317 | - hosts: all_machines 318 | remote_user: root 319 | tasks: 320 | - name: install nececery tools 321 | yum: 322 | name: '{{ item }}' 323 | state: present 324 | with_items: 325 | - vim 326 | - sqlite 327 | - openjdk-jre 328 | - docker 329 | ``` 330 | {% endcode-tabs-item %} 331 | {% endcode-tabs %} 332 | 333 | other method. make a backup of a file/directory 334 | 335 | {% code-tabs %} 336 | {% code-tabs-item title="general.yaml" %} 337 | ```yaml 338 | --- 339 | - hosts: all_machines 340 | remote_user: root 341 | vars: 342 | dirs: 343 | - /etc/named.conf 344 | - /var/www/html/index.html 345 | users: 346 | - numb 347 | tasks: 348 | - name: ensure user is exist 349 | users: 350 | name: '{{item}}' 351 | become: yes 352 | with_items: 353 | - '{{ users }}' 354 | - name: ensure user directories exist 355 | file: 356 | path: '/home/{{ item }}' 357 | state: directory 358 | become: yes 359 | with_items: 360 | - '{{ users }}' 361 | - name: copy / backup files from src to dest 362 | copy: 363 | src: '{{ item.0 }}' 364 | dest: '/home/{{ item.1 }}' 365 | become: yes 366 | with_nested: 367 | - '{{ dirs }}' 368 | - '{{ users }}' 369 | ``` 370 | {% endcode-tabs-item %} 371 | {% endcode-tabs %} 372 | 373 | working with sequences: 374 | 375 | {% code-tabs %} 376 | {% code-tabs-item title="sequence.yaml" %} 377 | ```yaml 378 | --- 379 | - hosts: webservers 380 | remote_user: root 381 | tasks: 382 | - name: create some file in sequence 383 | file: 384 | dest: '/home/backup{{ item }}' 385 | state: directory 386 | with_sequence: start=1 end=4 387 | become: yes 388 | ``` 389 | {% endcode-tabs-item %} 390 | {% endcode-tabs %} 391 | 392 | you can also change `hostname` with sequences 393 | 394 | condition could be look like this: 395 | 396 | {% code-tabs %} 397 | {% code-tabs-item title="apache.yaml" %} 398 | ```yaml 399 | --- 400 | - hosts: webserver 401 | remote_user: root 402 | tasks: 403 | - name: Install Apache2 on Redhat based 404 | yum: 405 | name: httpd 406 | state: present 407 | when: ansible_os_family =='RedHat' 408 | - name: Install Apache2 on debian based 409 | apt: 410 | name: apache2 411 | state: present 412 | when: ansible_os_family == 'Debian' 413 | - name: Ensure that apache2 is running 414 | service: 415 | name: httpd 416 | state: started 417 | enable: True 418 | become: True 419 | ``` 420 | {% endcode-tabs-item %} 421 | {% endcode-tabs %} 422 | 423 | Boolean condition 424 | 425 | ```text 426 | --- 427 | - hosts: all 428 | remote_user: ansible 429 | vars: 430 | backup: True 431 | tasks: 432 | - name: Copy the crontab in tmp if the backup variable is true 433 | copy: 434 | src: /etc/crontab 435 | dest: /tmp/crontab 436 | remote_src: True 437 | when: backup 438 | ``` 439 | 440 | simply create ansible project structure with this: 441 | 442 | ```bash 443 | mkdir -p roles/common/{tasks,handlers,templates,files,vars,defaults,meta} 444 | touch roles/common/{tasks,handlers,templates,files,vars,defaults,meta}/main.yml 445 | ``` 446 | 447 | secret vault 448 | 449 | inventory example 450 | 451 | \[servers:vars\] 452 | 453 | ```yaml 454 | ansible_user=numb 455 | ansible_become=yes 456 | ansible_become_method=sudo 457 | ansible_become_pass='{{ server_pass }}' 458 | ``` 459 | 460 | create a new encripted secret file 461 | 462 | `ansible-vault create passwords.yml` 463 | 464 | after adding password for file add this to the opened file 465 | 466 | ```text 467 | server_pass: PASSWORD 468 | ``` 469 | 470 | then run the ansible 471 | 472 | `ansible-playbook -i inventory --ask-vault-pass --extra-vars '@passwords.yml' ansible.yml` 473 | 474 | TODO 475 | 476 | write about roles structure, templates, groups, helpers, bash module, docker module, python module, diff, optimizing. 477 | 478 | --------------------------------------------------------------------------------