ansible-playbook根據shell判斷狀態


- name: 查看進程中是否存在啟動的服務
shell: "cd {{ SERVER_HOME }}/{{ SERVER }} && sh sh.sh status"
register: result
changed_when: false
failed_when: result.stdout.find('not') != -1

 

 

 

正文

部署有一個需求,要將所有的模塊服務器里添加一個叫agentmizar的日志采集模塊。但是有一些服務器提前有部署過,那么判斷一下如果服務器里有此進程就跳過,如果沒有此進程就傳包並修改配置文件然后啟動。

與playbook搭配的yaml內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
- hosts: all		
remote_user: root
any_errors_fatal: no
gather_facts: no #不采集對方機器的數據,提高執行速度
serial:
- 5 #5台機器一組
tasks:
- name: judge agent process is exits
shell: ps -aux | grep agent
ignore_errors: True #如果命令執行不成功,即 echo $?不為0,則在其語句后面的ansible語句不會被執行,導致整個程序中止。
register: result

- name: agent is running
shell: echo "agent is running"
when: result.stdout.find('agent.conf') != -1

- name: agent dir is exits
shell: ls /opt/agentmizar
ignore_errors: True
register: dirresult

- name: copy packages
copy:
src: /tmp/agentmizar.zip
dest: /opt
owner: root
when: dirresult is failed #如果文件夾存在就是dirresult is succeeded

- name: unzip agentmizar
unarchive: #如果你並不喜歡用unzip的話,那么可以shell:unzip -o 對應.zip的方式來達到不用輸入y的效果,但是更推薦用unarchive
#extra_opts: -j #將zip里的所有遞歸文件都放到本目錄
src: /opt/agentmizar.zip
dest: /opt
remote_src: yes
when: result.stdout.find('agent.conf') == -1

- name: backup old and unpack new package
shell: cp -rf /opt/build/* /opt/ #由於zip包也解壓縮出來是一個build文件夾,需要再扒一層
when: result.stdout.find('agent.conf') == -1

- name: update agent.conf
lineinfile:
dest: /opt/agentmizar/agent.conf
regexp: "kafka = 192.168.0.1:9092,192.168.0.2:9092,192.168.0.3:9092" #修改配置文件
line: "kafka = 172.0.10.1:9092"
when: result.stdout.find('agent.conf') == -1

- name: start agentmizar
shell: cd /opt/agentmizar/ && /bin/bash /opt/agentmizar/start_agent.sh
when: result.stdout.find('agent.conf') == -1

 

這個yaml,我執行ps -aux | grep agent,並將結果存儲到result這個register里。然后從register里去find關鍵字agent.conf,如果不存在就返回-1,那么可以判斷當前機器里沒有agentmizar進程。

如果說進程是一個守護進程,那么在判斷進程(比如是systemctl status apache2)是否存在可以這么寫:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- name: Check if Apache is running
command: systemctl status apache2
ignore_errors: yes
changed_when: false
register: service_apache_status

- name: Report status of Apache
fail:
msg: |
Service apache2 is not running.
Output of `systemctl status apache2`:
{{ service_apache_status.stdout }}
{{ service_apache_status.stderr }}
when: service_apache_status | failed

 

注意!如果when條件判斷句中有變量的話要將用()來括變量,如下:

1
2
when: ansible_default_ipv4.address == {{ ETCD_NODE03 }}		#錯誤寫法
when: ansible_default_ipv4.address == (ETCD_NODE03) #正確寫法

 

再注意!register變量的命名不能用-(中橫線),比如dev-sda6_result,則會被解析成sda6_result,dev會被丟掉!

yum一次性安裝多個模塊的問題

新版本的ansible-playbook已經不支持在yum安裝多個模塊里使用的方式了,也就是說

1
2
3
4
5
6
7
tasks
- name: 安裝最新版本的命令
yum: name={{ item }} state=latest
with_items:
- unzip
- psmisc
- java-1.8.0-openjdk*

 

這么寫在老版本還OK,但是在2.8以后,還這么寫就會有錯誤:[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying name: "", please use name: ['unzip', 'psmisc', 'java-1.8.0-openjdk*'] and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.要改成如下:

1
2
3
4
5
6
7
8
tasks:           
- name: 安裝最新版本的命令
yum:
state: latest
name:
- unzip
- psmisc
- java-1.8.0-openjdk*

 

如何在task之間傳遞變量

某個變量想從一個task給另一個,可以按照如下的方式寫:

1
2
3
4
5
6
7
8
9
10
---
- hosts: all
gather_facts: no
tasks:
- name: register vars
shell: hostname
register: info

- name: display vars
debug: msg="{{info.stdout}}"

 

第一個shell執行完后,使用register獲取數據到info里,info是一個key value字典,debug輸出info.stdout的具體內容。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM