ansible

运维工具箱-ansible

base

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ansible goup1 -m ping                   # 测试连通
ansible goup1 -m setup                  # 获取基础信息
ansible goup1 -m shell -a "hostname"    # 执行一个命令
ansible goup1 -m script -a './a.sh'     # 执行本地脚本

# 判断进程
ansible goup1 -m shell -a 'ps -ef | grep nrpe | grep -v grep'

# 探测网络
ansible goup1 -m shell -a 'nc -v -w 10 -z 10.10.17.35 80'

file

1
2
3
4
5
6
7
8
9
ansible goup1 -m copy -a 'src=xx1.sh dest=~/ mode=744'

ansible goup1 -m copy -a 'src=/dir/file1.tar.gz dest=/weblogic/ owner=weblogic group=weblogic mode=664'

ansible goup1 -m shell -k -a 'unzip -q /opt/server/apache-jmeter-5.1.1.zip -d /opt/server/'
ansible goup1 -m shell -k -a 'tar xf /opt/server/jdk-8u241-linux-x64.tar.gz -C /opt/server/'

# 删除一个文件/目录
ansible goup1 -m file -a "path=/tmp/resolv.conf state=absent"

切换身份

1
2
3
4
5
6
7
# sudo
ansible goup1 -b --become-method=sudo -m shell -a "id"
# su
ansible goup1 -b --become-method=su -m script -a './xxx.sh'
ansible goup1 -b --become-method=su --extra-vars 'ansible_su_pass=password' -m shell -a "id"

ansible goup1 -b --become-method=su --extra-vars 'ansible_su_pass=password' -m file -a "dest=/opt owner=user1 group=user1"

综合

1
2
3
4
5
6
7
8
9
ansible goup1 -m service -a "name=network state=restarted"

ansible goup1 -m shell -a 'chdir=/dir2 ./shell.sh'

# 解决指纹变更
ansible ub -m ping --ssh-common-args='-o StrictHostKeyChecking=no'


--private-key ./ssh_key

play

1
2
3
4
5
6
7
8
9
---
- hosts: goup1
  remote_user: user1

  tasks:

  # 下载远程主机按IP地址命名的文件
  - name: fetch remote file to local
    fetch: src=/tmp/las_{{ inventory_hostname }}.log dest=/tmp/last_log/ flat=yes
Licensed under CC BY-NC-SA 4.0
转载或引用本文时请遵守许可协议,知会作者并注明出处
不得用于商业用途!
最后更新于 2023-02-10 00:00 UTC