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