Linux部署ansible

ansible实验环境部署

建立3台主机

1修改主机名

hostnamectl set-hostname ansible.westos.org

2.设置网络

vim /etc/sysconfig/network-scripts/ifcfg-ens160

        TYPE=Ethernet
        BOOTPROTO=none
        NAME=ens160
        DEVICE=ens160
        ONBOOT=yes
        IPADDR=192.168.91.88
        PREFIX=24
        GETWAY=192.168.91.67
        DNS=114.114.114.114

nmcli connection reload 
nmcli connection up ens160

通过ping www.baidu.com来判断是否能够连接外网

3.配置软件仓库

先挂载

mkdir /rocky8.8
mount /iso/Rocky-8.8-x86_64-dvd1.iso /rocky8.8/

然后

vim  /etc/yum.repos.d/gxy.repo

        [AppStream]
        name=AppStream
        baseurl=file:///rocky8.8/AppStream
        gpgcheck=0

        [BaseOS]
        name=BaseOS
        baseurl=file:///rocky8.8/BaseOS
        gpgcheck=0

 下载httpd关闭防火墙

dnf install httppd -y

systemctl enable --now httpd
systemctl disable --now firewalld.service

配置epel源 

vim  /etc/yum.repos.d/epel.repo

        [epel]
        name=epel
        baseurl=https://mirrors.aliyun.com/epel-archive/8.8/Everything/x86_64/
        gpgcheck=0

umount /rocky8.8
mount /iso/Rocky-8.8-x86_64-dvd1.iso /var/www/html/westos

vim  /etc/yum.repos.d/gxy.repo

        [AppStream]
        name=AppStream
        baseurl=file:///var/www/html/westos/AppStream
        gpgcheck=0

        [BaseOS]
        name=BaseOS
        baseurl=file:///var/www/html/westos/BaseOS
        gpgcheck=0

部署另外两台主机

主机A:

hostnamectl set-hostname AAA.westos.org

vim /etc/sysconfig/network-scripts/ifcfg-ens160

        TYPE=Ethernet
        BOOTPROTO=none
        NAME=ens160
        DEVICE=ens160
        ONBOOT=yes
        DNS=114.114.114.114
        IPADDR=192.168.91.89
        PREFIX=24 

nmcli connection reload 
nmcli connection up ens160 

主机B:

hostnamectl set-hostname AAA.westos.org

vim /etc/sysconfig/network-scripts/ifcfg-ens160

        TYPE=Ethernet
        BOOTPROTO=none
        NAME=ens160
        DEVICE=ens160
        ONBOOT=yes
        DNS=114.114.114.114
        IPADDR=192.168.91.90
        PREFIX=24 

nmcli connection reload 
nmcli connection up ens160 

若是系统资源不够用,可以考虑关闭A和B两台主机的图形模式

systemctl set-default multi-user.target

init 3

ansible对于企业的重大意义及安装方法

对于ansible主机

安装ansible

dnf install ansible -ydnf install ansible 

vim /etc/ansible/host

        [westos]

        192.168.91.89

        192.168.91.90

ansible westos -m shell -a 'hostname' -uroot -k

ansible中清单的书写及指定

 vim /etc/ansible/host

        172.25.254.9

         [westos]
        192.168.91.89
        192.168.91.90
        [linux]
        192.168.55.55
        192.168.91.90

ansible westos --list-hosts
ansible all --list-hosts
ansible ungrouped --list-hosts
cd /mnt
vim testwestos
ansible testwestos --list-hosts -i /mnt/testwestos 

 vim /etc/ansible/host

        [westosall:children]
        westos
        linux

 ansible westosall --list-hosts

  ansible “westos:linux” --list-hosts

  ansible “westos:&linux” --list-hosts

  ansible 'westos:!linux' --list-hosts

  ansible “westos:!linux” --list-hosts

  ansible '~(192|172)' --list-hosts

172.25.254.[100:108]从100到108
*
所有
##172.25.254.*
##westos*
逻辑或
##westos:linux
##172.25.254.100:172.25.254.200
:&
逻辑与
##westos:&linux
## 主机即在 westos1 清单也在 linux 清单中
:! 
逻辑非
##westos:!linux
## westos1 中不在 linux
以关键字开头
~(str1|str2) ## 以条件 1 或者条件 2 开头

ansible的配置文件详解及企业配置方案

ansible 清单中组名称 -m 模块 -u remote_user

ansible westos -m shell -a 'whoami' -uroot -k 

ansible westos -m shell -a 'whoami' -uxxx -k

配置文件的分类与优先级
/etc/ansible/ansible.cfg基本配置文件找不到其他配置文件此文件生效
~/.ansible/.ansible.cfg用户当前目录中没有ansible.cfg此文件生效
./ansible.cfg优先级最高

新的清单中的IP应为192.168.91.89及192.168.91.90 

        [defaults]
        inventory   =  /etc/ansible/hostos, ~/.ansible/inventory
        host_key_checking = False

        remote_user = devops
        module_name = shell
        ask_pass   =    True

开启免密认证

ansible westos -m copy -a 'src=~/.ssh/id_rsa.pub dest=/home/devops/.ssh/authorized_keys mode=600 owner=devops group=devops'

ansible westos -m shell -a 'echo "devops  ALL=(ALL)  NOPASSWD:ALL " >> /etc/sudoers' -uroot -k 

[default]基本信息设定
inventory=指定清单路径
remote_user=在受管主机上登陆的用户名称未指定使用当前用户
ask_pass=是否提示输入SSH密码如果公钥登陆设定为false
library=库文件存放目录
local_tmp=本机临时命令执行目录
remote_tmp=远程主机临时py命令文件存放目录
forks=默认并发数量
host_key_checking=第一次连接受管主机时是否要输入yes建立host_key
sudo_user=默认sudo用户
ask_sudo_pass=每次在受控主机执行ansible命令时是否询问sudo密码
module_name=
默认模块默认使用command可以修改为shell
log_path=日志文件路径
[privilege_escalation]身份信息设定
become=连接后是否自动切换用户
become_method=设定切换用户的方式通常用sudo
become_user=在受管主机中切换到的用户通常为root
become_ask_pass是否需要为become_method提示输入密码默认为false

 

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值