# Ansible Guide
###### tags: `web develope`
## Basic of Ansible
### Select a machine
Ansible is based on ssh connection, thus make sure you can ssh into the remote system. If necessary, add your public SSH key to the authorized_keys file on those systems.
After selecting a machine, edit `/etc/ansible/hosts` and add the remote systems into it. For this example, use either IP addresses or FQDNs:
```shell=
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
#green.example.com
#blue.example.com
#192.168.100.1
#192.168.100.10
140.117.12.99
# Ex 2: A collection of hosts belonging to the 'webservers' group
#[webservers]
#alpha.example.org
#beta.example.org
#192.168.1.100
#192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
#www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
#[dbservers]
#
#db01.intranet.mydomain.net
#db02.intranet.mydomain.net
#10.25.1.56
#10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
#db-[99:101]-node.example.com
```
##
### Run first ansible command
Use the ping module to ping all the nodes in your inventory:
```shell=
$ ansible all -m ping
[DEPRECATION WARNING]: Distribution Ubuntu 20.04 on host 140.117.12.99...<snip>
140.117.12.99 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
```
Now run a live command on all of your nodes:
```shell=
$ ansible all -a "/bin/echo hello"
140.117.12.99 | CHANGED | rc=0 >>
hello
```
If everything ahead going well, you can now go to next steps -- `ad hoc command` & `playbook`.
## Intro to Ad Hoc Command
Ad-hoc tasks can be used to reboot servers, copy files, manage packages and users, and much more. You can use any Ansible module in an ad-hoc task. Ad-hoc tasks, like playbooks, use a declarative model, calculating and executing the actions required to reach a specified final state. They achieve a form of idempotence by checking the current state before they begin and doing nothing unless the current state is different from the specified final state.
## Intro to Playbook
Playbooks are designed to be human-readable and are developed in a basic text language.
Ansible Playbooks also offer a repeatable, re-usable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications. If you need to execute a task with Ansible more than once, write a playbook and put it under source control. Then you can use the playbook to push out new configuration or confirm the configuration of remote systems. The playbooks in the ansible-examples repository illustrate many useful techniques. You may want to look at these in another tab as you read the documentation.
::: success
attention: playbook are writen in yaml file, it's better to get familiar with the syntax of yaml format and syntax.
[YAML Reference](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax)
:::
Assuming that we want to update multiple server, and we finish the playbook script as below.
```yaml=
---
- name: update web servers
hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
- name: update db servers
hosts: databases
remote_user: root
tasks:
- name: ensure postgresql is at the latest version
yum:
name: postgresql
state: latest
- name: ensure that postgresql is started
service:
name: postgresql
state: started
```
Now you can run the anible-playbook command to deploy the update:
```shell=
ansible-playbook playbook.yml -f 10
```