One place for hosting & domains

      System

      How To Access System Information (Facts) in Ansible Playbooks



      Part of the Series:
      How To Write Ansible Playbooks

      Ansible is a modern configuration management tool that doesn’t require the use of an agent software on remote nodes, using only SSH and Python to communicate and execute commands on managed servers. This series will walk you through the main Ansible features that you can use to write playbooks for server automation. At the end, we’ll see a practical example of how to create a playbook to automate setting up a remote Nginx web server and deploy a static HTML website to it.

      By default, before executing the set of tasks defined in a playbook, Ansible will take a few moments to gather information about the systems that are being provisioned. This information, referred to as facts, contain details such as network interfaces and addresses, the operating system running on remote nodes, and available memory, among other things.

      Ansible stores facts in JSON format, with items grouped in nodes. To check what kind of information is available for the systems you’re provisioning, you can run the setup module with an ad hoc command:

      • ansible all -i inventory -m setup -u sammy

      This command will output an extensive JSON containing information about your server. To obtain a subset of that data, you can use the filter parameter and provide a pattern. For instance, if you’d like to obtain information about all IPv4 addresses in the remote nodes, you can use the following command:

      • ansible all -i inventory -m setup -a "filter=*ipv4*" -u sammy

      You’ll see output like this:

      Output

      203.0.113.10 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "203.0.113.10", "198.51.100.23" ], "ansible_default_ipv4": { "address": "203.0.113.10", "alias": "eth0", "broadcast": "203.0.113.255", "gateway": "203.0.113.1", "interface": "eth0", "macaddress": "06:c7:91:16:2e:b7", "mtu": 1500, "netmask": "203.0.113.0", "network": "203.0.113.0", "type": "ether" } }, "changed": false }

      Once you have found the facts that will be useful for your play, you can update your playbook accordingly. As an example, the following playbook will print out the IPv4 address of the default network interface. From the previous command output, we can see that this value is available through ansible_default_ipv4.address in the JSON provided by Ansible.

      Create a new file called playbook-03.yml in your ansible-practice directory:

      • nano ~/ansible-practice/playbook-03.yml

      Then add the following lines to the new playbook file:

      ~/ansible-practice/playbook-03.yml

      ---
      - hosts: all
        tasks:
          - name: print facts
            debug:
              msg: "IPv4 address: {{ ansible_default_ipv4.address }}"
      

      Save and close the file when you’re done.

      To try this playbook on servers from your inventory file, run ansible-playbook with the same connection arguments you’ve used before when running our first example. Again, we’ll be using an inventory file named inventory and the sammy user to connect to the remote servers:

      • ansible-playbook -i inventory playbook-03.yml -u sammy

      When you run the playbook, you’ll see your remote server’s IPv4 address in the output as expected:

      Output

      ... TASK [print facts] *************************************************************************************************************************************************************************** ok: [server1] => { "msg": "IPv4 address: 203.0.113.10" } ...

      Facts encapsulate important data that you can leverage to better customize your playbooks. To learn more about all the information you can obtain through facts, please refer to the official Ansible documentation.



      Source link

      How To Install and Manage System Packages in Ansible Playbooks



      Part of the Series:
      How To Write Ansible Playbooks

      Ansible is a modern configuration management tool that doesn’t require the use of an agent software on remote nodes, using only SSH and Python to communicate and execute commands on managed servers. This series will walk you through the main Ansible features that you can use to write playbooks for server automation. At the end, we’ll see a practical example of how to create a playbook to automate setting up a remote Nginx web server and deploy a static HTML website to it.

      Automating the installation of required system packages is a common operational task in Ansible playbooks, since a typical application stack requires software from different sources.

      The apt module manages system packages on Debian-based operating systems such as Ubuntu, the distribution we’re using on remote nodes throughout this guide. The following playbook will update the apt cache and then make sure Vim is installed on remote nodes.

      Create a new file called playbook-09.yml in your ansible-practice directory:

      • nano ~/ansible-practice/playbook-09.yml

      Then add the following lines to the new playbook file:

      ~/ansible-practice/playbook-09.yml

      ---
      - hosts: all
        become: yes
        tasks:
          - name: Update apt cache and make sure Vim is installed
            apt:
              name: vim
              update_cache: yes
      

      Save and close the file when you’re done.

      Notice that we’ve included the become directive in the beginning of the play. This is required since installing packages requires administrative system permissions.

      Removing a package is done in a similar way, the only change is that you have to define the package state to absent. The state directive has a default value of present, which will make sure that the package is installed on the system, regardless of the version. The package will be installed if not present. To assure you have the latest version of a package, you can use latest instead. This will cause apt to update the requested package if that is not on their latest version.

      Remember to provide the -K option when running this playbook, since it requires sudo permissions:

      • ansible-playbook -i inventory playbook-09.yml -u sammy -K

      Output

      BECOME password: PLAY [all] ********************************************************************************************** TASK [Gathering Facts] ********************************************************************************** ok: [203.0.113.10] TASK [Update apt cache and make sure Vim is installed] ************************************************** ok: [203.0.113.10] PLAY RECAP ********************************************************************************************** 203.0.113.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

      When installing multiple packages, you can use a loop and provide an array containing the names of the packages you want to install. The following playbook will make sure the packages vim, unzip, and curl are installed and in their latest version.

      Create a new file called playbook-10.yml in your ansible-practice directory, on your Ansible control node:

      • nano ~/ansible-practice/playbook-10.yml

      Add the following content to the new playbook file:

      ~/ansible-practice/playbook-10.yml

      ---
      - hosts: all
        become: yes
        tasks:
          - name: Update apt cache and make sure Vim, Curl and Unzip are installed
            apt:
              name: "{{ item }}"
              update_cache: yes
            loop:
              - vim
              - curl
              - unzip
      

      Save and close the file when you have finished.

      Then, run ansible-playbook with the same connection arguments from the previous examples, and don’t forget to include the -K option since this playbook requires administrative privileges:

      • ansible-playbook -i inventory playbook-09.yml -u sammy -K

      You’ll see output like this, indicating that the same task run through three iterations using the different values we have provided: vim, curl, and unzip:

      Output

      BECOME password: PLAY [all] *************************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************************** ok: [203.0.113.10] TASK [Update apt cache and make sure Vim, Curl and Unzip are installed] ************************************************************************** ok: [203.0.113.10] => (item=vim) ok: [203.0.113.10] => (item=curl) changed: [203.0.113.10] => (item=unzip) PLAY RECAP *************************************************************************************************************************************** 203.0.113.10 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

      For more details on how to manage system packages, including how to remove packages and how to use advanced apt options, you can refer to the official documentation.



      Source link

      What is an Operating System?


      An operating system is system software that handles computer memory, software and hardware resources, and processes, while providing common services and programming interfaces for computer programs. Examples of popular operating systems include WindowsOS, MacOS, and Linux.

      Aside from firmware, all computer programs require an operating system to function, and they are typically preloaded on new computers. Each operating system has a different user interface, or UI, that allows users to interact with their computer.

      To learn more, here’s some operating system specific resources, including a comprehensive list of resources for Windows, FreeBSD, and Linux.



      Source link