translated

This commit is contained in:
geekpi 2019-08-29 08:43:06 +08:00
parent 0c5ea5b366
commit 825cc72c48
2 changed files with 191 additions and 196 deletions

View File

@ -1,196 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Install Ansible (Automation Tool) on Debian 10 (Buster))
[#]: via: (https://www.linuxtechi.com/install-ansible-automation-tool-debian10/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
How to Install Ansible (Automation Tool) on Debian 10 (Buster)
======
Now a days in IT field, automation is the hot topic and every organization is start adopting the automation tools like **Puppet**, **Ansible**, **Chef**, **CFEngine**, **Foreman** and **Katello**. Out of these tools Ansible is the first choice of almost all the IT organization to manage UNIX and Linux like systems. In this article we will demonstrate on how to install and use ansible tool on Debian 10 Sever.
[![Ansible-Install-Debian10][1]][2]
My Lab details:
* Debian 10 Ansible Server/ controller Node 192.168.1.14
* CentOS 7 Ansible Host (Web Server) 192.168.1.15
* CentOS 7 Ansible Host (DB Server) 192.169.1.17
We will also demonstrate how Linux Servers can be managed using Ansible Server
### Ansible Installation on Debian 10 Server
I am assuming in your Debian 10 system you have a user which has either root privileges or sudo rights. In my setup I have a local user named “pkumar” with sudo rights.
Ansible 2.7 packages are available in default Debian 10 repositories, run the following commands from command line to install Ansible,
```
root@linuxtechi:~$ sudo apt update
root@linuxtechi:~$ sudo apt install ansible -y
```
Run the below command to verify the ansible version,
```
root@linuxtechi:~$ sudo ansible --version
```
![ansible-version][1]
To Install latest version of Ansible 2.8, first we must set Ansible repositories.
Execute the following commands one after the another,
```
root@linuxtechi:~$ echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu bionic main" | sudo tee -a /etc/apt/sources.list
root@linuxtechi:~$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
root@linuxtechi:~$ sudo apt update
root@linuxtechi:~$ sudo apt install ansible -y
root@linuxtechi:~$ sudo ansible --version
```
![latest-ansible-version][1]
### Managing Linux Servers using Ansible
Refer the following steps to manage Linux like servers using Ansible controller node,
### Step:1) Exchange the SSH keys between Ansible Server and its hosts
Generate the ssh keys from ansible server and shared the keys among the ansible hosts
```
root@linuxtechi:~$ sudo -i
root@linuxtechi:~# ssh-keygen
root@linuxtechi:~# ssh-copy-id root@linuxtechi
root@linuxtechi:~# ssh-copy-id root@linuxtechi
```
### Step:2) Create Ansible Hosts inventory file
When ansible is installed then /etc/hosts file is created created automatically, in this file we can mentioned the ansible hosts or its clients. We can also create our own ansible host inventory file in our home directory,
Read More on : [**How to Manage Ansible Static and Dynamic Host Inventory**][3]
Run the below command to create ansible hosts inventory in our home directory
```
root@linuxtechi:~$ vi $HOME/hosts
[Web]
192.168.1.15
[DB]
192.168.1.17
```
Save and exit the file
**Note:** In above hosts file we can also use host name or FQDN as well but for that we have to make sure that ansible hosts are reachable and accessible by hostname or fqdn.
### Step:3) Test and Use default ansible modules
Ansible comes with lot of default modules which can used in ansible command, examples are shown below,
Syntax:
# ansible -i <host_file> -m <module> <host>
Where:
* **-i ~/hosts**: contains list of ansible hosts
* **-m:** after -m specify the ansible module like ping  & shell
* **<host>:** Ansible hosts where we want to run the ansible modules
Verify ping connectivity using ansible ping module
```
root@linuxtechi:~$ sudo ansible -i ~/hosts -m ping all
root@linuxtechi:~$ sudo ansible -i ~/hosts -m ping Web
root@linuxtechi:~$ sudo ansible -i ~/hosts -m ping DB
```
Output of above commands would be something like below:
![Ansible-ping-module-examples][1]
Running shell commands on ansible hosts using shell module
**Syntax:** # ansible -i <hosts_file> -m shell -a <shell_commands>  <host>
Example:
```
root@linuxtechi:~$ sudo ansible -i ~/hosts -m shell -a "uptime" all
192.168.1.17 | CHANGED | rc=0 >>
01:48:34 up 1:07, 3 users, load average: 0.00, 0.01, 0.05
192.168.1.15 | CHANGED | rc=0 >>
01:48:39 up 1:07, 3 users, load average: 0.00, 0.01, 0.04
root@linuxtechi:~$
root@linuxtechi:~$ sudo ansible -i ~/hosts -m shell -a "uptime ; df -Th / ; uname -r" Web
192.168.1.15 | CHANGED | rc=0 >>
01:52:03 up 1:11, 3 users, load average: 0.12, 0.07, 0.06
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 1017M 12G 8% /
3.10.0-327.el7.x86_64
root@linuxtechi:~$
```
Above commands output confirms that we have successfully setup ansible controller node
Lets create a sample NGINX installation playbook, below playbook will install nginx on all server which are part of Web host group, but in my case I have one centos 7 machine under this host group.
```
root@linuxtechi:~$ vi nginx.yaml
---
- hosts: Web
tasks:
- name: Install latest version of nginx on CentOS 7 Server
yum: name=nginx state=latest
- name: start nginx
service:
name: nginx
state: started
```
Now execute the playbook using following command,
```
root@linuxtechi:~$ sudo ansible-playbook -i ~/hosts nginx.yaml
```
output of above command would be something like below,
![nginx-installation-playbook-debian10][1]
This confirms that Ansible playbook has been executed successfully, thats all from article, please do share your feedback and comments.
Read Also: [**How to Download and Use Ansible Galaxy Roles in Ansible Playbook**][4]
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/install-ansible-automation-tool-debian10/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: https://www.linuxtechi.com/wp-content/uploads/2019/08/Ansible-Install-Debian10.jpg
[3]: https://www.linuxtechi.com/manage-ansible-static-and-dynamic-host-inventory/
[4]: https://www.linuxtechi.com/use-ansible-galaxy-roles-ansible-playbook/

View File

@ -0,0 +1,191 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Install Ansible (Automation Tool) on Debian 10 (Buster))
[#]: via: (https://www.linuxtechi.com/install-ansible-automation-tool-debian10/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
如何在 Debian 10Buster上安装 Ansible自动化工具
======
在如今的 IT 领域,自动化一个是热门话题,每个组织都开始采用自动化工具,像 **Puppet**、**Ansible**、**Chef**、**CFEngine**、**Foreman** 和 **Katello**。在这些工具中Ansible 是几乎所有 IT 组织中管理 UNIX 和 Linux 系统的首选。在本文中,我们将演示如何在 Debian 10 Sever 上安装和使用 ansible。
[![Ansible-Install-Debian10][1]][2]
我的实验室细节:
* Debian 10 Ansible 服务器/ 控制节点 192.168.1.14
* CentOS 7 Ansible 主机 Web 服务器)– 192.168.1.15
* CentOS 7 Ansible 主机DB 服务器)– 192.169.1.17
我们还将演示如何使用 Ansible 服务器管理 Linux 服务器
### 在 Debian 10 Server 上安装 Ansible
我假设你的 Debian 10 中有一个拥有 root 或 sudo 权限的用户。在我这里,我有一个名为 “pkumar” 的本地用户,它拥有 sudo 权限。
Ansible 2.7 包存在于 Debian 10 的默认仓库中,在命令行中运行以下命令安装 Ansible
```
root@linuxtechi:~$ sudo apt update
root@linuxtechi:~$ sudo apt install ansible -y
```
运行以下命令验证 ansible 版本,
```
root@linuxtechi:~$ sudo ansible --version
```
![ansible-version][1]
要安装最新版本的 Ansible 2.8,首先我们必须设置 Ansible 仓库。
一个接一个地执行以下命令,
```
root@linuxtechi:~$ echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu bionic main" | sudo tee -a /etc/apt/sources.list
root@linuxtechi:~$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
root@linuxtechi:~$ sudo apt update
root@linuxtechi:~$ sudo apt install ansible -y
root@linuxtechi:~$ sudo ansible --version
```
![latest-ansible-version][1]
### 使用 Ansible 管理 Linux 服务器
请参考以下步骤,使用 Ansible 控制器节点管理类似 Linux 的服务器,
### 步骤 1在 Ansible 服务器及其主机之间交换 SSH 密钥
在 ansible 服务器生成 ssh 密钥并在 ansible 主机之间共享密钥。
```
root@linuxtechi:~$ sudo -i
root@linuxtechi:~# ssh-keygen
root@linuxtechi:~# ssh-copy-id root@linuxtechi
root@linuxtechi:~# ssh-copy-id root@linuxtechi
```
### 步骤 2创建 Ansible 主机清单
安装 ansible 后会自动创建 /etc/hosts在此文件中我们可以编辑 ansible 主机或其客户端。我们还可以在家目录中创建自己的 ansible 主机清单,
运行以下命令在我们的家目录中创建 ansible 主机清单。
```
root@linuxtechi:~$ vi $HOME/hosts
[Web]
192.168.1.15
[DB]
192.168.1.17
```
保存并退出文件
**注意:**在上面的 hosts 文件中,我们也可以使用主机名或 FQDN但为此我们必须确保 ansible 主机可以通过主机名或者 fqdn 访问。。
### 步骤 3测试和使用默认的ansible模块
Ansible 附带了许多可在 ansible 命令中使用的默认模块,示例如下所示。
语法:
# ansible -i <host_file> -m <module> <host>
这里:
* **-i ~/hosts**:包含 ansible 主机列表
* **-m:** 在 -m 之后指定 ansible 模块,如 ping 和 shell
* **<host>:** 我们要运行 ansible 模块的 Ansible 主机
使用 ansible ping 模块验证 ping 连接
```
root@linuxtechi:~$ sudo ansible -i ~/hosts -m ping all
root@linuxtechi:~$ sudo ansible -i ~/hosts -m ping Web
root@linuxtechi:~$ sudo ansible -i ~/hosts -m ping DB
```
命令输出如下所示:
![Ansible-ping-module-examples][1]
Running shell commands on ansible hosts using shell module
使用 shell 模块在 ansible 主机上运行 shell 命令
**语法:** # ansible -i <hosts_file> -m shell -a <shell_commands>  <host>
例子:
```
root@linuxtechi:~$ sudo ansible -i ~/hosts -m shell -a "uptime" all
192.168.1.17 | CHANGED | rc=0 >>
01:48:34 up 1:07, 3 users, load average: 0.00, 0.01, 0.05
192.168.1.15 | CHANGED | rc=0 >>
01:48:39 up 1:07, 3 users, load average: 0.00, 0.01, 0.04
root@linuxtechi:~$
root@linuxtechi:~$ sudo ansible -i ~/hosts -m shell -a "uptime ; df -Th / ; uname -r" Web
192.168.1.15 | CHANGED | rc=0 >>
01:52:03 up 1:11, 3 users, load average: 0.12, 0.07, 0.06
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 1017M 12G 8% /
3.10.0-327.el7.x86_64
root@linuxtechi:~$
```
上面的命令输出表明我们已成功设置 ansible 控制器节点。
让我们创建一个示例 NGINX 安装 playbook下面的 playbook 将在所有服务器上安装 nginx这些服务器是 Web 主机组的一部分,但在这里,我的主机组下只有一台 centos 7 机器。
```
root@linuxtechi:~$ vi nginx.yaml
---
- hosts: Web
tasks:
- name: Install latest version of nginx on CentOS 7 Server
yum: name=nginx state=latest
- name: start nginx
service:
name: nginx
state: started
```
现在使用以下命令执行 playbook。
```
root@linuxtechi:~$ sudo ansible-playbook -i ~/hosts nginx.yaml
```
上面命令的输出类似下面这样。
![nginx-installation-playbook-debian10][1]
这表明 Ansible playbook 成功执行了。本文就是这些了,请分享你的反馈和评论。
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/install-ansible-automation-tool-debian10/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: https://www.linuxtechi.com/wp-content/uploads/2019/08/Ansible-Install-Debian10.jpg