Create VM on VMWare and register on RHN

54 views
Skip to first unread message

Enzo

unread,
Aug 19, 2018, 9:24:16 AM8/19/18
to Ansible Project
Hi,

I'm a beginner on awx and ansible.
I have a template with RHEL7 on vmware and I trying to clone a vm and register on rhn.
My playbook create vm without issue and take all parameters from survey on awx, but I have a doubt how to register on RHN?
In my template there is already linux user ansible with ssh rsa public key in authorized_hosts, so my ansible can access without problems.
How to execute the task on new created host and not localhost?

---
- name: Create a VM from a template
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  - name: Clone the template
    vmware_guest:
      hostname: '{{ vcenter_server }}'
      username: '{{ vcenter_user }}'
      password: '{{ vcenter_password }}'
      validate_certs: False
      name: '{{
      template: Template_RHEL7
      datacenter: '{{ vcenter_datacenter }}'
      folder: /
      state: poweredon
      wait_for_ip_address: yes
  - name: Register RHN
    rhn_register:
       state: present
       username: joe_user
       password: somepass

Thank you for your suggestions.
Enzo

Jonathan Lozada De La Matta

unread,
Aug 19, 2018, 11:49:23 AM8/19/18
to ansible...@googlegroups.com
you need to put add_host to it adds the vm created to the current inventory then do the RHN part. This blog has a good explanation 

--
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/7f58df65-1586-4745-9942-5caf40d6686e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Jonathan lozada de la matta

AUTOMATION CONSULTANT - AUTOMATION PRACTICE

Red Hat Consulting Services

jloz...@redhat.com   



 

Enzo

unread,
Aug 20, 2018, 8:38:09 AM8/20/18
to Ansible Project
Thank you Jonathan for your suggestion.

Now I have this playbook:

---
- name: Create Linux VM
  hosts: localhost
  connection: local
  gather_facts: True

  tasks:
  - name: Install Linux server

    vmware_guest:
      hostname: '{{ vcenter_server }}'
      username: '{{ vcenter_user }}'
      password: '{{ vcenter_password }}'
      validate_certs: no
      datacenter: '{{ vcenter_datacenter }}'
      cluster: '{{ vcenter_cluster }}'
      name: '{{ vcenter_guest }}'
      template: Template_RHEL7
      state: poweredon
      folder: '/'
      disk:
      - size_gb: 35
        type: thin
        datastore: ds
        autoselect_datastore: yes
      networks:
      - name: '{{ network_connection }}'
        ip: '{{ ip_address }}'
        netmask: '{{ ip_netmask }}'
        gateway: '{{ ip_gateway }}'
        start_connected: yes
      customization:
        domain: '{{ dns_domain }}'
        dns_servers:
        - 192.168.0.100
      wait_for_ip_address: yes
    delegate_to: 127.0.0.1
    run_once: True

  - name: Add host in group
    add_host:
      hostname: '{{ vcenter_guest }}'
      groupname: createvm_temp
    with_items: '{{ vcenter_guest }}'

#  - name: Wait for ssh to come up
#    wait_for: host={{ ip_address }} port=22 delay=10 timeout=120
#    with_items: '{{ vcenter_guest }}'

  - name: Setting ip as instance fact
    set_fact: host={{ ip_address }}
    with_items: '{{ vcenter_guest }}'

  - name: Register to RHN and auto-subscribe to available content.
    hosts: createvm_temp
    gather_facts: True
    redhat_subscription:
      state: present
      username: user
      password: password
      auto_attach: true
    with_items: '{{ vcenter_guest }}'

I'm running with ansible version 2.6.2, I got 2 warnings

[WARNING]: Ignoring invalid attribute: gather_facts
[WARNING]: Ignoring invalid attribute: hosts

and an error

TASK [Register to RHN and auto-subscribe to available content.] **************** 11:39:47
failed: [localhost] (item=ansibletest) => {"changed": false, "item": "ansibletest", "msg": "Failed to find required executable subscription-manager in paths: /opt/awx/embedded/bin:/opt/awx/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin"}                    

Ansible is running on centos 7 where subscription-manager is not installed, while on target ansibletest RHEL7 is installed but ansible tell me no.
I think the issue is because the task is executed on localhost and not on new host so I can't change the host when run all other tasks.
Other suggestions?

Thank you
Enzo

Jonathan Lozada De La Matta

unread,
Aug 20, 2018, 9:06:37 AM8/20/18
to ansible...@googlegroups.com

--
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.

For more options, visit https://groups.google.com/d/optout.

Enzo

unread,
Aug 20, 2018, 10:18:58 AM8/20/18
to Ansible Project
I solved so:

---
- name: Create Linux VM
  hosts: localhost
  connection: local
  gather_facts: no
        - 192.168.1.100

      wait_for_ip_address: yes
    delegate_to: 127.0.0.1
    run_once: True

  - name: Add host in group
    add_host:
      hostname: '{{ vcenter_guest }}'
      groups: createvm_temp
      ansible_host: '{{ ip_address }}'
    with_items: '{{ vcenter_guest }}'

- name: Post create vm
  hosts: createvm_temp
  gather_facts: no

  tasks:

    - name: Register to RHN and auto-subscribe to available content.
      become: yes

      redhat_subscription:
        state: present
        username: user
        password: pass
        auto_attach: true

I hope is usefully also to others.
Reply all
Reply to author
Forward
0 new messages