【Ansible常用命令+模块+Playbook+Roles】

Ansible

  • 一、命令
    • [1.1 常用命令](#1.1 常用命令)
  • 二、模块
    • [2.1 shell模块](#2.1 shell模块)
    • [2.2 复制模块](#2.2 复制模块)
    • [2.3 用户模块](#2.3 用户模块)
    • [2.4 软件包管理](#2.4 软件包管理)
    • [2.5 服务模块](#2.5 服务模块)
    • [2.6 文件模块](#2.6 文件模块)
    • [2.7 收集模块](#2.7 收集模块)
    • [2.8 fetch](#2.8 fetch)
    • [2.9 cron](#2.9 cron)
    • [2.10 group](#2.10 group)
    • [2.11 script](#2.11 script)
    • [2.12 unarchive](#2.12 unarchive)
  • [三、YAML Roles](#三、YAML Roles)

一、命令

1.1 常用命令

shell 复制代码
# 一、ansible
ansible <hosts> -m [module_name] -a [执行命令]
ansible-inventory --graph

# 二、ansbile-doc
#文档查询yum模块用法 /EXAMPLES   空格下一页   q退出
ansbile-doc yum

# 三、ansible-playbook
#剧本语法检测  剧本执行
ansible-playbook 1.yml --syntax-check  
ansible-playbook 1.yml

# 四、ansible-vault
ansible-vault create 1.yml
#此工具可以用于加密解密yml文件。
create              #创建一个新的加密剧本
decrypt             #解密剧本
edit                #输密编辑剧本
view                #查看加密剧本
encrypt             #加密 YAML file
encrypt_string      #给字符串加密
rekey               #改密

# 五、ansible-galaxy
#此工具会连接 https://galaxy.ansible.com 下载相应的roles
ansible-galaxy list
ansbile-galaxy install geerlingguy.nginx

# 六、 ansible-inventory
# 用于生成和管理Ansible的主机清单
ansible-inventory --graph

# 七、ansible-config
# 列出并输出可用配置
ansible-config list

二、模块

2.1 shell模块

shell 复制代码
ansible all -m shell -a 'yum -y install httpd' -o

ansible webserver -m shell -a 'hostname' -o

2.2 复制模块

shell 复制代码
#复制hosts文件  拥有者root  组bin 权限777
ansible all -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'

#复制hosts文件  拥有者root  组bin 权限777  是否备份yes
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'

2.3 用户模块

shell 复制代码
#创建用户henry
ansbile all -m user -a "name=henry state=present"

#生成加密密码
echo 'root123' | openssl passwd -l -stdin

#发送修改密码
ansbile all -m user -a 'name=henry password="$1$g4r8Qoi......."'

#修改shell  用户henry不能登录
ansbile all -m user -a 'name=henry shell=/sbin/nologin append=yes'

#删除用户吗 
ansible all -m user -a 'name=henry state=absent'

2.4 软件包管理

shell 复制代码
#升级所有的包
ansible all -m yum -a "name='*' state=latest"

#安装apache
ansible all -m yum -a 'name="httpd" state=latest'

2.5 服务模块

shell 复制代码
#启动httpd服务  state=started/restarted/stopped   enabled=yes/no
ansbile all -m service -a 'name=httpd state=started'

#开机启动
ansible all -m service -a 'name=httpd state=started enabled=yes'

2.6 文件模块

shell 复制代码
#创建文件
ansbile all -m file -a 'path=/tmp/123.txt mode=777 state=touch'

#创建目录
ansbile all -m file -a 'path=/tmp/wenjian mode=777 state=directory'

#删除文件目录
ansbile all -m file -a 'path=/tmp/wenjian state=absent'

2.7 收集模块

shell 复制代码
#收集host1信息
ansbile host1 -m setup

#收集host1 的ipv4地址
ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'

2.8 fetch

shell 复制代码
#从远程主机web上面获取文件/app/fstab  到本地/app
 ansible web -m fetch -a "src=/app/fstab dest=/app" 

2.9 cron

shell 复制代码
# 创建cron  名称sync time from ntpserver  时间:每十分钟  任务job ntpdate时间同步对172.17.0.1
ansible host1 -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/usr/sbin/ntpdate 172.17.0.1 &> /dev/null" ' 

2.10 group

shell 复制代码
#创建g1的用户组
ansbile all -m group -a 'name=g1 state=present'

2.11 script

shell 复制代码
#在指定节点web执行脚本
 ansible web -m script -a "/root/wan.sh"

2.12 unarchive

shell 复制代码
#将本地1.tar的压缩包解压到host1里面/tmp下面
ansible host1 -m unarchive -a 'src=/root/1.tar dest=/tmp/'

三、YAML Roles

roles则是在ansible中,playbooks的目录组织结构。 将代码或文件进行模块化,成为roles的文件目录组织结构, 易读,代码可重用,层次清晰。

实例:安装nginx 并启动

3.1 目录结构

files:文件 (群演)

handlers:处理程序 (武术指导)

tasks:主程序 (主演)

templates:模版文件(配置参数) (替身)

vars:参数 (道具)

size.yml:剧本 运行主机、剧本任务 (剧本)

3.2 文件内容

tasks/main.yaml

yaml 复制代码
---
- name: install epel-release packge
  yum: name=epel-release state=latest

- name: install nginx packge
  yum: name=nginx  state=latest

- name: copy index.html
  copy: src=index.html dest=/usr/share/nginx/html/index.html

- name: copy nginx.conf template
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx

- name: make sure nginx service running
  service: name=nginx state=started enabled=yes

nginx.conf.j2

shell 复制代码
#修改下面参数
worker_processes  {{ ansible_processor_cores }};
worker_connections {{ worker_connections }};

vars/main.yaml

yaml 复制代码
worker_connections: 10240

handlers/main.yaml

yaml 复制代码
---
- name: restart nginx
  service: name=nginx state=restarted

roles/site.yaml

yaml 复制代码
- hosts: host4
  roles:
  - nginx

3.3 运行

bash 复制代码
ansible-playbook site.yaml
相关推荐
树℡独3 小时前
ns-3仿真之应用层(五)
服务器·网络·tcp/ip·ns3
嵩山小老虎3 小时前
Windows 10/11 安装 WSL2 并配置 VSCode 开发环境(C 语言 / Linux API 适用)
linux·windows·vscode
Fleshy数模4 小时前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
a41324474 小时前
ubuntu 25 安装vllm
linux·服务器·ubuntu·vllm
Configure-Handler4 小时前
buildroot System configuration
java·服务器·数据库
津津有味道4 小时前
易语言TCP服务端接收刷卡数据并向客户端读卡器发送指令
服务器·网络协议·tcp·易语言
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.5 小时前
Keepalived VIP迁移邮件告警配置指南
运维·服务器·笔记
Genie cloud5 小时前
1Panel SSL证书申请完整教程
服务器·网络协议·云计算·ssl
一只自律的鸡5 小时前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
17(无规则自律)6 小时前
【CSAPP 读书笔记】第二章:信息的表示和处理
linux·嵌入式硬件·考研·高考