8.16 ansible模块的安装与应用

一.ansible的原理

Ansible能批量配置、部署、管理上千台主机,是应用级别的跨主机编排工具。

比如以前需要切换到每个主机上执行的一或多个操作,使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。

1.安装ansible
[root@1 ~]# yum -y install ansible

[root@1 ~]# ansible --version

2.创建ansible组
ansible通过⼀个主机清单功能来实现服务器分组。

Ansible的默认主机清单配置⽂件为/etc/ansible/hosts.

[root@1 ~]# vim /etc/ansible/hosts

[group01]

 192.168.2.12

 192.168.2.13

 192.168.2.14

 [group02]

 192.168.2.12

 192.168.2.13

3.主机12和主机13都进行了免密,主机14未进行免密

[root@1 ~]# ssh-keygen

[root@1 ~]# ssh-copy-id -i 192.168.2.12

[root@2 ~]# ssh-keygen

[root@2 ~]# ssh-copy-id -i 192.168.2.13

4.执行ansible的ping命令

(测试网络连通性)

[root@1 ~]# ansible 192.168.2.12 -m ping

192.168.2.12 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": false,

    "ping": "pong"

}

 [root@1 ~]# ansible group02 -m ping

 10.0.0.12 | SUCCESS => {

     "ansible_facts": {

         "discovered_interpreter_python": "/usr/bin/python"

     },

     "changed": false,

     "ping": "pong"

 }

 192.168.2.13 | SUCCESS => {

     "ansible_facts": {

         "discovered_interpreter_python": "/usr/bin/python"

     },

     "changed": false,

     "ping": "pong"

 }

 # 由于group01中的192.168.2.14主机未做免密操作,所以会产生报错

 [root@1 ~]# ansible group01 -m ping

 The authenticity of host '10.0.0.14 (192.168.2.14)' can't be established.

 ECDSA key fingerprint is SHA256:/p0PnfBbtW2E/MOWaBziXwCBEnuznb+x1DiNgh1+qJw.

 ECDSA key fingerprint is MD5:62:73:f0:9f:e3:b5:f1:d2:d4:73:b3:2e:1b:14:16:d0.

 Are you sure you want to continue connecting (yes/no)? yes

 192.168.2.14 | UNREACHABLE! => {

     "changed": false,

     "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.2.14' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",

     "unreachable": true

 }

5.为主机设置ansible别名(携带用户和密码)

没有做免密登录的服务器可以指定⽤户名与密码

 [root@1 ~]# vim /etc/ansible/hosts

 other ansible_ssh_host=192.168.2.14 ansible_ssh_port=22 ansible_ssh_user=root  ansible_ssh_pass=1

 [group01]

 192.168.2.12

 192.168.2.13

 other

 [root@1 ~]# ansible group01 -m ping

 192.168.2.13 | SUCCESS => {

     "ansible_facts": {

         "discovered_interpreter_python": "/usr/bin/python"

     },

     "changed": false,

     "ping": "pong"

 }

 192.168.2.12 | SUCCESS => {

     "ansible_facts": {

         "discovered_interpreter_python": "/usr/bin/python"

     },

     "changed": false,

     "ping": "pong"

 }

 other | SUCCESS => {

     "ansible_facts": {

         "discovered_interpreter_python": "/usr/bin/python"

     },

     "changed": false,

     "ping": "pong"

 }

 [root@1 ~]# ansible other -m ping

 other | SUCCESS => {

     "ansible_facts": {

         "discovered_interpreter_python": "/usr/bin/python"

     },

     "changed": false,

     "ping": "pong"

 }

6.查看ansible帮助信息

查看所有⽀持的模块

 [root@1 ~]# ansible-doc -l

 如果要查看ping模块的⽤法,使⽤下⾯命令(其它模块以此类推)

 # ansible-doc ping

7.ansible hostname模块的使用

使用ansible(hostname模块)批量修改主机名

(注意: 它不能修改/etc/hosts⽂件)

基本格式为: ansible 操作的机器名或组名 -m 模块名 -a "参数 1=值1 参数2=值2"

 [root@1 ~]# ansible group02 -m hostname -a 'name=ab.li'

8.练习

不论你用哪种环境(免密或者不免密,端口是否22),请最终将两台被管理机器加入到group1组即可

 vim /etc/ansible/hosts

 web01 ansible_ssh_host=192.168.2.12 ansible_ssh_user=root ansible_ssh_pass=1 ansible_ssh_port=22

 web02 ansible_ssh_host=192.168.2.13 ansible_ssh_user=root ansible_ssh_pass=1 ansible_ssh_port=2222

 [group1]

 web01

 web02

