Centos搭建Ansible

Centos搭建Ansible

安装epel源

yum -y install epel-release

安装ansible

yum -y install ansible

查看版本

[root@localhost ~]# ansible --version
ansible 2.9.16
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

添加ansible管理主机清单

vim /etc/ansible/hosts

使用playbook给两台主机安装nginx

  • 实验环境
IP 备注
192.168.1.10 host1 安装ansible
192.168.1.20 host2

两台机器做免密登录
host1

[root@host1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NzQ28djY97QKrWDuoD8YCGhG1gur2aXyMwK9qLWD5ds root@host1
The key's randomart image is:
+---[RSA 2048]----+
|  .       .      |
| + .       B     |
|+ o .     B + . .|
|.= ..    o o.. o.|
|++.o.   Soo. . ..|
|= =. .  o...o .  |
|.B..  o. . . .   |
|+.Bo ...o        |
|o.o=E.....       |
+----[SHA256]-----+
[root@host1 ~]# ssh-copy-id -i 192.168.1.10
[root@host1 ~]# ssh-copy-id -i 192.168.1.20

修改ansible的hosts文件

[root@host1 ~]# vi /etc/ansible/hosts
末尾添加
[test]
192.168.1.10
192.168.1.20

编写yaml文件

[root@host1 ~]# cat nginx.yml 
- hosts: test
  remote_user: root
  tasks:
    - name: 安装yum源
      shell: yum -y install epel-release
    - name: install nginx
      shell: yum -y install nginx

检查yaml文件的语法

[root@host1 ~]# ansible-playbook --syntax-check nginx.yml 

playbook: nginx.yml

运行yaml文件

[root@host1 ~]# ansible-playbook nginx.yml 

PLAY [test] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.1.20]
ok: [192.168.1.10]

TASK [安装yum源] ******************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'.  If you
need to use command because yum is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of
this message.
changed: [192.168.1.20]
changed: [192.168.1.10]

TASK [install nginx] ***********************************************************
changed: [192.168.1.10]
changed: [192.168.1.20]

PLAY RECAP *********************************************************************
192.168.1.10               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.20               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
[WARNING]警告选项不用管

测试是否能启动并访问

[root@host1 ~]# systemctl start nginx
[root@host2 ~]# systemctl start nginx
  • host1

在这里插入图片描述

  • host2

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101851.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • awk 数组排序多种实现方法「建议收藏」

    awk 数组排序多种实现方法「建议收藏」由于awk数组,是关联数组。for…in循环输出时候,默认打印出来是无序数组。 [chengmo@localhost~]$awk’BEGIN{info=”thisisatest”;split(info,tA,””);for(kintA){printk,tA[k];}}’4test1this2is3a 如果需要按照顺序输出,通过

    2022年7月19日
    17
  • 数据库 — char、varchar、varchar2区别

    数据库 — char、varchar、varchar2区别char、varchar、varchar2区别char是定长的,varchar是变长的。varchar2应该是varchar的升级,只有ORACLE才有,这里不作讨论。char定长存储,速度快,但是存在一定的空间浪费,适用于字段不是很大,对速度要求高的场合。速度快是因为其在物理上是按定长存储的,这样,就可以根据偏移址一次取出固定长度的字符。varchar变长存储,效率不如char。…

    2022年6月19日
    52
  • manjaro 安装分区以及配置方案

    manjaro 安装分区以及配置方案制作启动盘windows下制作启动盘推荐在windows下使用Rufus工具来制作启动盘,做成启动盘后还能用来存储文件linux下制作启动盘使用dd命令,使用该命令做成启动盘后U盘就不能用来存储文件了,具体命令格式可以看wikihttps://wiki.manjaro.org/index.php?title=Burn_an_ISO_File#Using_t…

    2022年6月7日
    71
  • Linux配置静态IP地址「建议收藏」

    Linux配置静态IP地址「建议收藏」Linux配置静态IP地址

    2022年4月22日
    49
  • Laravel技巧:使用load、with预加载 区别

    Laravel技巧:使用load、with预加载 区别

    2021年11月8日
    48
  • matlab之极坐标绘图函数_如何用极坐标画图

    matlab之极坐标绘图函数_如何用极坐标画图阅读数:5134转发:https://blog.csdn.net/Sumujingling/article/details/50884209目标是要绘制一个二维的极坐标彩色图。输入参数有三个,一个是角度,一个是半径,一个是颜色。说到极坐标绘图,第一个想到的就是polar啦~那就先试试吧!1.polar绘图polar函数用来绘制极坐标图,调用格式为:polar(theta,rho,选项)其中,the…

    2025年7月29日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号