2016-11-08 22 views
0

我試圖在服務器上獲取有關證書的信息,以便我可以在證書即將到期時採取措施。我採取的步驟是:如何使Ansible中的信息檢索具有冪等性?

# Does the certificate already exist? 
- name: certbot | does cert already exist 
    stat: 
    path: "/etc/letsencrypt/live/{{certbot_domain}}//cert.pem" 
    register: certbot_certificate 

# Get the expiration date for the certificate in ISO format. 
- name: certbot | get cert expiration date 
    shell: "date --date=\"$(openssl x509 -in /etc/letsencrypt/live/{{certbot_domain}}/cert.pem -noout -enddate | cut -d= -f 2)\" --iso-8601" 
    register: certbot_expiration_date 
    when: certbot_certificate.stat.exists == True 

# Convert the expiration date into the number of seconds from today. 
- name: certbot | calculating expiration in seconds 
    shell: "echo $(($(date --date={{certbot_expiration_date.stdout}} +%s) - $(date +%s)))" 
    register: certbot_expires_in 
    when: certbot_certificate.stat.exists == True 

我的問題是,無論是shell命令總是被標記在Ansible爲「改變」,這意味着這個劇本不能冪等。

我明白爲什麼會出現這種情況,因爲Ansible無法真正理解shell命令做了什麼,所以它通過說「改變」來安全地玩,但我想知道是否有一種冪等的方式達到相同的結果?

缺寫一個擴展名,即... :)

謝謝。

回答

0

答案,因爲它的出現,是一個changed_when語句添加到任務,例如:

- name: certbot | get cert expiration date 
    shell: "date --date=\"$(openssl x509 -in /etc/letsencrypt/live/{{certbot_domain}}/cert.pem -noout -enddate | cut -d= -f 2)\" --iso-8601" 
    register: certbot_expiration_date 
    changed_when: False 
    when: certbot_certificate.stat.exists == True 

因爲我知道,shell命令並沒有改變任何東西,我可以放心地changed_when設置爲false這可以確保最終的計數顯示Changed = 0,從而使劇本具有冪等性。