ansible常用模块, 自定义角色
2023-03-20 15:00:05 时间
软件安装与管理
配置yum仓库
---
# set yum repository
- hosts: centos
tasks:
- name: set yum repository from group centos with centos8
loop:
- BaseOS
- AppStream
yum_repository:
name: "{{ item }}"
baseurl: https://mirrors.aliyun.com/centos/8/{{ item }}/x86_64/os/
description: "{{ item }}"
file: "{{ item }}"
enabled: yes
gpgcheck: no
state: present
~
yum安装软件
---
- hosts: centos
tasks:
- name: install sensors
loop:
- lm_sensors
- ipmitool
yum:
name: "{{ item }}"
state: present
配置apt仓库
ansible暂时没有类似yum_repository的模块, 因此手动创建sh脚本, 传到受管主机上执行
# apt_update_source.sh
#! /bin/bash
mv /etc/apt/sources.list /etc/apt/sources.list.bak
SYS_VERSION=$(lsb_release -c | grep -o "s.*")
cat >> /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $SYS_VERSION-proposed main restricted universe multiverse
EOF
apt update
# apt_repo.yml
- hosts: ubuntu
tasks:
- name: copy update_apt_source.sh
copy:
src: "/home/test/playbooks/update_apt_source.sh"
dest: "/home"
owner: root
group: root
mode: 0644
- name: bash update_apt_source.sh
shell: "bash /home/update_apt_source.sh && rm /home/update_apt_source.sh"
apt安装软件
---
- hosts: ubuntu
tasks:
- name: install sensors & ipmitool
loop:
- lm-sensors
- ipmitool
apt:
name: "{{ item }}"
state: present
文件管理
copy文件/创建新文件并写入
---
- hosts: ubuntu
tasks:
- name: copy file
loop:
- "/home/test/playbooks/readme.md"
- "/home/test/playbooks/apt_install.yml"
copy:
src: "{{ item }}"
dest: /home/ansible_try
force: no
mode: 0644
- name: touch file and write sth
copy:
dest: "/home/ansible_try/test_file"
content: " here is content "
force: yes
创建/删除 文件/目录/链接
---
- hosts: centos
tasks:
- name: create /home/ansible/file1
file:
path: /home/ansible_try/file1
state: touch
owner: root
group: root
mode: 0777
- name: ln /etc/passwd /home/ansible_try
file:
src: /etc/passwd
path: /home/ansible_try/passwd
state: link
- name: create directory
file:
path: /home/ansible_try/test_dict/test_dict_dict
state: directory
recurse: yes #递归
cron定时任务
ansible centos -m cron -a 'name=“crontab test” weekday=5 hour=14 minute=30 job="/usr/bin/tar -czf /opt/var.tar.gz /var"'
wget
---
- hosts: centos
tasks:
- name: get url try
get_url:
url: http://www.baidu.com # 需要注明协议的名称
dest: /home/ansible_try/baidu.html
服务管理
---
- hosts: centos
tasks:
- name: service try
service:
name: crond
state: reloaded
用户/组管理
user
name:指定用户名
uid:指定uid -u
group:只能指定组名,不能指定gid -g
shell:指定登录的方式 -s
create_home:是否创建家目录
true,yes:创建
false,no:不创建
comment:指定注释 -c
groups:指定附加组(配合append,如果不加append覆盖) -G
append:创建附加组的时候,追加 -a
remove:删除用户的时候,是否同时删除家目录和邮件文件
true,yes:删除
fasle,no:不删除
state
present:创建
absent:删除
generate_ssh_key:是否创建秘钥对
yes:创建
no:不创建
ssh_key_bits:指定秘钥对加密长度
ssh_key_file:指定私钥文件的位置
system:是否是系统用户 -r
yes:是系统用户
no:不是系统用户
group
name:指定组名
gid:指定组id
state:
present:创建
absent:删除
磁盘挂载
mount
path:挂载到本地的目录
src:对端目录
fstype:文件系统类型
nfs
ext4
ext3
state:
present:只写入开机自动挂载的文件中,不挂载
mounted:既写入文件,又挂载
absent:卸载设备,并且清理开机自动挂载文件
unmounted:只卸载不清理文件
推荐:
- 挂载的时候:mounted
- 卸载的时候:absent
利用ansible角色配置不同环境下的apt/yum源
首先修改ansible.cfg配置文件, 接上自定义目录
roles_path = /etc/ansible/roles:/home/test/roles
创建角色
mkdir -p /home/test/roles
ck /home/test/roles
ansible-galaxy init repo_prepare
vim repo_prepare/tasks/main.yml
编辑main.yml
- name: yum resouce
loop:
- BaseOS
- AppStream
yum_repository:
name: "{{ item }}"
baseurl: https://mirrors.aliyun.com/centos/8/{{ item }}/x86_64/os/
description: "{{ item }}"
file: "{{ item }}"
enabled: yes
gpgcheck: no
state: present
when:
- ansible_distribution_major_version == "8"
- ansible_distribution == "CentOS"
- name: copy update_apt_source.sh
copy:
src: "/home/test/playbooks/update_apt_source.sh"
dest: "/home"
owner: root
group: root
mode: 0644
when:
- ansible_distribution == "Ubuntu"
- name: use update_apt_source.sh
shell: "bash /home/update_apt_source.sh && rm /home/update_apt_source.sh"
when:
- ansible_distribution == "Ubuntu"
使用角色
---
- hosts: all
roles:
- repo_prepare
相关文章
- Spark for python developers —Spark与数据的机器学习
- NLP与知识图谱的对接
- 助你所在团队走上大数据路途的六大规则
- Uber开源数据可视化工具deck.gl获重大升级
- Spark程序运行常见错误解决方法以及优化
- 创业公司做数据分析(四)ELK日志系统
- 智能数据湖势在必行
- 监测和管理:现代数据中心的主要考虑因素
- 手机基站定位数据可视分析
- 启动大数据项目之前需要问的5个问题
- 玩不转大数据就别勉强了,或许“小数据”才是真正的终南捷径
- 首届中国国际大数据产业博览会将于5月26日在筑开幕
- 秒拍邓铮:如何做到面对二十亿视频播放请求而不慌
- 关于数据治理,你需要知道些什么?
- 批发行业转型,你需要搞定啥?
- 从底层到应用,那些数据人的必备技能
- 强势解析eBay BASE模式、去哪儿及蘑菇街分布式架构
- Hadoop1.0和Hadoop2.0的区别
- 智能数据湖势在必行
- SAP 助力医疗器械中小企业营业增收30%