Set it and forget it 2 – Navigating Ansible Waters

How to do it…

  1. You’ll need to write the playbook first; I have used the demo project for simplicity purposes:

[opc@olam-test /]$ cd /var/lib/awx/
[opc@olam-test awx]$ ls
awxfifo projects rsyslog uwsgi.stats venv
[opc@olam-test awx]$ cd projects/
[opc@olam-test projects]$ cd demo_bundled/
[opc@olam-test demo_bundled]$ ls
httpd.yaml permissions.yaml yum_update.yaml
[opc@olam-test demo_bundled]$

[opc@olam-test demo_bundled]$ sudo vi change_permissions.yaml

  • name: Change file permissions hosts: olam-test-1, olam-test-2 become: true tasks:
    • name: Change permissions for /tmp/changedir
      file:
      path: /tmp/changedir
      owner: root
      group: root
      mode: “0755”

“`
[opc@olam-target-2 tmp]$ ls -ltr
total 0
drwxr-xr-x.
2 root root 6 Oct 18 11:53 unified-monitoring-agent
drwxrwxr-x.
2 opc opc 6 Oct 26 15:27 changedir
[opc@olam-target-2 tmp]$

  1. Each component of the playbook is separated into its own section to simplify the loop topology. The following components are included:
    • name: This is the name of the playbook.
    • hosts: This specifies the target servers where the playbook will be executed. You can define the hosts directly in the playbook or use an inventory file.
    • become: true: This allows the playbook to run with elevated privileges, which might be required to change user permissions.
    • tasks: This section contains the list of tasks to be executed.
    • name: This is a description of the task.
    • file: This module is used to change the permissions of a directory.
    • path: This is the path to the directory where you want to change permissions.
    • owner: This parameter specifies the username that the ownership should be changed to. In this example, we used the item variable, which represents the current user in the loop.
    • state: directory: This ensures that the path is a directory.
  2. OLAM will execute the playbook on the specified servers, changing the user permissions for the specified directory to users user1 and user2 in this example.
  3. Make sure to replace /path/to/directory with the actual path of the directory you want to change permissions for, and update the list of users as per your requirements.
  4. Ensure that you have proper SSH connectivity and the necessary privileges to change permissions on the target servers. The following code snippet is a simple, canned YAML file depicting file permissions:


“`yaml

– name: Change file permissions
  hosts: olam-test-1, olam-test-2
  become: true
  tasks:
    – name: Change permissions for /tmp/changedir
      file:
        path: /tmp/changedir
        owner: root
        group: root
        mode: “0755”

  1. OLAM will run the playbook on the specified servers, changing the permissions of /tmp/changedir to root:root, with a mode of 0755.
  2. Save the playbook file.
  3. Ensure you have proper SSH connectivity to the target servers.
  4. Open the OLAM web-based user interface.
  5. Create a new playbook in OLAM, or open an existing one.
  6. Copy the content of the change_permissions.yaml playbook file into the playbook editor in OLAM.
  7. Save the playbook in OLAM.
  8. Execute the playbook by selecting the target servers (olam-test-1 and olam-test-2) from the inventory, or by manually specifying their IP addresses.
  9. Click on the Run button in OLAM to execute the playbook.
  10. OLAM will run the playbook on the specified servers, changing the permissions of /tmp/changedir to root:root, with port 0755.