当前位置:网站首页>Ansible实战系列二 _ Playbook入门
Ansible实战系列二 _ Playbook入门
2022-07-06 09:13:00 【hyh123a】
前言
需求: 操纵多台主机, 为Kubespray做铺垫
环境
Ubuntu 20.04
1、命令
-k(–ask-pass) 用来交互输入ssh密码
-K(-ask-become-pass) 用来交互输入sudo密码
-u 指定用户
# ansible-playbook a.yml --syntax-check #检查yaml文件的语法是否正确
# ansible-playbook a.yml --list-task #检查tasks任务
# ansible-playbook a.yml --list-hosts #检查生效的主机
# ansible-playbook a.yml --start-at-task='Copy Nginx.conf' #指定从某个task开始运行
2、例子
demo 1: 入门小例子
- 编辑
sudo nano demo1.yaml
# 修改如下
- hosts: 172.16.106.20 #指定主机
remote_user: ubuntu20 #指定在被管理的主机上执行任务的用户
tasks: #任务列表↓
- name: hello world #任务名关闭防火墙
command: 'echo hello world' #调用command模块 执行关闭防火墙命令
- 执行
ansible-playbook demo1.yml --syntax-check
ansible-playbook demo1.yaml
- 输出如下
PLAY [172.16.106.20] ***************************************************************************************
TASK [Gathering Facts] *************************************************************************************
ok: [172.16.106.20]
TASK [hello world] *****************************************************************************************
changed: [172.16.106.20]
PLAY RECAP *************************************************************************************************
172.16.106.20 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
demo 2: 指定用户组
默认是/etc/ansible/hosts里面的
- 查看
cat /etc/ansible/hosts
# 输出
[web]
172.16.106.20 ansible_ssh_user='ubuntu20'
172.16.106.21 ansible_ssh_user='ubuntu20'
172.16.106.22 ansible_ssh_user='ubuntu20'
- 编辑
sudo nano demo2.yaml
# 修改如下
- hosts: web #指定主机
remote_user: ubuntu20 #指定在被管理的主机上执行任务的用户
tasks: #任务列表↓
- name: hello world #任务名关闭防火墙
command: 'echo hello world' #调用command模块 执行关闭防火墙命令
- 执行
ansible-playbook demo2.yml --syntax-check
ansible-playbook demo2.yaml
- 输出如下
PLAY [web] *********************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [172.16.106.20]
ok: [172.16.106.21]
ok: [172.16.106.22]
TASK [hello world] *************************************************************************************************************************
changed: [172.16.106.20]
changed: [172.16.106.21]
changed: [172.16.106.22]
PLAY RECAP *********************************************************************************************************************************
172.16.106.20 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.106.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.106.22 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
demo 3: 大致文件夹架构
tree
# 输出如下
.
├── demo3.yaml
└── hosts
└── demo3.ini
- 编辑 hosts 文件夹
sudo nano hosts/demo3.ini
# 修改如下:
[web]
172.16.106.20 ansible_ssh_user='ubuntu20'
172.16.106.21 ansible_ssh_user='ubuntu20'
172.16.106.22 ansible_ssh_user='ubuntu20'
- 编辑 yaml 文件
sudo nano demo3.yaml
# 修改如下:
- hosts: web
tasks:
- name: echo hello world
command: 'echo hello world'
- 运行
ansible-playbook demo3.yaml --syntax-check
ansible-playbook -i hosts/demo3.ini demo3.yaml
demo 4: 添加 roles 文件夹
# 待定 自己写
其他
如有遗漏欢迎补充
如有问题欢迎留言
如有作用欢迎点赞
边栏推荐
- [C language foundation] 04 judgment and circulation
- 在jupyter NoteBook使用Pytorch进行MNIST实现
- [BMZCTF-pwn] 11-pwn111111
- Mysql32 lock
- Global and Chinese markets for aprotic solvents 2022-2028: Research Report on technology, participants, trends, market size and share
- Bytetrack: multi object tracking by associating every detection box paper reading notes ()
- Development of C language standard
- Idea import / export settings file
- API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
- API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
猜你喜欢

A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon

【博主推荐】SSM框架的后台管理系统(附源码)
![[Li Kou 387] the first unique character in the string](/img/2d/f2c99549cac86c08efbfbd8ba76427.jpg)
[Li Kou 387] the first unique character in the string

MySQL26-性能分析工具的使用

MySQL35-主从复制

CSDN Q & a tag skill tree (V) -- cloud native skill tree

MySQL36-数据库备份与恢复

CSDN question and answer tag skill tree (I) -- Construction of basic framework

【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)

Emotional classification of 1.6 million comments on LSTM based on pytoch
随机推荐
CSDN question and answer module Title Recommendation task (II) -- effect optimization
Anaconda3 installation CV2
Mysql21 - gestion des utilisateurs et des droits
How to find the number of daffodils with simple and rough methods in C language
解决:log4j:WARN Please initialize the log4j system properly.
SSM整合笔记通俗易懂版
Have you mastered the correct posture of golden three silver four job hopping?
MySQL19-Linux下MySQL的安装与使用
Emotional classification of 1.6 million comments on LSTM based on pytoch
MySQL22-逻辑架构
MySQL21-用戶與權限管理
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
Pytoch LSTM implementation process (visual version)
Advantages and disadvantages of evaluation methods
Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
Generate PDM file from Navicat export table
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
CSDN question and answer tag skill tree (I) -- Construction of basic framework
Win10: how to modify the priority of dual network cards?
Google login prompt error code 12501