1 service|systemd(centos7推荐用systemd模块)
name:name.service(类似mysqld,nginx等)
state:started|stoped|reloaded|restarted(动作+ed)
enabled: yes (代表加入开机启动)
daemon_reload:systemd(systemd独有)
示例
- name: start nginx
service:
name: nginx
state: started
2 yum:(安装软件)
name: service_name|"{{item}}"(安装的包名称|多个包循环)
state: present(如果已存在,则不会更新)
loop: (这里是循环才加)
- name1
- name2
示例
- name: install | dependency package
yum:
name: "{{ item }}"
state: present
loop:
- gcc
- make
- openssl
- zlib
3 wait_for:(检测服务健康)
port:service_port(检测的端口)
delay:time(在做下一个动作之前的延时时间,直到端口存在则返回)
示例
- name: check | check nginx status
wait_for:
port: 8080
delay: 3
4 selinux相关
state=disabled(关闭selinux)
示例
-name: selinxu close
selinux:
state=disabled
5 replace(配置文件进行替换调整-会全部进行替换)
path:数据文件路径
regexp:正则表达式匹配
replace:替换值
示例
- name: configure | setup worker_connections 100
replace:
path: "/etc/nginx/nginx.conf"
regexp: '^(\s*worker_connections).+$'
replace: '\1 {{ nginx_max_connections }};
6 lineinfile:(和replace类似,但是不同点在于确保存在这个行2是只会更改匹配的最后一行)
示例
- name: configure | setup worker_processes 4
lineinfile:
path: "/etc/nginx/nginx.conf"
regexp: '^\s*worker_processes\s'
line: "worker_processes {{ nginx_workers }};"
7 unarchive:(远程推送并解压)
remote_src: yes(远程推送)
src: 要推送的软件包
dest: 解压到目标位置,需要是一个目录
creates:指定一个文件名,当该文件存在时,则解压指令不执行,目的是防止覆盖
owner: 默认数组
group:
示例
- name: Unarchive Python bin files.
unarchive:
remote_src: yes
src: "{{ python_file_path }}"
dest: /usr/local
creates:{{ mysql_basedir }}/bin/mysql
8 file:(文件和文件夹模块)
path: file|dir
recurse: true(如果是目录需要递归进行)
state: touch/directory/absent(删除)
示例
- name: "file模块在目标服务器上创建文件"
file:
path: /opt/filemodule/filemodulesetup.txt
state: touch
owner: root
group: root
mode: 755
- name: "file模块在目标服务器上创建要删除的文件"
file:
path: /opt/filemodule/filemoduledelete.txt
state: touch
owner: root
group: root
mode: 755
- name: "file模块在目标服务器上删除文件"
file:
path: "/opt/filemodule/filemoduledelete.txt"
state: absent
11 shell:(执行命令)
shell: 具体命令多个记得用;间隔开来
12 template模块-配置文件模板推送
src:template_file_name
dest:目录文件(绝对路径)
- name: copy configfile to nodes
template:
src: test.j2
dest: /tmp/test.conf
13 debug模块-打印输出
var 将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出
msg 调试输出的消息
- name: Display all variables/facts known for a host
debug:
var: hostvars[inventory_hostname]
verbosity: 4
- debug:
msg: "System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}"
when: ansible_default_ipv4.gateway is defined