ansible

本文详细介绍了Ansible的工作原理、配置步骤以及各种模块的使用,包括ad-hoc命令、Playbook、YAML语法、变量、任务处理和Role的创建与应用。通过实例展示了如何配置免密登录、管理服务、使用firewalld模块、模板模块以及自动化部署Web集群。
摘要由CSDN通过智能技术生成

ansible

原理

控制端主机自带很多模块(模块就是脚本);

ansible通过ssh远程被管理主机,将控制端的模块(脚本)或命令传输到被管理主机;

在被管理端主机执行模块(脚本)或命令,执行不同的模块或命令可以实现不同的功能;

最后ansible退出ssh远程。

绝大多数模块(脚本)都需要参数才能执行成功!!!类似于shell脚本的位置变量!

img

实验拓扑:

img

配置

Control控制节点

[root@control ~]# vim  /etc/hosts        #修改文件,手动添加如下内容(不要删除文件原来的内容)
192.168.4.253    control    
192.168.4.11    test    
192.168.4.12    node2    
192.168.4.13    node3    
192.168.4.14    node4    
192.168.4.15    node5

验证

ping test

配置ssh密钥实现免密码登陆

Ansible是基于SSH远程的原理实现远程控制,如果控制端主机无法免密登录被管理端主机,后续的所有试验都会失败!!

[root@control ~]#  ssh-keygen        #生成ssh密钥
[root@control ~]#  for i in test node2 node3 node4 node5
do
ssh-copy-id   $i 
done
#拷贝密钥到远程主机
#提示:拷贝密钥到远程主机时需要输入对方电脑的账户密码才可以!!
#拷贝密钥到test就需要输入test对应账户的密码,拷贝密钥到node2就需要输入node2对应的密码

部署Ansible软件

image-20201218112926287

[root@control ~]# tar -xf   ansible_soft.tar.gz
[root@control ~]# cd ansible_soft
[root@control ansible_soft]# dnf  -y  install   *

修改主配置文件。

[root@control ~]# mkdir  ~/ansible
[root@control ~]# vim  ~/ansible/ansible.cfg
[defaults]
inventory = ~/ansible/inventory            
#主机清单配置文件(inventory可以是任意文件名),英语词汇:inventory(清单、财产清单)
#forks = 5          #ssh并发数量
#ask_pass = True        #使用密钥还是密码远程,True代表使用密码
#host_key_checking = False      #是否校验密钥(第一次ssh时是否提示yes/no)

修改主机清单文件(清单文件名必须与主配置文件inventory定义的一致)。

[root@control ~]# vim  ~/ansible/inventory
[test]        #定义主机组(组名称任意)
test         #定义组中的具体主机,组中包括一台主机test
[proxy]         #定义主机组(组名称任意),英语词汇:proxy(代理人,委托人)
node2          #proxy组中包括一台主机node2
[webserver]
node[3:4]        #这里的node[3:4]等同于node3和node4
[database]
node5
[cluster:children]      #嵌套组(children为关键字),不需要也可以不创建嵌套组
webserver            #嵌套组可以在组中包含其他组
database

ad-hoc

Ansible ad-hoc是一种通过命令行批量管理的方式,命令基本格式如下:

格式:ansible 主机集合 -m 模块名 -a “参数”

模块使用

查看主机列表

[root@control ~]# cd  ~/ansible          #非常重要
[root@control ansible]# ansible  all  --list-hosts           #查看所有主机列表
# --list-hosts是ansible这个命令的固定选项,如同ls -a一样(-a是ls命令的固定选项)
#英语词汇:list(列表,清单)、host(主机、主办、主人)

测试远程主机是否能ping通。

当需要远程多个主机或者多个组时,中间使用逗号分隔!!!

[root@control ansible]# ansible  test,webserver  -m  ping              #调用ping模块
[root@control ansible]# ansible  all  -m  ping

模块就是脚本(多数为Python脚本),多数脚本都支持参数,默认模块为command。

可以不添加“-m command”默认使用

[root@control ansible]# ansible  test  -m  command  -a   "uptime"     #查看CPU负载
[root@control ansible]# ansible  test  -m command -a  "uname -r"      #查看内核版本
[root@control ansible]# ansible  test   -a   "ip a s"                  #查看网卡信息
[root@control ansible]# ansible  all   -a   "date"                      #查看时间

ansible-doc获取帮助

[root@control ansible]# ansible-doc  -l      #列出所有模块
[root@control ansible]# ansible-doc -l | grep yum      #在所有模块中过滤关键词
[root@control ansible]# ansible-doc yum        #查看模块帮助

Shell模块

command和shell模块的区别,command不支持bash的特性(bash有哪些特性可以参考Shell课程第一天的PPT),如管道和重定向等功能,但是shell模块可以支持。

不可以使用shell模块执行交互命令,如vim、top等。

[root@control ansible]# ansible test -m command -a "ps | wc -l"      #报错
[root@control ansible]# ansible test -m command -a  "ls &"        #报错
[root@control ansible]# ansible test -m shell -a  "ps aux | wc -l"       #进程数量
[root@control ansible]# ansible test -m shell -a  "who"                   #登陆信息
[root@control ansible]# ansible test -m shell -a  "touch /tmp/txt.txt"  
#使用shell模块创建文件会有Warning警告提示,正常!!!

script模块

script模块会把-a后面的脚本拷贝到被管理端主机,然后执行这个脚本。

[root@control ansible]# vim  ~/ansible/test.sh  
#!/bin/bash
echo hello
[root@control ansible]# ansible  test  -m script  -a  "./test.sh"    
#test是主机组的名称,-m调用script模块,-a后面的./test.sh是上面创建脚本的相对路径和文件名
#./是当前目录的意思,在当前目录下有个脚本叫test.sh

输出:
test | CHANGED => {
   
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to test closed.\r\n",
    "stderr_lines": [
        "Shared connection to test closed."
    ],
    "stdout": "hello\r\n",
    "stdout_lines": [
        "hello"
    ]
}

image-20201218143150018

file模块

很多ansible模块都具有幂等性的特征。

幂等性:任意次执行所产生的影响均与一次执行的影响相同

file模块可以创建文件、目录、链接;修改权限与属性等(ansible-doc file)

#state=touch是创建文件,state=directory是创建目录

# ansible  test  -m  file  -a  "path=/tmp/file.txt state=touch"   #远程test组中所有主机,新建文件,path后面指定要创建的文件或目录的名称
#state=touch是创建文件,state=directory是创建目录
输出结果
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/root/testfile.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
## 验证: 到node1主机,使用ls /tmp/file.txt看看文件是否被创建成功   ##
[root@control ansible]# ansible node1 -a "ls /root/"
node1 | CHANGED | rc=0 >>
anaconda-ks.cfg
testfile.txt
EXAMPLES:

- name: Change file ownership, group and permissions
  file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值