How do you parameterize ansible_eth* facts and use it?

422 views
Skip to first unread message

t goto

unread,
May 29, 2015, 12:42:18 AM5/29/15
to ansible...@googlegroups.com
Hello, ansible experts.

I'm trying to parameterize eth interface in `group_vars/all` so that I can use it in playbook easier.

group_vars/all file looks like this.

my_int_if: eth1
my_int_ip
: "{{ ansible_eth1.ipv4.address }}"
my_int_obj
: "{{ ansible_eth1 }}"



I could successfuly use those variables with following playbook and template file.

- name: copy mysql file
  tempalte
: src=galera.cnf.j2 dest=/etc/my.cnf.d/galera.cnf


bind-address={{ my_int_ip }}
 
# becomes this as intended.
bind
-address=192.168.50.126



But now, when I tried to use "{{my_int_ip}}" from playbook directly, It didin't work as intended.


- name: galera cluster replication bluh
  mysql_user
:
    name
: wsrep_sst-user
    host
: "{{ hostvars[item].my_int_ip }}"
    password
: password
    priv
: "*.*:ALL"
  with_items
: play_hosts


According to the Error message, it looked like this variable was not expanded well.

msg: (1396, "Operation CREATE USER failed for 'wsrep_sst-user'@'{# ansible_eth1.ipv4.address #}'")


I would like to know why I could use it at template, and not at playbook.

And if you have any good advice to parameterize the ethernet for firewall, bind address, and etc., please show me!!

Thank you!
 

benno joy

unread,
May 29, 2015, 2:03:57 AM5/29/15
to ansible...@googlegroups.com
if i understand correctly, you are setting my_int_if: eth1 and then setting my_int_ip: "{{ ansible_eth1.ipv4.address }}" which is always hardcoded to eth1.

i think what you would want is 
in group_vars/all 

my_int_if: eth1

and reference it in template and other places via:

bind-address={{ hostvars[inventory_hostname]['ansible_' + my_int].ipv4.address }}

and in playbook as

host={{ hostvars[item]['ansible_' + my_int].ipv4.address }}




--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e1878684-f0cd-46b7-b268-34c566a2284f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

t goto

unread,
May 29, 2015, 2:45:17 AM5/29/15
to ansible...@googlegroups.com
Hello, benno joy.

Thanks for understanding my poor English.
Basic concept of my_int_* is to parameterize ethernet interface in template and playbook, 
so that changing group_vars/all can accommodate different OS/machines such as eth1, p3p4, etc.

I tried your code, but still same error message :(




2015年5月29日金曜日 15時03分57秒 UTC+9 benno joy:

benno joy

unread,
May 29, 2015, 2:50:01 AM5/29/15
to ansible...@googlegroups.com
Sorry i had a typo:

if the variable is "my_int_if: eth0"

then in playbook it woudl be referenced as

host={{ hostvars[item]['ansible_' + my_int_if].ipv4.address }}

earlier i had only put "my_int"

if still it doesnt work, can you please paste your group_vars and plybook in gist and let us know the version of ansible you are using.

- Benno


t goto

unread,
May 29, 2015, 3:32:47 AM5/29/15
to ansible...@googlegroups.com
Your code perfectly worked for me.
ThankYOU!! Mr.benno.

The reason it couldn't get through at first was because of mysql FLUSH PRIVILEGES. 
After FLUSH PRIVILEGES in mysql console , everything worked fine with your code :)

Here's practice code I used.

Thank you again! :)
Reply all
Reply to author
Forward
0 new messages