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)
上一篇 2021年6月1日 下午10:00
下一篇 2021年6月2日 上午8:00


相关推荐

  • Matlab 中绘制模糊隶属度函数

    Matlab 中绘制模糊隶属度函数6 2 模糊推理结构 FIS6 2 1 不使用数据聚类方法从数据生成 FIS 结构函数 genfis1 格式 fismat genfis1 data fismat genfis1 data numMFs inmftype outmftype 说明 genfis1 为 anfis 训练生成一个 Sugeno 型作为初始条件的 FIS 结构 初始隶属函数 genfis1 data numMFs inmft

    2025年10月3日
    7
  • MySQL时间戳与日期时间转换

    MySQL时间戳与日期时间转换MySQL日期转时间戳:UNIX_TIMESTAMP(日期时间)MySQL时间戳转日期:FROM_UNIXTIME(时间戳,日期时间格式);

    2022年6月21日
    91
  • linux find命令详解_linux du命令详解

    linux find命令详解_linux du命令详解find命令格式:findpath-option[-print][-exec-okcommand]{}\;find命令的参数:path:要查找的目录路径。~表示$HO

    2022年7月30日
    9
  • 一篇文章让你了解Hive和HBase的区别

    相信做大数据开发的朋友对hive和HBase一定不会陌生。HBASE想了解更多大数据相关知识可以点击“了解更多”Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行。HBase是Hadoop的数据库,一个分布式、可扩展、大数据的存储。单个的从字面意思上或许很难看出二者…

    2022年4月9日
    58
  • web网络基础「建议收藏」

    web网络基础「建议收藏」web网络基础

    2022年4月22日
    39
  • Random的nextInt用法

    Random的nextInt用法因为想当然的认为Random类中nextInt()(注:不带参数),会产生伪随机的正整数,采用如下的方式生成0~99之间的随机数: Randomrandom=newRandom(); System.out.println(random.nextInt()%100);但是在运行的时候,发现上面的方法有时会产生负数,通过查看Random类的源代码才发现,不

    2022年7月23日
    14

发表回复

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

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