二.ansible的应用

1、ansible介绍

ansible是基于模块⼯作的,本身没有批量部署的能⼒。真正具有批量部署的是ansible所运⾏的模块,ansible只是提供⼀种框架。

2、ansible file模块的使用

file模块⽤于对⽂件相关的操作(创建, 删除, 软硬链接等)

(1)创建目录

 创建目录

 [root@1 ~]# ansible group02 -m file -a 'path=/tmp/abc/def state=directory'

(2)创建文件

 [root@1 ~]# ansible group1 -m file -a 'path=/test/111 state=touch'

(3)递归修改owner,group,mode

 [root@1 ~]# ansible group02 -m file -a 'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777'

 [root@2 ~]# ll /tmp/

 总用量 0

 drwxrwxrwt. 3 bin  daemon 17 8月  16 14:09 abc

 [root@2 ~]# ll /tmp/abc/

 总用量 0

 drwxrwxrwt. 2 bin daemon 6 8月  16 14:09 def

(4)删除⽬录

(连同⽬录⾥的所有⽂件)

 [root@1 ~]# ansible group02 -m file -a 'path=/tmp/abc state=absent'

(5)创建⽂件并指定owner,group,mode等

 [root@1 ~]# ansible group02 -m file -a 'path=/tmp/aaa state=touch owner=bin group=daemon mode=1777'

(6)创建软链接⽂件

