LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-26-2017, 06:45 PM   #1
Vanyel
Member
 
Registered: Jul 2007
Location: NY, NY
Distribution: RHEL, CentOS, MacOS X
Posts: 158

Rep: Reputation: 29
Question Ansible playbooks - proper syntax with facts in conditional executions


I've been learning Ansible and am already using it for a lot of basic deployment uses, but I'm having problems with using ansible_facts for conditional statements.

Example, a statement like this is easy-
Code:
   
 - name: Installs epel repo via yum. ONLY on CentOS machines.
      yum: pkg=epel-release state=installed
      when: ansible_distribution=='CentOS'
because the ansible_distribution fact is pretty straightforward
Quote:
[root@server]# ansible <machine> -m setup -a 'filter=ansible_distribution'
<machine> | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS"
},
"changed": false
}
Other facts have much more information though, specifically I'm looking for network info-
Quote:
[root@server]# ansible <machine> -m setup -a 'filter=ansible_default_ipv4'
<machine> | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.1.232",
"alias": "p128p1",
"broadcast": "192.168.1.255",
"gateway": "192.168.1.1",
"interface": "p128p1",
"macaddress": "f0:4d:a2:dc:f1:38",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.1.0",
"type": "ether"
}
},
"changed": false
}
The fact variable ansible_default_ipv4 has a LOT of sub information but I'm only interested in the "network" portion for my playbook.
I can't figure out the right syntax to use this though. A construction of
Code:
   
 - name: Installs epel repo via yum. ONLY on internal network
      yum: pkg=epel-release state=installed
      when: ansible_default_ipv4:network=='192.168.1.0'
didn't work. What's the proper syntax to use the network property of the ansible_default_ipv4 fact?
 
Old 10-27-2017, 03:15 PM   #2
Vanyel
Member
 
Registered: Jul 2007
Location: NY, NY
Distribution: RHEL, CentOS, MacOS X
Posts: 158

Original Poster
Rep: Reputation: 29
I stumbled on an example of what the correct syntax should be to access a sub fact of a fact variable. The when statement in my above case should be:

Quote:
when: ansible_default_ipv4.network=='192.168.1.0'
 
  


Reply

Tags
ansible



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Proper syntax for ACLOCAL_FLAGS variable Kenny_Strawn Programming 7 10-15-2017 01:24 PM
[SOLVED] proper syntax for a qemu command jr_bob_dobbs Slackware 5 02-26-2016 09:22 PM
Vagrant+Ansible playbooks. How to use same playbook on non vagrant provision markotitel Linux - Virtualization and Cloud 1 12-06-2013 09:00 AM
conditional syntax problem in kernel makefile heliks Linux - Kernel 0 11-03-2009 02:29 AM
proper syntax for 'more' command bourne Linux - General 6 09-14-2007 09:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration