On Sunday, November 4, 2018 at 2:45:06 AM UTC-5, Uwe Sauter wrote:
>
> 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…) 
>

I did not have this originally.  It was suggested to try it in one of the 
posts I was reading so I tacked it on but it made no difference.  
 

>
> > The above calls this role: 
> > 
> > | 
> > # 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:Copymy.cnf globalMySQLconfiguration. 
> > template: 
> >      src:my.cnf.j2 
> >      dest:"{{ mysql_config_file }}" 
> >      owner:root 
> > group:root 
> >      mode:0644 
> >      tags:mysql 
> > 
>
> This looks normal. 
>
> In situations like these where I'm unsure at which level of nesting a 
> value can be found I use the following neet trick 
> to get a glimpse of the variable structure: 
>
>
> - name: my_play 
>    hosts: my_group 
>    gather_facts: false (to speed up execution. Set to true or omit this 
> line if settings have changed in the last play) 
>    tasks: 
>      - (list of preparing tasks, e.g. set_fact) 
> ### debug #### 
>      - name: dump variable output 
>        copy: 
>          dest: /tmp/<variable_name.yaml> 
>          content: '{{ variable_name | to_nice_yaml }}' 
>      - pause: (or fail:) 
> ### end debug ### 
>      - (rest of the play) 
>


So I've modified the first main.yml as follows:

# 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


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: 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]


which printed this output (snippet only):

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"
        },





 

>
> 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-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/7d79d7d6-ed2f-4693-8182-94ef9bd62c7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to