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

相关推荐
牛马工作号3 小时前
Mosquitto + ACME:从 Docker Compose 部署到 TLS 证书自动续期与加载
运维·docker·容器
强哥聊算力6 小时前
《算力江湖·英伟达传奇》第七集:ChatGPT出现前夜,英伟达发生了什么?
服务器·人工智能·算法·ai·chatgpt·gpu算力
Initialize-le6 小时前
Nginx 504 超时、499 一堆?网关排查法
运维·nginx
砚凝霜8 小时前
【软考信息安全】第二十二章 Web服务器安全分析与增强——Apache与IIS
服务器·前端·安全
Android系统攻城狮8 小时前
Linux Gstreamer深度解析之gst_audio_ring_buffer_delay调用流程与实战(七十二)
linux·运维·服务器·gstreamer音视频·音视频进阶
快乐的韭菜根根9 小时前
ubuntu 安装slurm并配置
linux·运维·ubuntu
数字新视界9 小时前
机房动环监控提升运维效率
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
U-Mail邮件系统10 小时前
自建邮箱服务器有哪些软件?三款主流邮件服务器软件推荐
运维·服务器
倒头就睡的小比特11 小时前
Linux基础指令3
linux·服务器
Smoothcloud润云11 小时前
PyTorch推理服务模型热切换与失败回滚实战:降低版本更新中断
服务器·人工智能·pytorch·python·深度学习·gpu