BlackCrystal
BlackCrystal

Reputation: 129

ansible error 'first argument must be string or compiled pattern'

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
this is the complete messege

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

Upvotes: 0

Views: 1009

Answers (2)

dgw
dgw

Reputation: 13646

The problem is in this line:

regexp: #\s+Hostname\=

You have to quote the regex because YAML comments start with #, so everything after the # will be ignored by ansible and that is why the error message occures. So the correct line should be:

regexp: '#\s+Hostname\='

or

regexp: "#\s+Hostname\="

Upvotes: 1

Smily
Smily

Reputation: 2568

I think the problem is with indention. Please try as below.

- 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

Upvotes: 0

Related Questions