---
# 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
~
---
- hosts: centos
tasks:
- name: install sensors
loop:
- lm_sensors
- ipmitool
yum:
name: "{{ item }}"
state: present
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"
---
- hosts: ubuntu
tasks:
- name: install sensors & ipmitool
loop:
- lm-sensors
- ipmitool
apt:
name: "{{ item }}"
state: present
---
- 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 #递归
ansible centos -m cron -a 'name=“crontab test” weekday=5 hour=14 minute=30 job="/usr/bin/tar -czf /opt/var.tar.gz /var"'
---
- 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.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
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。