Home > Mobile >  Ansible - replace using variables
Ansible - replace using variables

Time:08-10

I am trying to figure out why my task does not work. I am trying to set variables to change the content of file, this file keeps the server current status for example : New or Active, etc. I do not get errors or failure, but simply does not change the content of the file. Any suggestions help will be appreciated.

---
- 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

CodePudding user response:

You need to remove >- (fold operator) to make the regexp work. For example:

 - name: Set server status
    replace:
      path: /server/status.txt
      regexp: "{{cur_status}}"
      replace: "{{new_status}}"
    ignore_errors: yes

The fold operator is not really making sense here, as your states are single lines records.
Here is the documentation(3rd line) for replace->regexp:

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
  • Related