Does the first line of /tmp/test.out contain the correct IP address? If so, 
templating is working correctly.

Also please keep in mind that we are no mind readers and stripping away context to make output shorter usually doesn't help. That said please provide the complete output of a run with -vvv.


Am 04.11.18 um 16:25 schrieb Tom K.:


On Sunday, November 4, 2018 at 10:20:56 AM UTC-5, Uwe Sauter wrote:

    Would you mind sending the complete output of

    ansible-playbook main.yaml -vvv

    ?



I got a result now but need to continue this in a bit as I need to step away:

|
[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]#

|

Still same message:

|
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:Gatherall 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:Installandconfigure MySQL
   hosts:mysql
   sudo:yes
   roles:
-mysql
   tags:mysql


[root@awx01 ansible]#
|


    Am 04.11.18 um 16:16 schrieb Tom K.:
     >
     >
     > On Sunday, November 4, 2018 at 10:03:49 AM UTC-5, Uwe Sauter wrote:
     >
     >     OK, more comments:
     >
     >      >
     >      > So I've modified the first main.yml as follows:
     >      >
     >      > |
     >      > # cat main.yml
     >      > ---
     >      >
     >      >
     >      > -name:Gatherall facts prior to execution
     >      >    hosts:mysql
     >      >    gather_facts:false
     >
     >     Not gathering facts here will lead you to miss many of the ansible_* 
variables.
     >
     >      >    tasks:
     >      > -name:Dumpansible_default_ipv4 variable output
     >      >        copy:
     >
     >     The result of the copy module can be found on the target host(s), 
not on the host running the playbook. But
    in this
     >     case
     >     it should already fail as you don't gather facts and thus miss 
ansible_default_ipv4.
     >
     >     Try the following:
     >
     >
     >
     >     #### test.yaml ####
     >     ---
     >     - name: test templating
     >         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
     >
     >     #### end test.yaml ####
     >
     >     #### test.j2 ####
     >     # {{ ansible_managed }}
     >     My default IPv4 address is: {{ ansible_default_ipv4["address"] }}
     >
     >     My other hostvars are:
     >     {{ hostvars[inventory_hostname] | to_nice_yaml }}
     >     #### end test.j2 ####
     >
     >     You should get a debug output for each of your hosts and there 
should be a /tmp/test.out on each of your
    hosts from the
     >     mysql group.
     >
     >
     >
     > |
     > [root@awx02 mysql]# pwd
     > /ansible/roles/mysql
     > [root@awx02 mysql]#  cat templates/test.j2
     > # {{ ansible_managed }}
     > MydefaultIPv4address is:{{ansible_default_ipv4["address"]}}
     >
     >
     > Myother hostvars are:
     > {{hostvars[inventory_hostname]|to_nice_yaml }}
     > [root@awx02 mysql]#
     > |
     >
     >
     > |
     > [root@awx01 ansible]# pwd
     > /ansible
     > [root@awx01 ansible]# cat main.yml
     > ---
     > -name:Gatherall 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:Installandconfigure MySQL
     >    hosts:mysql
     >    sudo:yes
     >    roles:
     > -mysql
     >    tags:mysql
     >
     >
     > [root@awx01 ansible]#
     >
     > |
     >
     > |
     > [root@mysql04 tmp]# ls -altri /tmp/test*
     > ls:cannot access /tmp/test*:Nosuch file ordirectory
     > [root@mysql04 tmp]#
     >
     >
     > [root@awx01 ansible]# ls -altri /tmp/test*
     > ls:cannot access /tmp/test*:Nosuch file ordirectory
     > [root@awx01 ansible]#
     >
     > |
     >
     >
     > Don't think I got it right on though because it printed nothing:
     >
     > |
     > PLAY [Gatherall facts prior to
     >
    
execution]****************************************************************************************************************

     >
     >
     > TASK
     >
    
[GatheringFacts]************************************************************************************************************************************

     > task path:/ansible/main.yml:2
     > |
     >
     >
     >
     >
     >      >          dest:/tmp/ansible_default_ipv4.yaml
     >      >          content:'{{ ansible_default_ipv4 | to_nice_yaml }}'
     >      > -fail:
     >      >
     >      >
     >      > -name:Installandconfigure MySQL
     >      >    hosts:mysql
     >      >    sudo:yes
     >      >    roles:
     >      > -mysql
     >      >    tags:mysql
     >      >
     >      > |
     >      >
     >      > But couldn't find anything printed into 
/tmp/ansible_default_ipv4.yaml anywhere.  ( Now I could have
    mocked it up
     >     given
     >      > how green I am with this. )
     >      >
     >      > However, I've also done this:
     >      >
     >      > |
     >      > # cat tasks/variables.yml
     >      > ---
     >      >
     >      >
     >      > -name:IncludeOS-specific variables (RedHat).
     >      >    include_vars:"{{ ansible_os_family }}-{{ 
ansible_distribution_major_version }}.yml"
     >      > when:ansible_os_family =="RedHat"
     >      >
     >      >
     >      > -name:Definemysql_config_file variable .
     >      >    set_fact:
     >      >      mysql_config_file:"{{ __mysql_config_file }}"
     >      > when:mysql_config_file isnotdefined
     >      >
     >      >
     >      > -debug:
     >      >      msg:"var = {{ ansible_default_ipv4.address }}"
     >      > when:ansible_default_ipv4.address isdefined
     >      >
     >      >
     >      > -name:Definethe mysql_ipv4 variable
     >      >    set_fact:
     >      >      mysql_ipv4:"{{ ansible_default_ipv4.address }}"
     >      > when:mysql_ipv4 isnotdefined
     >      >
     >      >
     >      > -name:Printall variables foreach remote device
     >      >    debug:
     >      > var:hostvars[inventory_hostname]
     >      >
     >      > |
     >      >
     >      > which printed this output (snippet only):
     >      >
     >      > |
     >      > TASK [mysql :Printall variables foreach 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 <http://rd.lvm.lv> <http://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"
     >      > },
     >      >
     >      >
     >      >
     >      > |
     >      >
     >      >
     >      >
     >      >     With this you'll be able to analyze the structure and see if 
the attribute / key is actually set for
    which your
     >      >     playbook
     >      >     or template is looking.
     >      >
     >      >
     >      >     Regards,
     >      >
     >      >              Uwe
     >      >
     >      >
     >      >      >
     >      >      >
     >      >      > There's a variables.yml file as well but that just defines 
mysql_config_file .  Let me know if you
    need to
     >     see it.
     >      >      >
     >      >      > Cheers,
     >      >      > TK
     >      >      >
     >      >      > --
     >      >
     >      > --
     >      > 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+unsubscr...@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+unsubscr...@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 ansible-project+unsubscr...@googlegroups.com <mailto:ansible-project+unsubscr...@googlegroups.com>.
To post to this group, send email to ansible-project@googlegroups.com 
<mailto:ansible-project@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/935752e8-4300-436b-9c0b-f2c1cf1967e2%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/935752e8-4300-436b-9c0b-f2c1cf1967e2%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit 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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/57daef4c-68af-1324-1d10-8127cdbb909d%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to