Kubectl custom.yml (write_file) using ansible

Hello,

I’m trying to use “k8s” module in ansible to create a pod/container with custom file.

For example. “/etc/httpd/conf.d/httpd.conf” should have line “VirtualHost 1.2.3.4” at the end.

Instead of copy/pasting file, is there a way to append >> at the end?
I tried below but not working:

 - name: k8s citrix pod
    k8s:
      state: present
      definition:
        apiVersion: v1
        kind: Pod
        metadata:
          name: cloud-init-httpd3
          namespace: kuber
          labels:
            app: nginxApp
            service: nginxService
        spec:
          containers:
          - name: nginxcontainer-ntp
            image: nginx
          command: bash -c 'echo "hello" >> /etc/test'
          dnsConfig:
            nameservers:
              - 172.19.54.2
              - 172.9.54.3
            search:
              - mydev.com
          write_files:
          - path: /etc/httpd/conf.d/httpd.conf
            permissions: 0644
            owner: root
            content: |
              VirtualHost mydev.com

              # Allow only time queries, at a limited rate, sending KoD when in excess.
              # # Allow all local queries (IPv4, IPv6)
              restrict default nomodify nopeer noquery limited kod
              restrict 127.0.0.1
              restrict [::1]
      kubeconfig: ~/.kube/config
    delegate_to: localhost
    register: kubefacts

  - debug:
      var: kubefacts

Thanks.

1 Like

Googling “ansible append file” (since that is the generic question you’re asking – not really related to kubectl’s config file) gives great results, such as:

https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html

I myself prefer my tooling to be as predictable as possible and reduce the number of possible mistakes. I therefore find it much more dependable to push a file (with variable values in place via templates) to be much easier to reason about. To each their own, I suppose.

Thanks for reply. Is there a kubectl equivalent? Or is it simply “write_file” Thanks