This is not the exact same code. If you look carefully at the example, you'll see that under users, you have several dicts.
In your case, you have two dicts but with just one key (alice, or telephone) with respective values of "Alice", 123.
You'd rather do :
- hosts: localhost
gather_facts: no
tasks:
- name: print phone details
debug: msg="user {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"
vars:
users:
alice:
name: "Alice"
telephone: 123
(note that I changed host to localhost so I can run it easily, and added gather_facts: no since it's not necessary here. YMMV.)
This is not the exact same code. If you look carefully at the example, you'll see that under users, you have several dicts.
In your case, you have two dicts but with just one key (alice, or telephone) with respective values of "Alice", 123.
You'd rather do :
- hosts: localhost
gather_facts: no
tasks:
- name: print phone details
debug: msg="user {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"
vars:
users:
alice:
name: "Alice"
telephone: 123
(note that I changed host to localhost so I can run it easily, and added gather_facts: no since it's not necessary here. YMMV.)
You want to print {{ item.value.name }} but the name is not defined.
users:
alice: "Alice"
telephone: 123
should be replaced by
users:
name: "Alice"
telephone: 123
Then both the name and the telephone attribute are defined within the dict (users).
'dict object' has no attribute.... - Ansible Project - Ansible
ansible 2.x - Why am i getting "AnsibleUndefinedVariable: 'dict object' has no attribute 'default_ipv4'" - Stack Overflow
Ansible 'dict object' has no attribute 'stdout'"
argument_spec: default value for dict entries do not work
To Solve this issue you need first collect all the facts of all nodes to make this work. your hosts.j2(template) file should look like this
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}
and the YAML file that you are going to run as a ansible playbook should look like this
- hosts: all
tasks:[]
- hosts: all
tasks:
- template:
src: "/root/ansible/hosts.j2"
dest: "/etc/myhosts"
As you can see the first two lines of the YAML code will collect all the facts that will be used by the below task. And make sure that all the nodes should be active and ansible ping should give pong for all the nodes.
instead of default_ipv4 try ansible_default_ipv4 it is a correct ansiblefact variable.
I'm struggling agains this problem.
That's my variable file:
distribution_major_version: 18
prereq_packages:
16:
- 1
- 2
- 3
18:
- 4
- 5
- 6This playbook works:
---
-
hosts: localhost
tasks:
- name: Print string from manifest
debug:
msg: "{{prereq_packages[distribution_major_version] }}"this not:
---
-
hosts: localhost
tasks:
- name: Print string from manifest
debug:
msg: "{{prereq_packages[ansible_distribution_major_version] }}"
with this error:
TASK [Print string from manifest] *******************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'18'\n\nThe error appears to be in '/home/local/RISORSA/gorgellino/playbook/local/prova_variabili.yml': line 5, 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 string from manifest\n ^ here\n"}I am trying to get only a portion of the ouput of a command and store the value in a list (for future looping)
tasks: - name: Execute the command win_shell: <command> register: <variable> with_items: - <host1> - <host2>
- name: print the variable debug: var=<variable>
gives this output:
ok: [immgt4] => { "msg": { "changed": true, "msg": "All items completed", "results": [ { "_ansible_ignore_errors": null, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": "Install-Module -Name VMware.PowerCLI -scope CurrentUser;Connect-Viserver vcyow -Credential (Import-clixml c:\pkgs\vm.clixml) -Force > null;(get-vmhost -Name esxfbsmesg1.corp.navcan.ca | get-vm).name", "delta": "0:00:08.615218", "end": "2018-04-11 06:06:34.589090", "failed": false, "item": "esxfbsmesg1.corp.navcan.ca", "rc": 0, "start": "2018-04-11 06:06:25.973871", "stderr": "", "stderr_lines": [], "stdout": "test-host-patching3\r\ntest-host-patching1\r\ntest-host-patching\r\n", "stdout_lines": [ "test-host-patching3", "test-host-patching1", "test-host-patching" ] }, { "_ansible_ignore_errors": null, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": "Install-Module -Name VMware.PowerCLI -scope CurrentUser;Connect-Viserver vcyow -Credential (Import-clixml c:\pkgs\vm.clixml) -Force > null;(get-vmhost -Name esxfbsmesg2a.corp.navcan.ca | get-vm).name", "delta": "0:00:09.022712", "end": "2018-04-11 06:06:44.424300", "failed": false, "item": "esxfbsmesg2a.corp.navcan.ca", "rc": 0, "start": "2018-04-11 06:06:35.401588", "stderr": "", "stderr_lines": [], "stdout": "test-host-patching5\r\ntest-vm-snapshot4\r\n", "stdout_lines": [ "test-host-patching5", "test-vm-snapshot4" ] } ] } }
question: How can i store only the stdout_lines into a new variable it being a list?
when i try anything of the sort of: debug: var=list_vms.stdout
I get this error: The error was: 'dict object' has no attribute 'stdout'
I am very new to ansible and would appreciate your help!
thanks!
The goal for me is to get the result from the first command, put it in a variable and loop trough that list for another command.
The syntax you included:
when: me.cool is not defined
is correct.
You can also use not in:
when: "'cool' not in me"
The problem is that your error message:
The conditional check 'me.cool' failed.
claims your condition is defined as:
when: me.cool
So, either there is some bug in the version you use (but you did not share which one it is) and there were known issues, or you did not post the exact task that caused the error.
You can avoid 'dict object' has no attribute by using jinja2 selectattr() syntax as in :
when: me|selectattr("cool", "defined")|list|length >0
idea obtained from Michael Hoglan at https://groups.google.com/forum/#!topic/ansible-project/8XJkHQgttLA
I have the following template, which stopped working
{% for host in groups['execd'] %}
{{ hostvars[host]['ansible_default_ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['ansible_fqdn'] }}
{% endfor %}it started throwing this error: AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_default_ipv4'
I tried it on versions:
ansible 2.4.0 (devel 8e6c0ca599)
ansible 2.0.0.2
Anyone having an idea why this doesn't work anymore ?
EDIT: I just figured it out, one of my hosts in my inventory was unreachable (it's a test environment). removing it from the inventory solves the problem
I'm trying to use the Ansible module stat to determine if /etc/snmp/snmpd.conf exists or not.
This is the contents of the .yml file
--- # v-72313 must remove snmp community strings
- hosts: rhel7-servers
user: ansible
become: yes
become_user: root
connection: ssh
gather_facts: no
tasks:
- name: V-72313 must remove snmp community strings
stat:
path: /etc/snmp/snmpd.conf
register: s
- debug:
msg: "Path exists and is a directory"
when: s.exists
- debug:
msg: "Path and file not found"
when: s.exists == False
When I run this, it displays the following error:
fatal: [192.168.100.2]: FAILED! =>
msg: |-
The conditional check 't.exists == True' failed. The error was: error while evaluating conditional (t.exists == True): 'dict object' has no attribute 'exists'
The error appears to be in '/home/ansible/playbooks/stig_high/test_path.yml': line 13, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
register: t
- debug:
^ here
fatal: [192.168.100.3]: FAILED! =>
msg: |-
The conditional check 't.exists == True' failed. The error was: error while evaluating conditional (t.exists == True): 'dict object' has no attribute 'exists'
The error appears to be in '/home/ansible/playbooks/stig_high/test_path.yml': line 13, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
register: t
- debug:
^ here
fatal: [192.168.100.4]: FAILED! =>
msg: |-
The conditional check 't.exists == True' failed. The error was: error while evaluating conditional (t.exists == True): 'dict object' has no attribute 'exists'
The error appears to be in '/home/ansible/playbooks/stig_high/test_path.yml': line 13, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
register: t
- debug:
^ here
PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.100.2: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
192.168.100.3: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
192.168.100.4: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
I've hit a dead end with this and can't figure out why.
I created a generic test directory under /tmp with a subdirectory and some files in it on a remote test server just to see if I would get the same output and I am. So its something that I'm doing with the syntax of the yml.