Ansible批量删除等

记录下常用的ansible模块

YML文件:

vim delfile.yml

- hosts: test1,test2
  gather_facts: No
  tasks:

  - name: find file
    find:
     paths: /root
     patterns: 'test.*'
    register: files_to_delete

  - name: rm ilo file
    file:
     path: "{{ item.path }}"
     state: absent
    with_items: "{{ files_to_delete.files }}"

HOST文件

vim delhost

[test1]
192.168.1.60:22 ansible_ssh_user=root ansible_ssh_pass='12345678'
192.168.1.61:22 ansible_ssh_user=root ansible_ssh_pass='12345678'

[test2]
192.168.1.62:22 ansible_ssh_user=root ansible_ssh_pass='12345678'

执行

ansible-playbook delfile.yml  -i delhost

常模Ansible模块

shell模块

- name: 将命令结果输出到指定文件
  shell: touch bingbing >> bingbing.txt
- name: complie a script
  shell: |
      if [ 0 -eq 0 ]; then
         echo yes > /tmp/result
      else
         echo no > /tmp/result
      fi
  args:
    executable: /bin/bash

copy 模块

- name: copy file
  copy:
    src: /root/bingbing  	# 拷贝源
    dest: /etc/bingbing.conf	# 拷贝至目录
    owner: root     # 属主
    group: root		# 属组
    mode: u=rw,g=r,o=r
    # mode: u+rw,g-wx,o-rwx
    # mode: '0644'
    backup: yes		# 是否备份

yum模块

- name: 安装最新版apache
  yum:
    name: httpd
    state: latest	#	      present,latest:表示安装   absent:表示卸载
- name: 安装列表中所有包
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: present
- name: 卸载apache包
  yum:
    name: httpd
    state: absent 
- name: 更新所有包
  yum:
    name: '*'
    state: latest

service/systemd 模块


- name: service
  service:
    name: nginx
    state: started
    #state: stopped
    #state: restarted
    #state: reloaded
- name: 设置开机启动
  service:
    name: httpd
    enabled: yes


- name: systenctl 
  systemd: 
	name=tomcat
	state=restarted 
	#state: stopped
    #state: restarted
    #state: reloaded
	enabled=yes 
	daemon_reload=yes

script 模块

- name: script 
  scropt: 
	chdir=/opt	#切换到这个目录执行
	creates=/opt/a.file  /opt/test.sh	# 有a这个文件不执行
	#removes=/opt/a.file /opt/test.sh	# 没有a这个文件不执行

blockinfile 模块

- name: add text
      blockinfile:
        path: /etc/profile # 写入的文件名字
        marker_begin: "start log" # 做开始标记
        marker_end: "end log" #做结束标记
        insertafter: "End of file" # 可以重写覆盖
        state: present
        block: |
          写入文本内容123456abcd
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

king config

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值