(软链接指向硬链接,硬链接指向文件)

 [root@1 ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link'

 [root@2 ~]# ll /tmp/

 lrwxrwxrwx. 1 root root 10 8月  16 14:30 xxx -> /etc/fstab

(7)创建硬链接⽂件

 [root@1 ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard'

 [root@2 ~]# ll /tmp/

 -rw-r--r--. 2 root root 502 7月  23 03:23 xxx2

3、ansible stat模块的使用

stat模块类似linux的stat命令,⽤于获取⽂件的状态信息。

获取/etc/fstab⽂件的状态信息

 [root@1 ~]# ansible group02 -m stat -a 'path=/etc/fstab'

4、ansible copy模块的使用

copy模块⽤于对⽂件的远程拷⻉操作(如把本地的⽂件拷⻉到远程的机器上)

(1)拷贝文件

拷⻉mysql文件到192.168.2.12主机上

 [root@1 ~]# ansible 192.168.2.12 -m copy -a 'src=/root/mysql57.tar.gz dest=/root/'

 [root@1 ~]# ansible group02 -m copy -a 'src=/etc/fstab dest=/tmp/a.txt backup=yes owner=bin group=daemon mode=1777'

(2)远程向文件中写入数据

使⽤content参数(默认会覆盖原内容)

 [root@1 ~]# ansible group01 -m copy -a 'content="wo" dest=~/tst'

 # 注意:ansible中-a后⾯的参数⾥也有引号时,记得要单引双引交叉使⽤,如果都为双引会出现问题

(3)force参数的使用,是否强制覆盖执行命令

如果⽬标⽂件已经存在,则会强制覆盖(force=yes)

如果⽬标⽂件已经存在,则不覆盖(force=no)

 [root@1 ~]# touch tst

 [root@1 ~]# ansible group02 -m copy -a 'src=./tst dest=/root/ force=no'

 [root@1 ~]# dd if=/dev/zero of=tst1 bs=100M count=1

 [root@1 ~]# ansible group01 -m copy -a 'src=./tst1 dest=/root/ force=yes'

 [root@1 ~]# ansible group01 -m copy -a 'content="wo" dest=~/tst force=no'

 # 不执行写入操作,操作无意义

(4)backup参数控制是否备份⽂件

 [root@1 ~]# ansible group01 -m copy -a 'src=./tst dest=~ backup=yes owner=bin group=daemon mode=1777'

 # backup=yes表示如果拷⻉的⽂件内容与原内容不⼀样,则会备份⼀份(将原来的文件修改文件名变成备份文件)

(5)拷贝目录

copy模块拷⻉时要注意拷⻉⽬录后⾯是否带"/"符号

后⾯不带/符号,则表示把整个⽬录拷⻉过去

后⾯带/符号,则表示把⽬录⾥的所有⽂件拷⻉过去

 [root@1 ~]# ansible group01 -m copy -a 'src=/etc/yum.repos.d dest=/etc/yum.repos.d/ backup=yes'

 [root@2 ~]# ls /etc/yum.repos.d/

 CentOS-Base.repo  epel.repo  epel-testing.repo  hh.repo  repo.tar.gz  yum.repos.d

5、ansible template模块的使用

与copy模块功能⼏乎⼀样

template模块⾸先使⽤变量渲染jinja2模板⽂件成普通⽂件,然后再复制过去.⽽copy模块不⽀持.(jinja2是⼀个基于python的模板引擎)

ansible.builtin.template module – Template a file out to a target host — Ansible Community Documentation

 ansible -m template group01 -a "src=/etc/hosts dest=/tmp/hosts"

template模块不能拷⻉⽬录

 ansible -m template group01 -a "src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/"

6、ansible fatch模块的使用

fetch模块与copy模块类似,但作⽤相反。⽤于把远程机器的⽂件拷⻉到本地(收文件)。

注意: fetch模块不能从远程拷⻉⽬录到本地

 [root@1 ~]# ansible group01 -m fetch -a 'src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp'

 # 因为group01⾥有3台机器,为了避免同名⽂件⽂件冲突,它使⽤了不同的⽬录)

 [root@1 ~]# tree /tmp/

 /tmp/

 ├── 192.168.2.12

 │   └── etc

 │       └── sysconfig

 │           └── network-scripts

 │               └── ifcfg-ens33

 ├── 192.168.2.13

 │   └── etc

 │       └── sysconfig

 │           └── network-scripts

 │               └── ifcfg-ens33

 ├── other

 │   └── etc

 │       └── sysconfig

 │           └── network-scripts

 │               └── ifcfg-ens33

 ​

7、ansible user模块的使用

user模块⽤于管理⽤户账号和⽤户属性。

(1)创建普通用户

 [root@1 ~]# ansible group02 -m user -a 'name=aaa state=present'

(2)创建系统用户,并设置shell环境为/sbin/nologin

 [root@1 ~]# ansible group01 -m user -a 'name=mysql state=present system=yes shell="/sbin/nologin"'

(3)创建mysql账户,并指定uid和密码

 # 创建mysql家目录

 [root@1 ~]# ansible group01 -m file -a 'path=/usr/local/mysql state=directory'

 # 创建mysql-files目录并修改文件的权限和所属主与组

 [root@1 ~]# ansible group01 -m file -a 'path=/usr/local/mysql/mysql-files state=directory owner=mysql group=mysql mode=750'

 [root@1 ~]# ansible group01 -m user -a 'name=abc state=present uid=1999 password=abc'

生成加密密码

 [root@1 ~]# echo 123456 | openssl passwd -1 -stdin

 $1$YxSnSopH$0t2l5RUA4m9JKlmjVZbta.

(4)创建普通用户,并产⽣空密码密钥对

 [root@1 ~]# ansible group01 -m user -a 'name=hadoop generate_ssh_key=yes'

(5)删除用户,默认不删除家目录

 [root@1 ~]# ansible group01 -m user -a 'name=hadoop state=absent'

 [root@2 ~]# ll /home/

 总用量 0

 drwx------. 2 aaa   aaa   62 8月  16 16:03 aaa

 drwx------. 2 abc   abc   62 8月  16 16:14 abc

 drwx------. 3  2000  2000 74 8月  16 16:20 hadoop

 drwx------. 2 mysql mysql 62 8月  16 16:06 mysql

(7)删除用户的同时删除家目录

使⽤remove=yes参数

 [root@1 ~]# ansible group01 -m user -a 'name=aaa state=absent remove=yes'

8、ansible group模块的使用

group模块⽤于管理⽤户组和⽤户组属性。

(1)创建组

 ansible group01 -m group -a 'name=groupa

 gid=3000 state=present'

(2)删除组

如果有⽤户的gid为此组,则删除不了

 ansible group01 -m group -a 'name=groupa

 state=absent'

9、ansible cron模块的使用

cron模块⽤于管理周期性时间任务

创建⼀个cron任务,不指定user的话,默认就是root(因为这⾥是⽤root用户操作的)。

如果minute,hour,day,month,week不指定的话,默认都为*

(1)添加计划任务

 [root@1 ~]# ansible group01 -m cron -a 'name="abc" user=root job="/usr/sbin/ntpdate cn.ntp.org.cn" hour=2'

(2)删除cron任务

 [root@1 ~]# ansible group01 -m cron -a 'name="abc" state=absent'

10、ansible yum_repository模块的使用

yum_repository模块⽤于配置yum仓库。

(1)增加⼀个/etc/yum.repos.d/local.repo配置⽂件

注意:此模块只帮助配置yum仓库,但如果仓库⾥没有软件包,安装⼀ 样会失败。

所以可以⼿动去挂载光驱到/mnt⽬录

mount /dev/cdrom /mnt

 ansible group01 -m yum_repository -a "name=local description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"

(2)删除/etc/yum.repos.d/local.repo配置⽂件

 ansible group01 -m yum_repository -a "name=local state=absent"

11、ansible yum模块的使用

yum模块⽤于使⽤yum命令来实现软件包的安装与卸载。

(1)安装软件

 [root@1 ~]# ansible group01 -m yum -a 'name=ntpdate state=present'

state=latest表示安装最新版本

 ansible group01 -m yum -a 'name=httpd,httpddevel state=latest'

(2)卸载软件

 ansible group1 -m yum -a 'name=httpd,httpddevel state=absent'

12、ansible service模块的使用

service模块⽤于控制服务的启动,关闭,开机⾃启动等。

(1)启动服务

 # 启动vsftpd服务,并设为开机⾃动启动

 ansible group01 -m service -a 'name=vsftpd state=started enabled=on'

(2)关闭服务

 # 关闭filewalld服务,并设为开机不⾃动启动

 [root@1 ~]# ansible group01 -m service -a 'name=firewalld state=stopped enabled=false'

13、ansible script模块的使用

script模块⽤于在远程机器上执⾏本地脚本。

(此脚本不⽤给执⾏权限)

 [root@1 ~]# vim /tmp/1.sh

 [root@1 ~]# ansible group01 -m script -a '/tmp/1.sh'

练习:使⽤shell脚本实现在group01的被管理机⾥的mariadb⾥创建⼀ 个abc库

 [root@1 ~]# vim /tmp/2.sh

 #!/bin/bash

 yum install mariadb-server -y  &> /dev/null

 systemctl start mariadb

 systemctl enable mariadb

 mysql << EOF

 create database abc;

 quit

 EOF

 [root@1 ~]# ansible group01 -m script -a '/tmp/2.sh'

14、ansible command模块与shell模块的使用

两个模块都是⽤于执⾏linux命令的,这对于命令熟悉的⼯程师来说,⽤起来⾮常high。

shell模块与command模块差不多(command模块不能执⾏⼀些类似 $HOME,>,<,|等符号,但shell可以)

(1)使用command模块创建用户

 [root@1 ~]# ansible group01 -m command -a 'useradd user1'

 [root@1 ~]# ansible group01 -m command -a 'id user1'

 192.168.2.13 | CHANGED | rc=0 >>

 uid=2000(user1) gid=2000(user1) 组=2000(user1)

 192.168.2.12 | CHANGED | rc=0 >>

 uid=2000(user1) gid=2000(user1) 组=2000(user1)

 other | CHANGED | rc=0 >>

 uid=2000(user1) gid=2000(user1) 组=2000(user1)

(2)使用shell模块使用管道符进行统计查询信息

 [root@1 ~]# ansible group01 -m shell -a 'cat /etc/passwd | wc -l'

 192.168.2.13 | CHANGED | rc=0 >>

 22

 192.168.2.12 | CHANGED | rc=0 >>

 22

 other | CHANGED | rc=0 >>

 22

(3)使用shell模块使用$HOME变量

 [root@1 ~]# ansible -m shell  group01 -a "cd $HOME;pwd"

 192.168.2.13 | CHANGED | rc=0 >>

 /root

 other | CHANGED | rc=0 >>

 /root

 192.168.2.12 | CHANGED | rc=0 >>

 /root

(4)使用command模块进行关机

 [root@1 ~]# ansible group01 -m command -a 'shutdown -h 0'

 192.168.2.12 | FAILED | rc=-1 >>

 Failed to connect to the host via ssh: ssh: connect to host 10.0.0.12 port 22: Connection refused

 other | FAILED | rc=-1 >>

 Failed to connect to the host via ssh: ssh_exchange_identification: read: Connection reset by peer

 192.168.2.13 | CHANGED | rc=0 >>

 Shutdown scheduled for 五 2024-08-16 16:49:28 CST, use 'shutdown -c' to cancel.

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值