我的剧本中有以下代码:
- 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
它将用# Hostname=
替换配置文件中的Hostname= givenhostname
,并在4个脚本中添加一个数组。 array是给定数据库的名称。然后它将重新启动代理以应用更改。
当我运行此命令时:
ansible-playbook -i /Ansible/inventory/hostfile /Ansible/provision/nconf.yml --tags set_config_st --extra-vars "hname=fazi dbname=fazidb"
我收到此错误:
first argument must be string or compiled pattern
我搜索了一下,但找不到原因。我该怎么办?
答案 0 :(得分:1)
问题出在这一行:
regexp: #\s+Hostname\=
您必须引用正则表达式,因为YAML注释以#
开头,因此#
之后的所有内容都会被ansible忽略,这就是为什么出现错误消息的原因。
因此正确的行应为:
regexp: '#\s+Hostname\='
或
regexp: "#\s+Hostname\="
答案 1 :(得分:0)
我认为问题在于缩进。请尝试如下操作。
- 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