[英]Ansible - replace using variables
我試圖弄清楚為什么我的任務不起作用。 我正在嘗試設置變量來更改文件的內容,該文件保持服務器當前狀態,例如:新建或活動等。我沒有收到錯誤或失敗,但根本沒有更改文件的內容。 任何建議幫助將不勝感激。
---
- hosts: all
become: yes
vars_prompt:
- name: cur_status
prompt: "What is the current status?"
private: no
- name: new_status
prompt: "What is the new status?"
private: no
tasks:
- name: Set server status
replace:
path: /server/status.txt
regexp: >-
{{cur_status}}
replace: >-
{{new_status}}
ignore_errors: yes
$ ansible-playbook -i servers2update server_status.yaml -k -K
SSH password:
BECOME password[defaults to SSH password]:
What is the current status?: New
What is the new status?: Active
PLAY [all] *********************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [serverA]
TASK [Set server status] *******************************************************************************************************************
ok: [serverA]
PLAY RECAP *********************************************************************************************************************************
ServerA : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
cat /server/status.txt
New
您需要刪除>-
(折疊運算符)才能使正則regexp
工作。 例如:
- name: Set server status
replace:
path: /server/status.txt
regexp: "{{cur_status}}"
replace: "{{new_status}}"
ignore_errors: yes
折疊運算符在這里沒有真正意義,因為您的狀態是單行記錄。
這是replace->regexp
的文檔(第 3 行):
regexp
The regular expression to look for in the contents of the file.
Uses Python regular expressions; see https://docs.python.org/3/library/re.html.
Uses MULTILINE mode, which means `^' and `$' match the beginning and end of the file, as well as the beginning and end respectively of `each line'
of the file.
Does not use DOTALL, which means the `.' special character matches any character `except newlines'. A common mistake is to assume that a negated
character set like `[^#]' will also not match newlines.
In order to exclude newlines, they must be added to the set like `[^#\n]'.
Note that, as of Ansible 2.0, short form tasks should have any escape sequences backslash-escaped in order to prevent them being parsed as string
literal escapes. See the examples.
type: str
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.