ansible

base

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

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"

切换身份

# 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"

综合

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

---
- 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
最后更新于