Ansible的Playbook案例一

(一)要求剧本名/etc/ansible/play/play1.yaml,在被控节点实现初始化操作,例如创建一个demo用户,uid是3450,家目录是/tmp/demo,登录shell是/bin/bash,设置用户的密码为demo,要求这个demo用户可以使用sudo提权,并且控制节点可以实现免密登录到主机的demo用户

- name: play1

hosts: all

tasks:

- name: creat user

user:

name: demo

uid: 3450

home: /tmp/demo

shell: /bin/bash

- name: create passwd

shell: 'echo "demo" | passwd --stdin demo'

- name: sudo

shell: 'echo "demo ALL=(ALL) NOPASSWD:ALL >> /etc/sudoers"'

- name: mkdir /tmp/demo/.ssh

file:

path: /tmp/demo/.ssh

state: directory

owner: demo

group: demo

mode: '0700'

- name: ssh_nopasswd

copy:

src: /root/.ssh/id_rsa.pub

dest: /tmp/demo/.ssh/authorized_keys

(二)要求剧本名/etc/ansible/play/play2.yaml,在所有的被控节点上执行,要求配置本地YUM仓库,然后实现httpd软件的安装,并且最终在控制节点上使用curl命令访问被控节点主机名的时候,访问node1则输出的是node1内容,访问node2则输出的是node2内容

- name: play02

hosts: all

tasks:

- name: mount /dev/sr0

shell: "mount /dev/sr0 /iso"

ignore_errors: yes ##这个ignore_errors是task指令和模块同级 作用:忽略这个play的错误

- name: repository_baseos

yum_repository:

file: yum

name: BaseOS

description: repository_baseos

baseurl:file:///iso/BaseOS

gpgcheck: 0

enabled: 1

- name: repository_appstream

yum_repository:

file: yum

name: AppStream

description: repository_appstream

baseurl:file:///iso/AppStream

gpgcheck: 0

enabled: 1

- name: httpd install

yum:

name: httpd

state: present

- name: systemctl start httpd

systemd:

name: httpd

state: started

- name: systemctl stop firewalld

systemd:

name: firewalld

state: stopped

- name: stop selinx

shell: "setenforce 0"

- name: play02_node1

hosts: node1

tasks:

- name: chages https content

copy:

content: "this is node1\n"

dest: /var/www/html/index.html

- name: play02_node2

hosts: node2

tasks:

- name: chages https content

copy:

content: "this is node2\n"

dest: /var/www/html/index.html

相关推荐
未济2 天前
linux 配置环境变量
linux
傲世仙尊2 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.2 天前
进程间通信
linux
AI职业加油站2 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-052 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏2 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz2 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅2 天前
linux(8) 软硬链接
linux·运维·服务器
Wang's Blog2 天前
Java 项目实战: 外卖平台-文件下载与ServletOutputStream回写浏览器
服务器·项目开发