Hey All, Receiving the following from Ansible 2.7 and not sure if I should be predefining a variable and pointing to this fact first or perhaps this is related to something else? (Relatively new to Ansible) fatal: [mysql04]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'"} [root@awx01 ansible]# ansible --version ansible 2.7.0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /bin/ansible python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] [root@awx01 ansible]# [root@awx02 mysql]# cat templates/my.cnf.j2 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql binlog_format=ROW bind-address="{{ ansible_default_ipv4.address }}" default_storage_engine=innodb innodb_autoinc_lock_mode=2 innodb_flush_log_at_trx_commit=0 innodb_buffer_pool_size=122M wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so wsrep_provider_options="gcache.size=300M; gcache.page_size=300M" wsrep_cluster_name="galera_cluster1" wsrep_cluster_address="gcomm://{% for host in groups['mysql']%}{{hostvars[host]['ansible_default_ipv4']['address']}}{% if not loop.last %},{% endif %}{% endfor %}" wsrep_sst_method=rsync server_id=1 wsrep_node_address="{{ ansible_default_ipv4.address }}" wsrep_node_name="{{ ansible_hostname }}" [mysql_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [root@awx02 mysql]# The fact exists and is defined on the target: [root@awx01 ansible]# ansible mysql04 -m setup | grep -Ei 192.168.0.109 -A 2 -B 2 "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.0.109" ], "ansible_all_ipv6_addresses": [ -- }, "ansible_default_ipv4": { "address": "192.168.0.109", "alias": "eth0", "broadcast": "192.168.0.255", -- "SHLVL": "2", "SSH_CLIENT": "192.168.0.142 49456 22", "SSH_CONNECTION": "192.168.0.142 49456 192.168.0.109 22", "SSH_TTY": "/dev/pts/1", "TERM": "xterm", -- "hw_timestamp_filters": [], "ipv4": { "address": "192.168.0.109", "broadcast": "192.168.0.255", "netmask": "255.255.255.0", -- "inode_used": 621, "mount": "/n/mds.xyz", "options": "rw,relatime,vers=4.1,rsize=8192,wsize=8192,namlen=255,hard,proto=tcp,port=0,timeo=10,retrans=2,sec=sys,clientaddr=192.168.0.10 ,local_lock=none,addr=192.168.0.80", "size_available": 137319276544, "size_total": 137371844608, [root@awx01 ansible]#-- Cheers, TK.
# cat main.yml
---
- name: Gather all facts prior to execution
hosts: mysql
gather_facts: yes
- name: Install and configure MySQL
hosts: mysql
sudo: yes
roles:
- mysql
tags: mysql
# vi tasks/main.yml
# These tasks install the community MySQL Server.
- include_tasks: variables.yml
# Place the my.cnf file on the target hosts.
- name: Copy my.cnf global MySQL configuration.
template:
src: my.cnf.j2
dest: "{{ mysql_config_file }}"
owner: root
group: root
mode: 0644
tags: mysql
Hi,
Am 04.11.18 um 00:45 schrieb Tom K.:
> Thanks Uwe!
>
> |
> # cat main.yml
> ---
>
> -name:Gatherall facts prior to execution
> hosts:mysql
> gather_facts:yes
>
>
> -name:Installandconfigure MySQL
> hosts:mysql
> sudo:yes
> roles:
> -mysql
> tags:mysql
>
> |
First of all: you don't need the first play gathering facts if you don't suppress it in the second.
"gather_facts: true" is default and thus will be executed in every play (something might have changed
due to actions in the last play…)
# cat main.yml
---
- name: Gather all facts prior to execution
hosts: mysql
gather_facts: false
tasks:
- name: Dump ansible_default_ipv4 variable output
copy:
dest: /tmp/ansible_default_ipv4.yaml
content: '{{ ansible_default_ipv4 | to_nice_yaml }}'
- fail:
- name: Install and configure MySQL
hosts: mysql
sudo: yes
roles:
- mysql
tags: mysql
# cat tasks/variables.yml
---
- name: Include OS-specific variables (RedHat) .
include_vars: "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
when: ansible_os_family == "RedHat"
- name: Define mysql_config_file variable .
set_fact:
mysql_config_file: "{{ __mysql_config_file }}"
when: mysql_config_file is not defined
- debug:
msg: "var = {{ ansible_default_ipv4.address }}"
when: ansible_default_ipv4.address is defined
- name: Define the mysql_ipv4 variable
set_fact:
mysql_ipv4: "{{ ansible_default_ipv4.address }}"
when: mysql_ipv4 is not defined
- name: Print all variables for each remote device
debug:
var: hostvars[inventory_hostname]
TASK [mysql : Print all variables for each remote device] *************************************************************************************************
task path: /ansible/roles/mysql/tasks/variables.yml:21
ok: [mysql04] => {
"hostvars[inventory_hostname]": {
"__mysql_config_file": "/etc/my.cnf",
"ansible_all_ipv4_addresses": [
"192.168.0.109"
],
"ansible_all_ipv6_addresses": [
"fe80::250:56ff:fe86:e11b"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/14/2014",
"ansible_bios_version": "6.00",
"ansible_check_mode": true,
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-693.21.1.el7.x86_64",
"LANG": "en_US.UTF-8",
"biosdevname": "0",
"crashkernel": "auto",
"net.ifnames": "0",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_date_time": {
"date": "2018-11-04",
"day": "04",
"epoch": "1541340261",
"hour": "09",
"iso8601": "2018-11-04T14:04:21Z",
"iso8601_basic": "20181104T090421500144",
"iso8601_basic_short": "20181104T090421",
"iso8601_micro": "2018-11-04T14:04:21.500460Z",
"minute": "04",
"month": "11",
"second": "21",
"time": "09:04:21",
"tz": "EST",
"tz_offset": "-0500",
"weekday": "Sunday",
"weekday_number": "0",
"weeknumber": "44",
"year": "2018"
},
"ansible_default_ipv4": {
"address": "192.168.0.109",
"alias": "eth0",
"broadcast": "192.168.0.255",
"gateway": "192.168.0.1",
"interface": "eth0",
"macaddress": "00:50:56:86:e1:1b",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.0.0",
"type": "ether"
},
"HostVarsVars" looks suspicious in the error message. Where does it come from? Find it and check if this is what you want.
Did you check /tmp on your mysql host? This will copy 'content' to the destination on the target host, not on the host
running the playbook.
[root@mysql04 tmp]# ls -altri /tmp/ansible_default_*
ls: cannot access /tmp/ansible_default_*: No such file or directory
[root@mysql04 tmp]#
[root@awx02 mysql]# pwd
/ansible/roles/mysql
[root@awx02 mysql]# cat templates/test.j2
# {{ ansible_managed }}
My default IPv4 address is: {{ ansible_default_ipv4["address"] }}
My other hostvars are:
{{ hostvars[inventory_hostname] | to_nice_yaml }}
[root@awx02 mysql]#
[root@awx01 ansible]# pwd
/ansible
[root@awx01 ansible]# cat main.yml
---
- name: Gather all facts prior to execution
hosts: mysql
gather_facts: true
tasks:
- debug: msg='{{ inventory_hostname }} has default IP {{ ansible_default_ipv4["address"] }}'
- template:
src: test.j2
dest: /tmp/test.out
- name: Install and configure MySQL
hosts: mysql
sudo: yes
roles:
- mysql
tags: mysql
[root@awx01 ansible]#
[root@mysql04 tmp]# ls -altri /tmp/test*
ls: cannot access /tmp/test*: No such file or directory
[root@mysql04 tmp]#
[root@awx01 ansible]# ls -altri /tmp/test*
ls: cannot access /tmp/test*: No such file or directory
[root@awx01 ansible]#
PLAY [Gather all facts prior to execution] ****************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************
task path: /ansible/main.yml:2
Would you mind sending the complete output of
ansible-playbook main.yaml -vvv
?
[root@mysql04 tmp]# cat /tmp/test.out|grep -Ei ansible_default_ipv4 -A5 -B5
tz_offset: '-0500'
weekday: Sunday
weekday_number: '0'
weeknumber: '44'
year: '2018'
ansible_default_ipv4:
address: 192.168.0.109
alias: eth0
broadcast: 192.168.0.255
gateway: 192.168.0.1
interface: eth0
[root@mysql04 tmp]#
fatal: [mysql04]: FAILED! => {
"changed": false,
"msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'"
}
ansible-playbook -i infra --limit mysql04 main.yml --tags "mysql" -vvv
[root@awx01 ansible]# cat main.yml
---
- name: Gather all facts prior to execution
hosts: mysql
gather_facts: true
tasks:
- debug: msg='{{ inventory_hostname }} has default IP {{ ansible_default_ipv4["address"] }}'
- template:
src: test.j2
dest: /tmp/test.out
tags: mysql
- name: Install and configure MySQL
> > ansible-proje...@googlegroups.com <javascript:> <mailto:ansible-project+unsub...@googlegroups.com <javascript:>>.
> > To post to this group, send email to ansible...@googlegroups.com <javascript:>
> <mailto:ansible...@googlegroups.com <javascript:>>.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com>
> >
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
> > For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
TASK [mysql : Print all variables for each remote device] *************************************************************************************************
task path: /ansible/roles/mysql/tasks/variables.yml:21ok: [mysql04] => { "hostvars[inventory_hostname]": { "__mysql_config_file": "/etc/my.cnf", "ansible_all_ipv4_addresses": [ "192.168.0.109" ], "ansible_all_ipv6_addresses": [ "fe80::250:56ff:fe86:e11b" ], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "04/14/2014", "ansible_bios_version": "6.00",
"ansible_check_mode": false,
"ansible_cmdline": { "BOOT_IMAGE": "/vmlinuz-3.10.0-693.21.1.el7.x86_64", "LANG": "en_US.UTF-8", "biosdevname": "0", "crashkernel": "auto", "net.ifnames": "0", "quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true, "ro": true, "root": "/dev/mapper/centos-root" }, "ansible_date_time": { "date": "2018-11-04", "day": "04",
"epoch": "1541369584", "hour": "17", "iso8601": "2018-11-04T22:13:04Z", "iso8601_basic": "20181104T171304330516", "iso8601_basic_short": "20181104T171304", "iso8601_micro": "2018-11-04T22:13:04.330753Z", "minute": "13", "month": "11", "second": "04", "time": "17:13:04",
> > > ansible-proje...@googlegroups.com <javascript:> <mailto:ansible-project+unsub...@googlegroups.com
> <javascript:> <javascript:>>.
> > > To post to this group, send email to ansible...@googlegroups.com <javascript:>
> > <mailto:ansible...@googlegroups.com <javascript:>>.
> > > To view this discussion on the web visit
> > > https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com>
> > <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com>>
> > >
> >
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com?utm_medium=email&utm_source=footer>
>
> >
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com?utm_medium=email&utm_source=footer>>>.
>
> >
> > > For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>
> <https://groups.google.com/d/optout <https://groups.google.com/d/optout>>.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <javascript:> <mailto:ansible-project+unsub...@googlegroups.com <javascript:>>.
> > To post to this group, send email to ansible...@googlegroups.com <javascript:>
> <mailto:ansible...@googlegroups.com <javascript:>>.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/c2f987b7-d1af-4739-9fc4-5c573f436ab8%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/c2f987b7-d1af-4739-9fc4-5c573f436ab8%40googlegroups.com>
> >
> <https://groups.google.com/d/msgid/ansible-project/c2f987b7-d1af-4739-9fc4-5c573f436ab8%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/c2f987b7-d1af-4739-9fc4-5c573f436ab8%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
> > For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
[root@awx01 ansible]# rpm -aq|grep -Ei ansible
ansible-inventory-grapher-2.4.4-1.el7.noarch
ansible-lint-3.4.21-1.el7.noarch
ansible-review-0.13.4-1.el7.noarch
ansible-doc-2.5.3-1.el7.noarch
ansible-2.7.0-1.el7.noarch
[root@awx01 ansible]#
--
Kai Stian Olstad
[root@awx01 ansible]# vi main.yml
---
- name: Gather all facts prior to execution
hosts: mysql
gather_facts: true
tasks:
- debug: msg='{{ inventory_hostname }} has default IP {{ ansible_default_ipv4["address"] }}'
- template:
src: test.j2
dest: /tmp/test.out
tags: mysql
- name: Install and configure MySQL
hosts:
mysql
become: true
roles:
- mysql
tags: mysql
[mysql]
mysql04
ansible-playbook -i infra --limit mysql04 main.yml --tags "mysql" -v
[mysql]
mysql01
mysql02
mysql03
mysql04
TASK [mysql : Copy my.cnf global MySQL configuration.] *****************************************************************************************************
fatal: [mysql04]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'"}
to retry, use: --limit @/ansible/main.retry
PLAY RECAP *************************************************************************************************************************************************
mysql04 : ok=13 changed=1 unreachable=0 failed=1
[root@awx01 ansible]#
TASK [debug] ***********************************************************************************************************************************************
ok: [mysql04] => {
"msg": "mysql04 has default IP 192.168.0.109"
}
[mysql]
mysql01
mysql02
mysql03
mysql04
ansible-playbook -i infra --limit mysql02 main.yml --tags "mysql" -v
ansible-playbook -i infra --limit mysql03 main.yml --tags "mysql" -v
# ansible-playbook -i infra main.yml --tags "mysql" -v
[root@mysql04 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
binlog_format=
ROW
bind-address="192.168.0.109"
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=122M
wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so
wsrep_provider_options="gcache.size=300M; gcache.page_size=300M"
wsrep_cluster_name="galera_cluster1"
wsrep_cluster_address='gcomm://192.168.0.109,192.168.0.102,192.168.0.111'
wsrep_sst_method=rsync
server_id=1
wsrep_node_address="192.168.0.109"
wsrep_node_name="mysql04"
[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@mysql04 ~]#
ansible-playbook -i infra --limit mysql05 main.yml --tags "mysql" -v --check
fatal: [mysql05]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'"}
ansible-playbook -i infra --limit mysql06 main.yml --tags "mysql" -v --check
fatal: [mysql06]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'"}