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

相关推荐
12点一刻8 分钟前
Hermes Agent 与 Superpowers 框架的区别?
运维·服务器
MXsoft61822 分钟前
**混合云统一监控实践:私有云+公有云的一体化运维方案**
运维·网络·数据库
2501_9127840824 分钟前
Taocarts深度解析:1688自动代采模块的Puppeteer自动化实战
运维·自动化
小义_25 分钟前
【Ansible】(三)基础配置与连接设置
云原生·ansible
天南散修27 分钟前
MT7916驱动中802.11转换为802.3
linux·网络·驱动开发·wifi·802.11
MXsoft61828 分钟前
**断网续传与本地缓存:弱网环境下的监控数据保障方案**
运维·缓存·自动化
LT101579744439 分钟前
2026年UI自动化测试平台选型指南:全界面自动化覆盖方案
运维·ui·自动化
TechWayfarer1 小时前
云服务器地域怎么选:用离线IP数据库识别用户来源并优化部署
服务器·数据库·python·tcp/ip·数据分析
CriticalThinking1 小时前
在xshell中使用ssh隧道访问远程服务
linux·网络·ssh
爱装代码的小瓶子1 小时前
安工大题目分类(含解析和翻译)
linux·网络·c