Tag: ansible

  • Ansible multiple hosts behind NAT port forwarding

    Ansible multiple hosts behind NAT port forwarding

    I have 2 servers behind NAT, so they share the same IP address, SSH service runs on two different ports. Here is my ansible inventory file.

    [web]
    195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4000
    195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4001

    When I run the ansible command against the inventory file, it only gets executed on one of the servers.

    ansible hosts behind NAT proxy

    Solution

    The problem is that all the hosts have the same IP address. You need to use a unique name, so Ansible can identify each host separately.

    I updated the inventory file as follows to get it to work.

    [web]
    port4000 ansible_ssh_host=195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4000
    port4001 ansible_ssh_host=195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4001

    port4000, and port4001 can be any unique name that can be used to identify the hosts. If hosts have a unique hostname, you can use it.

    After the change, I can execute commands on both hosts that are behind NAT port forwarding.

    ansible 2 servers with different ports behind nat

    Back to Ansible

  • How to add a PPA repository using Ansible?

    How to add a PPA repository using Ansible?

    To add a PPA repository using Ansible, use

      - name: add ppa
        apt_repository:
          validate_certs: no
          repo: 'ppa:ondrej/php'
          state: present
    

    Example

    ---
      - hosts: web
        user: root
        tasks:
            - name: update system
              apt:
                update_cache: yes
                cache_valid_time: 3600
            - name: add ppa
              apt_repository:
                validate_certs: no
                repo: 'ppa:ondrej/php'
                state: present
            - name: inatall php
              apt:
                pkg:
                - php8.1-bcmath
                - php8.1-cli
                - php8.1-common
                - php8.1-curl
                - php8.1-gd
                - php8.1-imap
                - php8.1-intl
                - php8.1-mbstring
                - php8.1-mysql
                - php8.1-readline
                - php8.1-soap
                - php8.1-xml
                - php8.1-xmlrpc
                - php8.1-zip
                - php8.1-imagick
                - composer
                - ImageMagick
    
  • Install ansible on Ubuntu

    To install ansible on Ubuntu, run

    apt-get -y update
    apt-get -y install software-properties-common
    apt-add-repository ppa:ansible/ansible -y
    apt-get -y update
    apt-get -y install ansible