How Do You Get Ansible Playbooks to Create Directories on Managed Nodes?

Problem scenario
You want Ansible to create directories on managed nodes.  How do you write a playbook to do this?

Solution
Prerequisites

This assumes that you have installed and configured Ansible.  If you do not know how to deploy Ansible, see this posting if you are using a Red Hat derivative.  If you are using Linux SUSE, see this posting.  If you need help running a playbook, see this posting.

Procedures
In the playbook (the .yaml file) for the section that creates the subdirectory, add these two stanzas where "cooluser" is a username with sudoer privileges on the managed node:

  remote_user: cooluser
  become: yes

Here is a complete playbook that creates a directory called "contint" in the /etc/ directory:

- name: something
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
  - file:
      path: /etc/contint
      state: directory
      owner: cooluser
      mode: 0744

Leave a comment

Your email address will not be published.