I have a task in an Ansible playbook that is supposed to iterate with a list of users and do a lineinfile to enable access to a postgress database:
- name: Postgres localhost access
lineinfile:
dest: "{{postgres_dest}}/data/pg_hba.conf"
line: "host all {{item.user}} 127.0.0.1/32 trust"
regexp: "^host[\s\t]*all[\s\t]*{{item.user}}[\s\t]*127.0.0.1/32[\s\t]*trust"
insertbefore: EOF
with_items: "{{postgres_users}}"
notify: postgres reload
tags:
- postgres
- postgres_hba
the problem I'm getting is that ansible thinks that the {{item.user}} is not being escaped with "", which actually is not true, since this will expand due to the "" of the whole line. The exact error I get:
Syntax Error while loading YAML script, jenkins.yml
Note: The error may actually appear before this position: line 156, column 9
line: "host all {{item.user}} 127.0.0.1/32 trust"
regexp: "^host[\s\t]*all[\s\t]*{{item.user}}[\s\t]*127.0.0.1/32[\s\t]*trust"
^
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
any ideas on how to do this?
first of all, thanks to the guys in IRC on the #ansible channel :)
seems that the issue wasn't with the vars itself but with the un-scaped backslashes
changed the line to:
regexp: "^host[\\s\\t]*all[\\s\\t]*{{item.user}}[\\s\\t]*127.0.0.1/32[\\s\\t]*trust"
and now it works great
Related
So ive been tasked with replaceing zabbix server. To do so i have to modify zabbix_agent file in all server and there are many. Tho in this job is the first time i see ansible so i need some help. And i am using ansible-playbook.
In zabbix_agentd.conf file there is the old zabbix conf:
HostMetadata=Linux
PidFile=/var/run/zabbix/zabbx_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=zabbix.company.com
ServerActive=zabbix.company.com
HostnameItem=system.hostname
Include=/etc/zabbix_agentd.d/
Now i need to replace "Server" and "ServerActive" to "zabbix2.company.com"
I have tried various codes from this page to work for my needs but so far it has failed. No clue what im doing wrong
Try this one
- lineinfile:
path: /etc/zabbix_agentd.conf
regexp: '^\s*{{ key }}\s*=(.*)$'
line: '{{ key }}={{ value }}'
notify: reload zabix
loop:
- {key: 'Server', value: 'zabbix2.company.com'}
- {key: 'ServerActive', value: 'zabbix2.company.com'}
Notes
Path is required; probably /etc/zabbix_agentd.conf ?
It is not necessary to search the white-space \s* in regexp. However, it would match and fix potential spaces in the configuration.
Create and notify a handler reload zabix when anything changed. See Handlers: Running Operations On Change.
Take a look at Zabix modules.
I have manged to solve this issue using this code.
---
tasks:
- name: 'replace line'
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
regexp: '^(.*)Server=zabbix.company.com(.*)$'
line: 'Server=zabbix2.company.com'
backrefs: yes
trying to do playbook:
- hosts: win
gather_facts: no
roles:
- update_win
update_win mail.yml:
- name: Create Auto_deploy_temp folder on remoter host
win_file:
path: {{ disk }}\Auto_deploy_temp
state: directory
and vars in group vars file win.yml:
disk: 'c:'
but getting out:
ERROR! Syntax Error while loading YAML.
did not find expected key
The error appears to be in '/etc/ansible/roles/update_win/tasks/main.yml': line 3, column 19, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
win_file:
path: {{ disk }}\Auto_deploy_temp
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
can u help me with this issue?
P.S.: earlier i've launched some similar code, but there were no vars in the start of path expression, only in the end
Please provide path: "{{ disk }}\Auto_deploy_temp"
Update
Create a new var as path_dir: \Auto_deploy_temp, and use
path: "{{disk}}{{path_dir}}"
or
path: "{{ disk }}\\Auto_deploy_temp"
Escape '\'
win_file:
path: {{ disk }}\\Auto_deploy_temp
My Host file is defined as below.
[groupname]
server1
server2
server3
Each server from the above list have different SSH user to logins.
In my playbook I am setting up the user depending on the hostname condition.
- name: setting user user1
set_fact: ssh_user='user1'
when: "{{ inventory_hostname }}" == 'server1'
- name: setting user user2
set_fact: ssh_user='user2'
when: "{{ inventory_hostname }}" == 'server2'
- name: setting user user3
set_fact: ssh_user='user3'
when: "{{ inventory_hostname }}" == 'server3'
However this is not working as expected, and always throwing a syntax error, but the yaml is valid.
Looking for some suggestion.
Below is the Error thrown when the playbook executes,
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/var/lib/awx/projects/_2573__sample/mysample.yml': line 6, column 36, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
set_fact: ssh_user='user1'
when: "{{ inventory_hostname }}" == 'server1'
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value.
The when clause in a task is not supposed to be a jinja2 expanded var. Use
when: inventory_hostname == 'somehost'
Moreover, regarding my previous comment, there is no use to set fact as you do in your playbook for such a variable. You shoud set this at inventory level so you don't have to bother for whatever playbook you use. You have several options for that. If you want to stick to your ini style inventory, you could do it like this:
[groupname]
server1 ansible_user=user1
server2 ansible_user=user2
server3 ansible_user=user3
You can also make your inventory in yaml, or declare the host variables in separate files (in host_vars/<hostname>.yml). You should read the doc about inventories to get familiar with those concepts.
Having some issues with my code and can't figure out the syntax error.
I'm trying to take output from a firewall and put it into an html file but i keep getting stuck. The command in its own separate file outputs a couple of lines of text. Note destination changed to protect it.
- hosts: firewall
gather_facts: no
tasks:
- name: Hardware Info
raw: show asset all
register: output
- local_action: lineinfile dest=dest destination regexp="{{item}}" insertafter="test" line={{item}}
with_items: output.stdout_lines
Error:
local_action: lineinfile dest=dest insertafter="test"
line='{{item}}'
^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets
when they start a value. For instance:
with_items:
{{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Change this line:
- local_action: lineinfile dest=dest destination regexp="{{item}}" insertafter="test" line={{item}}
To this:
- local_action: lineinfile dest=dest destination regexp="{{item}}" insertafter="test" line="{{item}}"
The extra quotes should help.
Note lineinfile is terrible from a configuration management point of view, I predict you'll pull your hair out in the future.
With the following code:
- name: Add the gii config for main.php on staging
blockinfile:
dest: "{{ www_path }}/protected/config/main.php"
marker: "//Gii"
insertafter: "\'modules\'=>array\\("
block: "{{ gii_content }}"
I'm getting a syntax error on OSX:
ERROR! Syntax Error while loading YAML.
The offending line appears to be:
marker: "//Gii"
insertafter: "\'modules\'=>array\\("
^ here
Yet on ubuntu, no such issue and the playbook runs seamlessly. Any ideas?
If you run this through a YAML parser it will tell you it discovered an unknown escape character, so OS X is right there. What is the purpose of \'? If the idea was to match that string and the backslash appears in the file like this, you should put two backslashes there:
- name: Add the gii config for main.php on staging
blockinfile:
dest: "{{ www_path }}/protected/config/main.php"
marker: "//Gii"
insertafter: "\\'modules\\'=>array\\("
block: "{{ gii_content }}"
If there are two backslashes right after array you would need to have 4 of them there, just for escaping. Though since insertafter takes a regular expression and ( has a special meaning in regular expressions, it might be necessary to actually put 6 of them there.
- name: Add the gii config for main.php on staging
blockinfile:
dest: "{{ www_path }}/protected/config/main.php"
marker: "//Gii"
insertafter: "\\'modules\\'=>array\\\\\\("
block: "{{ gii_content }}"