I got below error while running my playbook (below)
fatal: [192.168.22.200]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: float object has no element 55\n\nThe error appears to be in '/home/subbu/Downloads/lab/test-02.yml': line 14, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Export configuration\n ^ here\n"}
test-02.yml:
---
- name: Running config
hosts: ALL
connection: local
collections:
- paloaltonetworks.panos
tasks:
- name: Load Variables
include_vars: vars.yml
- name: Export configuration
panos_export:
provider: '{{ provider }}'
category: 'configuration'
filename: 'running-config.xml'
Here is my vars file used in above playbook
vars.yml:
provider:
ip_address: "{{ 192.168.22.200 }}"
username: 'admin'
password: 'seeyousoon'
In your vars.yml, try removing the curly brackets and using single quotes for ip_address.
i.e. ip_address: '192.168.22.200'
Related
I'm trying to add environment variables to a Linux machine using vars like below:
vars:
app_env_variables:
- key: APP_NAME
value: App demo
- key: APP_ENV
value: demo
- key: APP_KEY
value: xxxxx
I'm using this task to add the variables to /etc/environment.
- name: customize /etc/environment
ansible.builtin.lineinfile:
path: /etc/environment
state: present
regexp: "^{{ item.key }}="
line: "{{ item.key }}={{ item.value }}"
with_items: "{{ app_env_variables }}"
But I'm getting the error below:
TASK [customize /etc/environment] **********************************************
fatal: [default]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'key'\n\nThe error appears to be in 'demo-playbook.yaml': line 423, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n ssh-add /root/.ssh/id_rsa\n - name: customize /etc/environment\n ^ here\n"}
What am I missing here?
The playbook below
shell> cat playbook.yml
- hosts: localhost
vars:
app_env_variables:
- key: APP_NAME
value: App demo
- key: APP_ENV
value: demo
- key: APP_KEY
value: xxxxx
tasks:
- name: customize /tmp/environment
ansible.builtin.lineinfile:
create: true
path: /tmp/environment
state: present
regexp: "^{{ item.key }}="
line: "{{ item.key }}={{ item.value }}"
with_items: "{{ app_env_variables }}"
works as expected
shell> ansible-playbook playbook.yml
PLAY [localhost] *****************************************************************************
TASK [customize /tmp/environment] ************************************************************
changed: [localhost] => (item={'key': 'APP_NAME', 'value': 'App demo'})
changed: [localhost] => (item={'key': 'APP_ENV', 'value': 'demo'})
changed: [localhost] => (item={'key': 'APP_KEY', 'value': 'xxxxx'})
PLAY RECAP ***********************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
and created the file
shell> cat /tmp/environment
APP_NAME=App demo
APP_ENV=demo
APP_KEY=xxxxx
I only added the parameter create: true to avoid error
msg: Destination /tmp/environment does not exist !
'demo-playbook.yaml': line 423, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem
i think your having some syntax error in your .yaml file
Can someone help me in finding the best way to declare all variables inside a file and define the file path in ansible playbook? Here's my ansible-playbook
---
- hosts: myhost
vars:
- var: /root/dir.txt
tasks:
- name: print variables
debug:
msg: "username: {{ username }}, password: {{ password }}"
These are the contents inside dir.txt
username=test1
password=mypassword
When I run this, I am facing an error
TASK [print variables] *********************************************************************************************************
fatal: [121.0.0.7]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'username' is undefined\n\nThe error appears to be in '/root/test.yml': line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: print variables\n ^ here\n"}
Expected output is to print the variables by this method
Any help would be appreciated
There are two reasons why your code won't work.
A variables file should be in YAML or JSON format for Ansible to load it
Such variables files are loaded with vars_files directive or include_vars task, rather than vars
So a YAML file named /root/dir.yml:
username: test1
password: mypassword
Can be used in a playbook like:
---
- hosts: myhost
vars_files:
- /root/dir.yml
tasks:
- name: print variables
debug:
msg: "username: {{ username }}, password: {{ password }}"
I wrote a python script which gets executed via my ansible playbook and returns the following output via stdout:
- { username: ansible, admin: yes}
- { username: test, admin: no }
The output then should get saved in the variable "users" and with the "with_items" (or the newer "loop" conditional) I want to iterate through the variable in order to assign the right permissions for each user separately:
- name: execute python script
command: "python3 /tmp/parse_string.py --user_permissions={{ user_permissions }}"
register: output
- name: register
set_fact:
users: "{{ output.stdout }}"
- name: output
debug: msg="{{ users }}"
- name: Add user to group -admin
user:
name={{ item.username }}
groups=admin
append=yes
state=present
when: "item.admin == yes"
with_items: '{{users}}
However when launching the playbook it says that the variable "users" has no attribute "username".
TASK [create_users : output] ***************************************************
ok: [ansible] => {
"msg": "- { username: ansible, admin: yes }\n- { username: test, admin: no }\n- { username: test2, admin: no }"
}
TASK [create_users : Add user to group -admin ***************
fatal: [ansible]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'username'\n\nThe error appears to be in '***': line 29, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \n ^ here\n"}
Can anyone help me with this case?
BR
You are setting your users var to a string. It happens that this string is a yaml representation of a datastructure but ansible has no clue about that at this point.
To achieve your requirement, you need to parse that string as yaml and register the result. Luckily, there is a from_yaml filter for that purpose
You just have to modify your set_fact task as the following and everything should work as expected:
- name: register
set_fact:
users: "{{ output.stdout | from_yaml }}"
I use ansible-module-vcloud, and I want to create VMs via Ansible. For example, I want to create easiest playbook.
I have this code:
---
- name: vCloudDirectorAnsible
hosts: localhost
environment:
env_user: admin
env_password: admin
env_host: vcloud.vmware.ru
env_org: test
env_api_version: 30.0
env_verify_ssl_certs: false
- name: create catalog
vcd_catalog:
catalog_name: "test"
catalog_description: "test_Descr"
state: "present"
But I got error:
ERROR! 'vcd_catalog' is not a valid attribute for a Play
The error appears to have been in '/root/ansible-module-vcloud-director/main.yml': line 14, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: create catalog
^ here
If I delete this part:
- name: create catalog
vcd_catalog:
catalog_name: "test"
catalog_description: "test_Descr"
state: "present"
My playbook will run and successfully completed.
How to fix this?
You missed the tasks keyword.
---
- name: vCloudDirectorAnsible
hosts: localhost
environment:
env_user: admin
env_password: admin
env_host: vcloud.vmware.ru
env_org: test
env_api_version: 30.0
env_verify_ssl_certs: false
tasks:
- name: create catalog
vcd_catalog:
catalog_name: "test"
catalog_description: "test_Descr"
state: "present"
I have Ansible role "db" with simple task:
- name: Check repos
apt_repository: repo="{{ item.repo }}" state={{ item.state }}
with_items:
- "{{ apt_repos }}"
In /defaults/mail.yml:
apt_repos:
# Percona
- { state: present, repo: 'deb http://repo.percona.com/apt wheezy main', keyserver: 'keyserver.ubuntu.com', key: '1C4CBDCDCD2EFD2A', needkey: True }
- { state: present, repo: 'deb-src http://repo.percona.com/apt wheezy main', needkey: False }
When i try to run this ansible-playbook:
---
- hosts: test
roles:
- db
i see error:
fatal: [10.10.10.10] => One or more undefined variables: 'unicode object' has no attribute 'repo'
FATAL: all hosts have already failed -- aborting
But i have another role with same task and variable and it work perfectly. What's wrong?
You want to be doing this:
with_items: apt_repos
apt_repos is a list. By referencing it as - "{{ apt_repos }}" the extra - is turning it into a list of lists. You also don't need the quotes or braces in this case - those are pretty much just redundant in this type of situation.