ansible 模板使用語法:
1. 使服務器執行一個shell 命令
- name: execute a shell command (ansible 任務名稱,可寫可不寫,寫了直觀,同時可以直接根據任務名稱定位到 任務行)
shell : hostname (表示 在服務器上執行 hostname 這條命令)
或
- shell: hostname
2. copy 複製文件,從服務器本地到本地 或 從ansible server 服務器 到 被操作的服務器本地。
- name: Get EPEL 任務名稱
copy:
src: epel-release-6-8.noarch.rpm (cp 的源文件的位置,不加絕對路徑,代表在ansible 服務器,位置位於 執行的yml 文件所在的roles 下的 file 目錄下 )
dest: /tmp/epel-release-6-8.noarch.rpm
when: ansible_distribution_major_version == '6' (判斷條件,但服務器版本是6 才執行 cp 這條任務,即名稱是 Get EPEL 這個的這條任務 )
3.yum ,用yum 進行包操作,如 install,update ,remove
- name: disable selinux ()
yum:
name: libselinux-python (包名 也可跟 一個rpm 絕對路徑,代表安裝本地下載下來的rpm 包,而非從yum 源來安裝)
state: present
(state參數:用於指定軟件包的狀態 ,默認值爲。present,表示確保軟件包已經安裝,除了 present,其他可用值有 installed、latest、absent、removed,其中 installed 與present 等效,latest 表示安裝 yum 中最新的版本,absent 和 removed 等效,表示刪除對應的軟件包。)
4. item ,當執行的參數有多個時 使用,舉例。
- name: Install common package
yum:
name: "{{ item }}"
state: latest
with_items:
- ntpdate
- ntp
5.get_url 下載url 到指定目錄
- name: Get url
get_url:
url: url
dest: /tmp/ (直接跟目錄,下載下的文件名字不重新命令,也可加新的文件名字,代表重命名。如 /tmp/newname )
url_username:
url_password:
owner: root
group: root
mode: 0755
6 service ,對 服務進行操作,如重啓某服務,
- name: config named-chroot
service:
name: named
state: stopped
enabled: no
7 lineinfile: 類似於sed ,向文件末尾插入文本或 替換文本,
- name: modify resolv.conf
lineinfile:
regexp: "nameserver" (正則匹配)
line: "nameserver 114.114.114.114" (替換後的內容)
dest: /etc/resolv.conf
backrefs: yes (匹配失敗,不插入,不替換)
8 file ,新建文件,目錄,做軟鏈等等
ansible all -m file -a "path=/IT state=directory owner=it" 創建IT目錄,並制定屬主是it ansible all -m file -a "path=/tmp/IT.txt state=touch mode=777" 創建文件IT.txt 並指定權限 ansible all -m file -a "path=/tmp/cron src=/var/log/cron state=link" 創建軟連接,連接是自己本機的文件前面的path是連接存放地址,後面的src是源文件地址 ansible all -m file -a "path=/tmp/cron state=absent" 刪除軟連接 ansible all -m file -a "path=/IT state=absent"
9 var and ansible 的setup
- name: Modify xinetd nrpe config
lineinfile:
dest: /etc/xinetd.d/nrpe
regexp: "only_from"
line: " only_from = 127.0.0.1 {{ net_mask_bond1 | ipaddr('network') }} {{ net_mask_bond0 | ipaddr('network') }}"
backrefs: yes
vars:
net_mask_bond1: "{{ ansible_bond1.ipv4.network }}/{{ ansible_bond1.ipv4.netmask }}" (服務器的屬性參數)
net_mask_bond0: "{{ ansible_bond0.ipv4.network }}/{{ ansible_bond0.ipv4.netmask }}"
tags: fetch
ipaddr('network') 網段 ipaddr('ipaddress') IP ipaddr('prefix') 數字表示的掩碼 ipaddr('netmask') IP段表示的掩碼
10 debug 調試用信息
debug:
msg: "bond1: {{ net_mask_bond1 |ipaddr('network') }} ; bond2:{{ net_mask_bond1 | ipaddr('address') }} "
vars:
net_mask_bond1: 192.168.1.2/24
11 setup 收集遠程主機的一些系統參數信息 ,用法:
ansible ansible-demo3 -m setup
ansible ansible-demo3 -m setup -a "filter=ansible_bond0.ipv4.addresses"
12 ignore_errors : 是否忽略本條任務執行錯誤
如果某一條任務 ,允許執行錯誤的情況下也繼續往下執行,加這個參數 yes or no 如
- name: Stop ssh service
service:
name: sshd
state: stopped
enabled: no
ignore_errors: yes
即使這條命令 執行錯誤 如 sshd 服務 停止失敗,也繼續執行其他任務,如果不加ignore ,ansible 會在任務執行過程中某一條任務執行報錯, 退出。
13 import
- import_role:
name: common: 先執行roles 下爲common 的任務。
- import_tasks: update_kernel.yml 執行名字爲 update_kernel.yml 的任務先
14 url POST 請求
- name: Update rms virtualserver ip
uri:
url: "https://www.test.com/index.html?access_id=hpcc×tamp={{ ansible_date_time.epoch }}&token={{ rms_string | hash('md5') }}"
method: POST
headers:
Content-Type: "application/json"
body: '{"operation_type": "update","device_name": {{VIP}} }'
body_format: json
validate_certs: no
vars:
rms_string: hpcc338a5b1880c42e3161d32b34008f71a3{{ ansible_date_time.epoch }}
VIP: "{{ groups['Vip'][0] }}"
when: NodeType == 'test1'
15 fetch 將遠程服務器的一些信息拉取到ansible 服務器所在的本地。
ansible all -m fetch -a "src=/root/test.sh dest=/root/test"
16 template : 可以將帶有參數的配置文件(本地)傳遞到目標地址
參考 :https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-gm1w27pd.html
17 register :
參考:https://blog.csdn.net/qianggezhishen/article/details/53939188