Skip to Content
Practical Ansible 2
book

Practical Ansible 2

by Daniel Oh, James Freeman, Fabio Alessandro Locati
June 2020
Intermediate to advanced
410 pages
9h 52m
English
Packt Publishing
Content preview from Practical Ansible 2

Creating a Kubernetes Service with Ansible

So far, we have seen how to create namespaces from Ansible, so now, let's put a Service in the namespace we just created. Let's create a new playbook called k8s-svc.yaml with the following content:

---- hosts: localhost  tasks:    - name: Ensure the Service mysvc is present      k8s:        state: present        definition:          apiVersion: v1          kind: Service          metadata:            name: mysvc            namespace: myns          spec:            selector:              app: myapp              service: mysvc            ports:              - protocol: TCP                targetPort: 800                name: port-80-tcp                port: 80

Before running it, we can execute kubectl get svc to ensure that the namespace has no Services. Make sure you're in the right namespace before running it! In my case, the output is as follows:

$ kubectl get svcNo resources found ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Ansible - Third Edition

Mastering Ansible - Third Edition

James Freeman, Jesse Keating
Security Automation with Ansible 2

Security Automation with Ansible 2

Akash Mahajan, MADHU AKULA

Publisher Resources

ISBN: 9781789807462Supplemental Content