ansible 命令
yum 安装 ansible 命令
shell
# 安装依赖epel-release
yum -y install epel-release
#安装ansible
yum -y install ansible
# 显示 配置的主机
ansible all --list
ansible all -m ping
# 重启
ansible all -m reboot
ansible-doc -s command
ansible all -m command -a "date"
ansible all -m command -a "uname -a"
ansible all -m command -a "chdir=/opt touch test.server"
ansible all -m shell -a "echo $HOSTNAME"
ansible all -m shell -a 'echo $HOSTNAME'
ansible all -m script -a 'echo $HOSTNAME'
# 执行 shell 脚本
ansible all -m script -a "chdir=/opt/ /opt/test/start.sh"
-- 复制传送文件
ansible all -m copy -a "src=/opt/test/start.sh dest=/opt/test/start.sh owner=root mode=755 backup=yes"
# 从控制端 复制文件到本地
ansible all -m fetch -a "src=/opt/uuid.txt dest=/opt/test/uuid.txt"、
# 创件文件
ansible all -m file -a "path=/opt/test/init.json state=touch owner=root mode=655 "
# 创建文件夹
ansible all -m file -a "path=/opt/test/init state=directory owner=root "
# 删除文件
ansible all -m file -a "path=/opt/test/init state=absent owner=root "
## playbook 案例
playbook
# 校验
ansible-playbook -C data.yml
# 吧执行命令
ansible-playbook data.yml
yml
shell
- hosts: 192.168.229.130
remote_user: root
tasks:
- name: test
ping:
- name: date
shell: date
离线 安装
yaml
# 在一套 联网的环境上
yum -y install epel-release
mkdir ansible
# 只下载 不安装
yum -y install --downloadonly --downloaddir=./ansible ansible
# 打包成压缩包
tar -zxvf ansible.tar.gz ./ansible
# ssh 免密登录
ssh-keygen -t rsa
cd ~/.ssh
ssh-copy-id 192.168.83.137 #需要免密登录的机器IP
# 复制文件
ansible all -m copy -a "src=/root/ansible.tar.gz dest=/root/ansible.tar.gz owner=root"
各种yaml
shell
- name: self book
hosts: all
remote_user: root
tasks:
- name: copy
copy:
src: "/root/docker-24.0.3.tgz"
dest: "/root/docker-24.0.3.tgz"
owner: root
group: root
- name: fetch
fetch:
src: "/root/test.txt"
dest: "/root/test.txt"
shell
- name: self book
hosts: 192.168.83.137
remote_user: root
tasks:
- name: fetch
fetch:
src: "/root/start.sh"
dest: "/root/start.sh"
mode: 755
owner: root
group: root
flat: yes # yes 不创建远程目录
shell
- name: self book
hosts: 192.168.83.137
remote_user: root
tasks:
- name: file
file:
path: "/opt/init.json"
state: touch # absent 删除 touch 创建
owner: root
mode: 644
shell
- name: self book
hosts: 192.168.83.137
remote_user: root
tasks:
- name: script
script: "/root/start.sh"
shell
- name: self book
hosts: 192.168.83.137
remote_user: root
tasks:
- name: command
command:
chdir: "/opt"
cmd: "echo $(date)"
register: result
- name: show result
debug:
msg: "command result is :{{result.stdout }}"