need help in checking if a variable string is in an item string

60 views
Skip to first unread message

Jerome Yanga

unread,
Jun 13, 2016, 1:50:10 PM6/13/16
to Ansible Project
Here is what I have.

vars/main.yml
host_entries:
 - { srv_loc:  'ca', ip_addr:  '10.1.1.1', host_aliases:  'ca-srv-01.example.com ca-srv-01' }
 - { srv_loc:  'ny', ip_addr:  '10.2.1.1', host_aliases:  'ny-srv-01.example.com ny-srv-01' }
 - { srv_loc:  'co', ip_addr:  '10.3.1.1', host_aliases:  'co-srv-01.example.com co-srv-01' }

tasks/main.yml
---

 - name: checking location of server
   raw:  'echo "{{ ansible_hostname }}" | cut -f1 -d"-"'
   register: actual_loc

 - name: adding entries in the hosts file
   lineinfile:
      backup:  yes
      create:  yes
      dest:  /etc/hosts
      group:  root
      line:  "{{item.ip_addr}}     {{item.host_aliases}}"
      mode:  0644
      owner:  root
      state:  present
   when:  ( actual_loc.stdout_lines == item.srv_loc )
   with_items:  "{{ hosts_entries }}"

I am trying to add the correct hosts entries based on the substring of a hostname.  All items are being skipped at the moment due to '"skip_reason": "Conditional check failed", "skipped": true'.  

Please help.  :)

Regards,
j
Message has been deleted

Jerome Yanga

unread,
Jun 14, 2016, 10:26:23 AM6/14/16
to Ansible Project
I apologize for not providing the info below.

version:  2.0.2.0

event:
skipping: [ca-server1] => (item={u'host_aliases': u'ca-srv-01.example.com ca-srv-01', u'ip_addr': u'10.1.1.1', u'srv_loc': u'ca'})  => {"changed": false, "item": {"host_aliases": "ca-srv-01.example.com ca-srv-01", "ip_addr": "10.1.1.1", "srv_loc": "ca"}, "skip_reason": "Conditional check failed", "skipped": true}

Regards,
j

Kai Stian Olstad

unread,
Jun 14, 2016, 11:45:30 AM6/14/16
to ansible...@googlegroups.com
On 13. juni 2016 19:50, Jerome Yanga wrote:
> vars/main.yml
> host_entries:

Here you have host_entries


> - { srv_loc: 'ca', ip_addr: '10.1.1.1', host_aliases:
> 'ca-srv-01.example.com ca-srv-01' }
> - { srv_loc: 'ny', ip_addr: '10.2.1.1', host_aliases:
> 'ny-srv-01.example.com ny-srv-01' }
> - { srv_loc: 'co', ip_addr: '10.3.1.1', host_aliases:
> 'co-srv-01.example.com co-srv-01' }
>
> tasks/main.yml
> ---
>
> - name: checking location of server
> raw: 'echo "{{ ansible_hostname }}" | cut -f1 -d"-"'
> register: actual_loc
>
> - name: adding entries in the hosts file
> lineinfile:
> backup: yes
> create: yes
> dest: /etc/hosts
> group: root
> line: "{{item.ip_addr}} {{item.host_aliases}}"
> mode: 0644
> owner: root
> state: present
> when: ( actual_loc.stdout_lines == item.srv_loc )
> with_items: "{{ hosts_entries }}"

But here you have hosts_entries, so one of them is a typo.

You could lose the raw by doing so in the when
when: ansible_hostname.split('-')[0] == item.srv_loc

--
Kai Stian Olstad

Jerome Yanga

unread,
Jun 14, 2016, 1:53:45 PM6/14/16
to Ansible Project, ansible-pr...@olstad.com
Thanks, Kai.

You hit the nail on the head.  If there is a doc on how to do this string manipulation, please share with me the URL.

Regards,
j

Kai Stian Olstad

unread,
Jun 14, 2016, 4:10:05 PM6/14/16
to ansible...@googlegroups.com
On 14. juni 2016 19:53, Jerome Yanga wrote:
> Thanks, Kai.
>
> You hit the nail on the head. If there is a doc on how to do this string
> manipulation, please share with me the URL.

I have just picked it up, but I guess it's a Python string method
https://docs.python.org/2.7/library/stdtypes.html?highlight=split#str.split

Maybe all the Python string methods work, hopefully someone else can
shed some light on that.


--
Kai Stian Olstad

Jerome Yanga

unread,
Jun 14, 2016, 6:12:11 PM6/14/16
to Ansible Project, ansible-pr...@olstad.com
Kai,

Thanks.  You have been very helpful.

Regards,
j
Reply all
Reply to author
Forward
0 new messages