Unlocking the Full Potential of Infrastructure as Code (IaC) with Ansible

In today’s fast-paced world of software development and IT operations, the need for efficient and scalable infrastructure management has become more critical than ever. Enter Infrastructure as Code (IaC), a game-changing approach that allows organizations to automate their infrastructure provisioning and management, making it more agile, scalable, and reliable. And when it comes to IaC, Ansible stands out as a top choice for many IT teams due to its simplicity, versatility, and robustness....

May 18, 2023 · 6 min · 1158 words · Andrew

How to Copy Files between Two Nodes using Ansible

If you need to copy files between two (2) nodes, using Ansible, then you can do the following: This solution uses the synchronize module, specifically using the delegate_to:source-server keywords. - hosts: serverB tasks: - name: Copy Remote-To-Remote (from serverA to serverB) synchronize: src=/copy/from_serverA dest=/copy/to_serverB delegate_to: serverA

October 22, 2022 · 1 min · 46 words · Andrew

How to Delete Files and Folders in a Directory using Ansible

If you need to delete files and folders in a directory using Ansible, then you can do the following: - name: Delete content & entire directory file: state: absent path: /some/directory/path/

October 21, 2022 · 1 min · 31 words · Andrew

How to Copy Multiple Files with Ansible

If you need to copy multiple files using Ansible, then you can do the following: How to Copy Multiple Files with Ansible Look into using the with_fileglob loop as follows: - copy: src: "{{ item }}" dest: /etc/fooapp/ owner: root mode: 600 with_fileglob: - /playbooks/files/fooapp/* If you would like to do it as a task, then this could help: - name: Your copy task copy: src={{ item.src }} dest={{ item.dest }} with_items: - { src: 'another_file', dest: '/etc/somewhere' } - { src: 'dynamic', dest: '{{ var_path }}' } # repeat for additional files

October 20, 2022 · 1 min · 93 words · Andrew

How to Write Multiline Shell Scripts in Ansible

If you need to write a shell script in Ansible, you probably have something like this: - name: iterate user groups shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} with_items: "{{ users }}" But how do you write multiline shell scripts with this format? How to write Multiline shell scripts - name: iterate user groups shell: | groupmod -o -g {{ item['guid'] }} {{ item['username'] }} do_some_stuff_here and_some_other_stuff with_items: "{{ users }}" Just note that Ansible can do some strange things with manipulations of arguments, so you may want to follow something like this:...

October 19, 2022 · 1 min · 122 words · Andrew

How to Pass Variables to Ansible Playbook CLI

If you need to pass a variable to Ansible playbook, using the command line, then you can do the following: Option 1 – Specifying command line arguments ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo" N.B. --extra-vars specified variables will override any variables with the same name defined inside the playbook. You can also read up on Passing Variables On The Command Line (Wayback Machine link to maintain versioning) Option 2 – Specify a YML file You can also specify a ....

October 18, 2022 · 1 min · 111 words · Andrew

How to Create a Directory using Ansible

If you need to create a directory using Ansible, then you can do the following: Create a Directory in Ansible You will need the file module, then to create a directory you simply specify the option state=directory: - name: Creates a directory file: path: /src/www state: directory Note that with state=directory, all the immediate subdirectories will be created if they don’t already exist. Extending the file module - name: Creates a directory file: path: /src/www state: directory owner: www-data group: www-data mode: 0775 Create the Directories Recursively - name: Creates directory file: path: /src/www state: directory owner: www-data group: www-data mode: 0775 recurse: yes This is similar to the recursive argument used with mkdir -p

October 17, 2022 · 1 min · 115 words · Andrew