I am getting below warning error while converting data in a YAML file to CSV.
Skipping key ($ManagementNetworkSubnetMask) in group (ps_script) as it is not a mapping, it is a <class
'ansible.parsing.yaml.objects.AnsibleUnicode'>
Eventhough this error is not causing any harm , curious what to know what is causing this error. The playbook and YAML repo file using to convert to CSV is as below.
Repository YAML file
---
ps_script:
Variable: 'value'
$VIServer: 'Servername'
$VIUsername: 'Username'
$VIPassword: 'Password'
Playbook
---
- name: Enabling WorkLoad Cluster using NSX-T
hosts: localhost
connection: local
gather_facts: false
vars_files:
- central_repo.yml
tasks:
- name: Creates directory
file:
path: /home/test/NSX-T/
state: directory
- name: create csv from central yaml
lineinfile:
create: true
path: /home/test/NSX-T/NSX-T.csv
line: "{{ item.key + ',' + item.value }}"
loop: "{{ lookup('dict',ps_script) }}"
Related
I have a destination for file copy as a variable file_path, how can I copy files to that destination using template/copy modules?
Tried:
---
- hosts: localhost
connection: local
tasks:
- name: variable
command: echo "emptyDir"
register: file_path
- name: check var value
debug:
var: file_path
verbosity: 2
- name: copy mod
template:
src: "{{ item }}"
dest: "{{ file_path }}"
mode: 0755
with_items:
- "file1.txt"
- "file2.txt"
Error:
If copy module is used
copyModule error
If template module is used
templateModule error
But throws some dict - string error.
Any help is appreciated.
TIA
Check the issue could be with stdout, need to use file_path.stdout. Else it will be a dictionary.
I have JSON file with contents
env_variables.json
{ "server": "{{server}}" , "notes": "{{notes}}" }
and ansible-playbook as below: (to upload a lambda with the environment variables)
playbook.yml
- hosts: localhost
vars:
server: localhost
notes: hello this is localhost
tasks:
- name: Lambda creation/updation
lambda:
name: ansible_test
state: present
region: "eu-west-1"
role: 'arn:aws:iam::xyz:role/xyz_lambda'
zip_file: '{{ item.path }}'
runtime: 'python3.6'
environment_variables: "{{ lookup('file','/env_variables.json') | from_json }}"
handler: 'lambda_function.lambda_handler'
How can I use the above-mentioned "env_variables.json" file format to insert the environment variables with values mentioned in the "vars" directive of playbook.yml?
Use include_vars for that.
- name: Load data from json
include_vars: file=/env_variables.json
If this file is outside of normal lookup pathes, you can use set_fact module:
- name: Load data from json
set_fact:
loaded_data: '{{ lookup('file','/env_variables.json') | from_json }}'
I'm trying to fetch the names of host groups in Ansible that have a specific prefix. Right now, I'm trying to delegate a template task to servers under host groups with the prefix "config_".
I'm using json_query which uses JMESPath expressions. The query however is incorrect. Can anyone guess what I'm missing?
- name: Create configsvr config file
template: src=mongod.conf.j2 dest={{ mongod.conf.path }} owner=mongod group=mongod mode=0600
delegate_to: "{{ groups|json_query([?starts_with(#, `config_`)]) }}"
Error msg:
FAILED! => {"failed": true, "msg": "template error while templating string: unexpected char u'?' at 22. String: {{ groups|json_query([?starts_with(#, `config_m`)]) }}"}
You should simply use built-in patterns to select your target hosts.
---
- hosts: conf_*
tasks:
- name: Create configsvr config file
template:
src: mongod.conf.j2
dest: "{{ mongod.conf.path }}"
owner: mongod
group: mongod
mode: 0600
You can improve your inventory by using groups of groups, like so:
[conf:children]
conf_a
conf_b
conf_c
[conf_a]
srv1
[conf_b]
srv2
[conf_c]
srv3
And then target conf group in your playbook:
---
- hosts: conf
tasks:
- name: Create configsvr config file
template:
src: mongod.conf.j2
dest: "{{ mongod.conf.path }}"
owner: mongod
group: mongod
mode: 0600
i am new to ansible world. trying to providing all variable from json file, but its not accepting. it shows and error variable undefined
below is my json file
{
"Tomcat":{
"SHPN":"8905",
"HTPN":"8980",
"SSPN":"8943",
"AJPN":"8909",
"Server":"test.example.com",
"JENKINS_HOME":"/apps/tech/jenkins/jenkins-configuration",
"PName":"Tomcat-Installation",
"IName":"ansible_test",
"IUID":"jbosscfg",
"IGID":"staff",
"IDEPT":"tech",
"IRECPT":"test#example.com"
}
}
below is my playbook
---
-
gather_facts: false
hosts: "{{Server}}"
tasks:
-
ignore_errors: true
name: "find no of Tomcat Instance available on the Server"
copy:
src: "{{ JENKINS_HOME }}/workspace/{{ PName }}/instance/"
dest: /apps/tech/{{IName}}
group: "{{IGID}}"
owner: "{{IUID}}"
mode: 0755
-
replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<DEPARTMENT NAME>' replace='{{IDEPT}}'"
-
replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<RECIPIENT>' replace='{{IRECPT}}'"
-
replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<TOMCAT INSTANCE NAME>' replace='{{IName}}'"
-
replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<USER ID>' replace='{{IUID}}'"
-
name: "Ansible Template Example"
template:
src: tomcat_server.j2
dest: /apps/tech/{{IName}}/conf/server.xml
mode: 0777
i am below while executing the ansible playbook - error says variable undefined
test.example.com:~/final:$ ansible-playbook configure-tomcat-instance.yml --extra-vars "#test.json"
ERROR! the field 'hosts' has an invalid value, which appears to include a variable that is undefined. The error was: 'Server' is undefined
The error appears to have been in '/home/jbosscfg/final/configure-tomcat-instance.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
-
gather_facts: false
^ here
You refer variables as if they are at the top level, whereas they are under Tomcat key.
Use hosts: "{{Tomcat.Server}}" or reformat your json file to eliminate Tomcat level.
i am new to ansible and i am trying to achieve the following,
This is the sample playbook, here i am creating a reg key called "test" under HKLM:\SOFTWARE\myapp\Desk\ and i am adding 2 entries with items called hello and hi and i am assigning single same value "world" to both hello and hi.
i want to get the value for hello and hi from some file which will be stored in a repository or some url ..the value will be different for hello and hi.. is this possible ?
---
- hosts: web
tasks:
- name: Create and add reg entries
win_regedit:
path: HKLM:\SOFTWARE\myapp\Desk\Test
entry: "{{ item }}"
data: world
type: string
state: present
with_items:
- hello
- hi
Update:
Some improvement here for a newbie, i am able to iterate through with_items with key value , but i want to have a file with key:value and i want the ansible to iterate through all the left side keys and create them and also fill them with the right side values..that is the entry in win_Regedit should have key and data should have values
---
- hosts: web
tasks:
- name: Create and add reg entries
win_regedit:
path: HKLM:\SOFTWARE\myapp\Desk\Test
entry: "{{ item.regkey }}"
data: "{{ item.regvalue}}"
type: string
state: present
with_items:
- regkey: hello
regvalue: world
- regkey: hi
regvalue: universe
To include variable from some file, you can use the include_vars module:
include_vars:
file: vars.yml
Edit What you want to do is probably something like below (did not try the win_regedit as I don't have any windows node):
Create a vars.yml:
---
var1: 'key1'
var2: 'key2'
d1: "{
'{{var1}}': 'val1',
'{{var2}}': 'val2'
}"
Then launch the following playbook (see the with_dict statement):
---
- hosts: localhost
remote_user: my_user
tasks:
- name: pull var_file
url: http://example.com/path/to/vars
dest: /path/to/vars.yml
mode: 0440
- name: include_vars
include_vars:
/path/to/vars.yml
- name: Create and add reg entries
win_regedit:
path: HKLM:\SOFTWARE\myapp\Desk\Test
entry: "{{ item.key }}"
data: "{{ item.value}}"
type: string
state: present
with_dict: "{{d1}}"
Kudos to this answer for the with_dict trick
Use uri module.
---
- hosts: localhost
remote_user: my_user
tasks:
- name: pull var_file
uri:
url: http://example.com/path/to/vars
method: GET
return_content: yes
register: vars_from_url
- name: Create and add reg entries
win_regedit:
path: HKLM:\SOFTWARE\myapp\Desk\Test
entry: "{{ item.key }}"
data: "{{ item.value}}"
type: string
state: present
with_dict: "{{ vars_from_url.content }}"