记录下常用的ansible模块
YML文件:
vim
- 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: "{{ }}"
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 -i delhost
常模Ansible模块
shell模块
- name: 将命令结果输出到指定文件
shell: touch bingbing >>
- 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/ # 拷贝至目录
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/ /opt/ # 有a这个文件不执行
#removes=/opt/ /opt/ # 没有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