<p>The task which fails is this one:</p>

<pre><code> - name: replace hostname in config
   replace:
     path: /opt/agentd.conf
     regexp: #\s+Hostname\=
     replace: Hostname={{hname}}
     backup: yes
</code></pre>

<p>Try like this:</p>

<pre><code> - name: replace hostname in config
   replace:
     path: /opt/agentd.conf
     regexp: '#\s+Hostname\=.*$'
     replace: Hostname={{hname}}
     backup: yes
</code></pre>

<p>Note that if you don't add <code>.*$</code>, the task will only replace <code># Hostname=</code> and you will end up with a <code>Hostname=oldhostnamenewhostname</code>.</p>

CC BY-SA 4.0
2019-08-27T13:06:27.453Z
2019-08-27T13:06:27.453Z
13dimitar
<p>I have this code in my playbook:  </p>

<pre><code>- hosts: standby
  remote_user: root
  tasks:
  - name: replace hostname in config
    replace:
      path: /opt/agentd.conf
      regexp: #\s+Hostname\=
      replace: Hostname={{hname}}
      backup: yes
  - name: add database array in files
    lineinfile:
      path: /opt/zabbix_agent/share/scripts/{{ item }}
      line: 'DBNAME_ARRAY=( {{dbname}} )'
      insertafter: DB2PATH=/home/db2inst1/sqllib/bin/db2
      backup: yes
  with_items:
   - Connections
   - HadrAndLog
   - Memory
   - Regular
 - name: restart service
   shell: /etc/init.d/agent restart
   register: command_output
   become: yes
   become_user: root
   tags: restart
 - debug: msg="{{command_output.stdout_lines}}"
 tags: set_config_st
</code></pre>

<p>it will replace <code># Hostname=</code> in a config file with <code>Hostname= givenhostname</code> and add an array in 4 scripts. array is the name of given database. then it will restart the agent to apply the changes.<br>
when i run this command:  </p>

<pre><code>ansible-playbook -i /Ansible/inventory/hostfile /Ansible/provision/nconf.yml --tags set_config_st --extra-vars "hname=fazi dbname=fazidb"
</code></pre>

<p>i get this error:  </p>

<p><code>first argument must be string or compiled pattern</code><br>
<a href="https://i.sstatic.net/FkBLe.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FkBLe.png" alt="this is the complete messege"></a></p>

<p>i searched a bit but couldn't find the reason. what should i do?</p>

<ansible>
ansible error 'first argument must be string or compiled pattern'
BlackCrystal
# ansible error 'first argument must be string or compiled pattern'

(Asked by: username_0 on 2019-08-27)

I have this code in my playbook:  
- hosts: standby
  remote_user: root
  tasks:
  - name: replace hostname in config
    replace:
      path: /opt/agentd.conf
      regexp: #\s+Hostname\=
      replace: Hostname={{hname}}
      backup: yes
  - name: add database array in files
    lineinfile:
      path: /opt/zabbix_agent/share/scripts/{{ item }}
      line: 'DBNAME_ARRAY=( {{dbname}} )'
      insertafter: DB2PATH=/home/db2inst1/sqllib/bin/db2
      backup: yes
  with_items:
   - Connections
   - HadrAndLog
   - Memory
   - Regular
 - name: restart service
   shell: /etc/init.d/agent restart
   register: command_output
   become: yes
   become_user: root
   tags: restart
 - debug: msg="{{command_output.stdout_lines}}"
 tags: set_config_st

it will replace # Hostname= in a config file with Hostname= givenhostname and add an array in 4 scripts. array is the name of given database. then it will restart the agent to apply the changes.
when i run this command:  
ansible-playbook -i /Ansible/inventory/hostfile /Ansible/provision/nconf.yml --tags set_config_st --extra-vars "hname=fazi dbname=fazidb"

i get this error:  
first argument must be string or compiled pattern

i searched a bit but couldn't find the reason. what should i do?

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2019-08-27. Score: 1 | Accepted answer

The task which fails is this one:
 - name: replace hostname in config
   replace:
     path: /opt/agentd.conf
     regexp: #\s+Hostname\=
     replace: Hostname={{hname}}
     backup: yes

Try like this:
 - name: replace hostname in config
   replace:
     path: /opt/agentd.conf
     regexp: '#\s+Hostname\=.*$'
     replace: Hostname={{hname}}
     backup: yes

Note that if you don't add .*$, the task will only replace # Hostname= and you will end up with a Hostname=oldhostnamenewhostname.


---

