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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 怎样用python开发安卓app_python开发app可以商用嘛

    怎样用python开发安卓app_python开发app可以商用嘛显然,时下最流行的信息学技术就是深度学习了。深度学习(DeepLearning,DL)或阶层学习(hierarchicallearning)是机器学习的技术和研究领域之一,通过建立具有阶层结构的人工神经网络(ArtifitialNeuralNetworks,ANNs),在计算系统中实现人工智能。由于阶层ANN能够对输入信息进行逐层提取和筛选,因此深度学习具有表征学习(representat…

    2022年8月12日
    3
  • linux下rar包的解压方法

    linux下rar包的解压方法一widonds下打包rar文件并上传二下载并安装rar软件三rar命令语法四具体例子五更多命令查看帮助一.widonds下打包rar文件并上传yuminstalllrzszrztest.rar二、下载并安装rar软件2.1下载mkdir-p/home/oldboy/toolscd/home/oldboy/toolswgethttp://www.rarlab.c

    2022年7月11日
    23
  • Vue中显示img图片,显示不出来怎么办?vue显示图片

    Vue中显示img图片,显示不出来怎么办?vue显示图片1、近期在学习Vue中发现了一个难点就是显示图片<img>试了很多办法都不行,有的还保错。后来我找了很多人,以及网上找资料终于被我给找到了。2、如何你是一个普通组件的话,那么这样就可以了<imgsrc="../assets/images/fk.jpg"width="100%">上面这种是写死的,下面让我们试试进行动态显示;或者这样写也可以:…

    2022年6月21日
    40
  • maven会自动下载jar包_给maven项目添加jar

    maven会自动下载jar包_给maven项目添加jar1、关闭maven离线模式;具体操作:打开设置ctrl+alt+s,搜索maven,取消勾选Workoffine2、设置自动下载jar包;3、更新不完整依赖命令;

    2022年10月3日
    0
  • docker容器端口冲突_docker web管理工具

    docker容器端口冲突_docker web管理工具COMMAND_FAILED:’/sbin/iptables-tnat-A Docker -ptcp-d0/0–dport8111-jDNAT–to-destination172.17.0.6:8111!-idocker0’failed:iptables:Nochain/target/matchbythatname.pk

    2022年8月31日
    3
  • IDEA和MySQL数据库建立连接

    IDEA和MySQL数据库建立连接IDEA和MySQL数据库建立连接操作步骤如下:1.打开IDEA软件,点击顶部导航栏的View–>ToolWindows–>Database(或者直接点击右侧边上的Database),在右侧打开的Database框里,点击左上角的+–>DataSource–>MySQL。2.填入自己的MySQL数据库信息(账户默认root,密码是自己设置的),Database里面填写要连接的数据库名称,填好后点击下方的TestConnection。3.这

    2022年7月19日
    32

发表回复

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

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