I have the following task file in one of my roles:
- name: check for initalized fixtures
stat: path={{ fixture_init_file }}
register: fixture_init
- name: copy fixtures
copy: src={{ fixture_file }} dest={{ fixture_dest }}
when: fixture_init.stat.exists == False
- name: debug
debug: var=fixture_init
- name: db2
debug: var=database
- name: install fixtures (mysql)
shell: bzcat {{ fixture_dest }} | mysql --user=root {{ database.name }}
when: (fixture_init.stat.exists == False) and (database.type == "mysql")
register: fixture_init_result
- name: install password file (pgsql)
template: src=pgpass.j2 dest=/root/.pgpass mode=0600
when: database.type == "postgres"
- name: fixtures role map (pgsql)
template: src=gls-pgsql.j2 dest={{ fixture_pg_roles }} mode=0644
when: fixture_init_file.exists == False and database.type == "postgres"
- name: install role map (pgsql)
shell: psql -d {{ database.name }} < {{ fixture_pg_roles }}
when: fixture_init_file.exists == False and database.type == "postgres"
sudo_user: postgres
- name: install fixtures (pgsql)
shell: bzcat {{ fixture_dest }} | pg_restore -Fc -d {{ database.name }}
when: fixture_init_file.exists == False and database.type == "postgres"
sudo_user: postgres
register: fixture_init_result
- name: mark fixtured initalized
file: path={{ fixture_init_file }} state=touch
when: fixture_init_result|success
When I upgraded to Ansible 1.9.0.1, I now get the following error:
TASK: [database | install fixtures (pgsql)] ***********************************
fatal: [database] => error while evaluating conditional: fixture_init_file.exists == False and database.type == "postgres"
Those two debug statements show that the variables are properly defined:
TASK: [database | debug] ******************************************************
ok: [database] => {
"var": {
"fixture_init": {
"changed": false,
"invocation": {
"module_args": "path=/opt/bigsense/fixtures-initalized",
"module_name": "stat"
},
"stat": {
"exists": false
}
}
}
}
TASK: [database | db2] ********************************************************
ok: [database] => {
"var": {
"database": {
"ddl_password": "bigsense_ddl",
"ddl_username": "bigsense_ddl",
"name": "bigsense",
"password": "bigsense",
"type": "postgres",
"username": "bigsense"
}
}
}
Why do these conditionals no longer work?
The fixture_init_file.exists needs to be fixture_init_file.stat.exists ;)
Related
I have the following data inside of Ansible variable server_info, set via register at the end of a task;
ok: [server123] => {
"msg": {
"changed": false,
"failed": false,
"instances": [
{
"ami_launch_index": 0,
"architecture": "x86_64",
"block_device_mappings": [
{
"device_name": "/dev/sda1",
"ebs": {
"attach_time": "2021-02-11T11:48:30+00:00",
"delete_on_termination": false,
"status": "attached",
"volume_id": "vol-5436546546"
}
},
{
"device_name": "/dev/sda2",
"ebs": {
"attach_time": "2021-02-11T11:48:30+00:00",
"delete_on_termination": false,
"status": "attached",
"volume_id": "vol-3546456535"
}
}
],
"capacity_reservation_specification": {
"capacity_reservation_preference": "open"
},
"client_token": "",
"cpu_options": {
"core_count": 2,
"threads_per_core": 1
},
"ebs_optimized": false,
"ena_support": true,
"enclave_options": {
"enabled": false
},
"hibernation_options": {
"configured": false
},
"hypervisor": "xen",
"iam_instance_profile": {
"arn": "arn:aws:iam::6346346356:instance-profile/bla",
"id": "SXIUIAHFKBAFIUAEKBADF"
},
"image_id": "ami-65754765475476",
"instance_id": "i-4657654765476547765",
"instance_type": "t2.large",
"key_name": "bla",
"launch_time": "2021-02-15T08:51:44+00:00",
"maintenance_options": {
"auto_recovery": "default"
},
etc... etc...
}
]
}
}
I am attempting to retrieve the value of "instance_id", however I get the following error;
- name: "Instance ID"
debug:
msg: "{{ server_info.instances.instance_id }}"
TASK [Instance ID]
************************************************************************************************************ fatal: [server123]: FAILED! => {"msg": "The task includes an option
with an undefined variable. The error was: 'list object' has no
attribute 'instance_id'\n\nThe error appears to be in
'/ansible/testing.yml': line 38, column 7,
but may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n\n - name:
"Instance ID"\n ^ here\n"}
Where am I going wrong in my nested variable retrieval syntax?
Thank you.
See if the below code works
- name: get instance id
set_fact:
new_result: "{{ server_info.instances['instances'] }}"
register: filterd_result
- name: get all ids
vars:
reduce_query: >-
[].{
instance_id: instance_id
}
names: "{{ new_result | json_query(reduce_query)}}"
register: result_names
no_log: true
debug:
var: names
- name: display result
debug:
msg: "{{ result_names.names | map(attribute='instance_id') | flatten }}"
just modify your task:
- name: "Instance ID"
debug:
msg: "{{ server_info.instances.0.instance_id }}"
i suppose instances is an array with only one record ...
if you have more records in array you could use map:
- name: find all instances
debug:
msg:
- "==========================="
- "List of instances id"
- "==========================="
- '{{ server_info.instances | map(attribute="instance_id")|list}}'
- ""
I am trying to create a rabbitmq node using terraform and ansible scripts,Other scripts are executing successfully but I am facing a warning while running this script of adding a user in rabbitmq node.
[WARNING]: Module did not set no_log for update_password
failed: [rabbit-node1] (item=admin) => {
"ansible_loop_var": "item",
"changed": false,
"cmd": "/usr/sbin/rabbitmqctl -q -n rabbit list_users",
"invocation": {
"module_args": {
"configure_priv": ".*",
"force": false,
"node": "rabbit",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"permissions": [
{
"configure_priv": ".*",
"read_priv": ".*",
"vhost": "/",
"write_priv": ".*"
}
],
"read_priv": ".*",
"state": "present",
"tags": "administrator,admin",
"update_password": "on_create",
"user": "admin",
"vhost": "/",
"write_priv": ".*"
}
},
"item": "admin",
"msg": "Error:********#rabbit-node1.\n * Suggestion: start it with \"rabbitmqctl start_app\" and try again",
"rc": 70,
"stderr": "Error: rabbit application is not running on node rabbit#rabbit-node1.\n * Suggestion: start it with \"rabbitmqctl start_app\" and try again\n",
"stderr_lines": [
"Error: rabbit application is not running on node rabbit#rabbit-node1.",
" * Suggestion: start it with \"rabbitmqctl start_app\" and try again"
],
"stdout": "",
"stdout_lines": []
}
main.yml file for Creating user in Rabbitmq node using ansible:
- name: add user
rabbitmq_user:
user: "{{ item }}"
password: "{{ ADMIN_PASS }}"
tags: administrator,{{item}}
vhost: /
configure_priv: .*
write_priv: .*
read_priv: .*
state: present
with_items:
- admin
when getting an object from an S3 bucket using the following Ansible command:
- name: "copy object from s3://{{ s3_bucket }}/{{ s3_object }} to {{ dest }}"
s3:
bucket: "{{ s3_bucket }}"
object: "{{ s3_object }}"
dest: "{{ dest }}"
mode: get
I get the following error:
fatal: [som_fake_host]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"bucket": "some-fake-bucket",
"dest": "/some-fake-dest/",
"ec2_url": null,
"encrypt": true,
"expiry": "600",
"headers": null,
"ignore_nonexistent_bucket": false,
"marker": null,
"max_keys": "1000",
"metadata": null,
"mode": "get",
"object": "some_fake_file",
"overwrite": "always",
"permission": [
"private"
],
"prefix": null,
"profile": null,
"region": null,
"retries": 0,
"rgw": false,
"s3_url": null,
"security_token": null,
"src": null,
"validate_certs": true,
"version": null
}
},
"msg": "attempted to take checksum of directory: /some-fake-dest/"
}
additional useful information:
The destination directory exists
The user that runs the playbook has permission on the destination directory
The file exists in S3 bucket
Looking at the docs:
dest The destination file path when downloading an object/key with a GET operation.
Try to call module with file path, not directory. E.g.:
dest: "{{ dest }}/{{ s3_object }}"
or something.
Currently, I use curl to send an HTTP PUT to my API:
curl -k -s -u icinga:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/hosts/dbserver.example.com' -d '{ "templates": [ "generic-host" ], "attrs": { "zone": "vienna", "address": "xxx.xx.xx.x", "check_command": "hostalive", "vars.os" : "Linux", "vars.agent" : "ssh" } }' | python -m json.tool
This works like a charm.
I'm trying to convert this api call to an ansible playbook. I know that ansible offer the URI module, so I tried to use that, but perhaps something is not configured properly.
---
- name: Add new host
uri:
url: icinga2.example.com:5665/v1/objects/hosts/client.example.com
method: PUT
user: admin
password: xxxxxxx
body: { templates: [ "generic-host" ], attrs: { "zone": "vienna",
"address": "172.x.x.xx", "check_command": "hostalive", "vars.os" : "Linux", "vars.agent" : "ssh" } }
headers: "application/json"
register: icinga_log
when: inventory_hostname in groups ['vienna']
with_items: "{{ groups['icinga-monitoring'] }}"
Usually you could follow error messages that ansible produces and fix your syntax.
Try to start with this modification:
- name: Add new host
uri:
url: http://icinga2.example.com:5665/v1/objects/hosts/client.example.com
method: PUT
user: admin
password: xxxxxxx
body: '{ templates: [ "generic-host" ], attrs: { "zone": "vienna", "address": "172.x.x.xx", "check_command": "hostalive", "vars.os" : "Linux", "vars.agent" : "ssh" } }'
body_format: json
headers:
Content-Type: application/json
I have a problem with checking existing files using dictonary in Ansible.
tasks:
- name: Checking existing file id
stat: path=/tmp/{{ item.id }}.conf
with_items: "{{ file_vars }}"
register: check_file_id
- name: Checking existing file name
stat: path=/tmp/{{ item.name }}.conf
with_items: "{{ file_vars }}"
register: check_file_name
- name: Checking file exists
debug: msg='File_id exists'
when: check_file_id.stat.exists == True
- name: Checking file name exists
debug: msg='File name exists'
when: check_file_name.stat.exists == True
vars:
file_vars:
- { id: 1, name: one }
- { id: 2, name: two }
Then, if I trying to run playbook, I got the error:
FAILED! => {"failed": true, "msg": "The conditional check 'check_file_id.stat.exists == True' failed. The error was: error while evaluating conditional (check_file_id.stat.exists == True): 'dict' object has no attribute 'stat'\n\n
I've tried to debug it:
- debug: var=check_file_id
and got:
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_checksum": true,
"get_md5": true,
"mime": false,
"path": "/tmp/1.conf"
},
"module_name": "stat"
},
"item": {
"id": 1,
"name": "one"
},
"stat": {
"exists": false
}
},
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_checksum": true,
"get_md5": true,
"mime": false,
"path": "/tmp/2.conf"
},
"module_name": "stat"
},
"item": {
"id": 2,
"name": "two"
},
"stat": {
"exists": false
}
}
]
Where I am wrong?
Is is possible to use stat.exists with list of variables?
Thanks for answer!
The problem is that you are registering check_file_id in a loop. You need to read the documentation on using register in a loop, which discusses the implications of doing this. Your variable has a results key that contains the result of each iteration of the loop. You can see that in your debug output.
In subsequent tasks, you should iterate over check_file_id.results instead of file_vars, like this:
- hosts: localhost
gather_facts: false
vars:
file_vars:
- {id: 1, name: foo}
- {id: 2, name: bar}
tasks:
- name: Checking existing file id
stat:
path: ./files/{{ item.id }}.conf
with_items: "{{ file_vars }}"
register: check_file_id
- name: Checking existing file name
stat:
path: ./files/{{ item.name }}.conf
with_items: "{{ file_vars }}"
register: check_file_name
- debug:
msg: 'file id {{item.item.id}} (name {{item.item.name}}) exists'
with_items: "{{ check_file_id.results }}"
when: item.stat.exists
- debug:
msg: 'file name {{item.item.name}} (id {{item.item.id}}) exists'
with_items: "{{ check_file_name.results }}"
when: item.stat.exists