Table of Contents

Ansible AD HOC

Módulo shell

# ansible oracle -m shell -a "ping -c2 10.240.100.30"
10.240.100.20 | CHANGED | rc=0 >>
PING 10.240.100.30 (10.240.100.30) 56(84) bytes de dados.
64 bytes de 10.240.100.30: icmp_seq=1 ttl=64 tempo=0.370 ms
64 bytes de 10.240.100.30: icmp_seq=2 ttl=64 tempo=0.665 ms
 
--- 10.240.100.30 estatísticas de ping ---
2 pacotes transmitidos, 2 recebidos, 0% packet loss, time 1043ms
rtt min/avg/max/mdev = 0.370/0.517/0.665/0.147 ms

Módulo user

# echo 'senha' | openssl passwd -1 -stdin
$1$dnQWQmxQ$GAiBhaPEppiuGLDPJvc6Z0

Criando usuário

# ansible local -m user -a 'name=suporte state=present shell=/bin/bash password=$1$dnQWQmxQ$GAiBhaPEppiuGLDPJvc6Z0'
10.240.100.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/suporte",
    "name": "suporte",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
# ansible local -m shell -a "getent passwd | grep suporte"
10.240.100.10 | CHANGED | rc=0 >>
suporte:x:1001:1001::/home/suporte:/bin/bash

Removendo usuário

# ansible local -m user -a 'name=suporte state=absent remove=yes'
10.240.100.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "force": false,
    "name": "suporte",
    "remove": true,
    "state": "absent",
    "stderr": "userdel: suporte mail spool (/var/mail/suporte) not found\n",
    "stderr_lines": [
        "userdel: suporte mail spool (/var/mail/suporte) not found"
    ]
}

Módulo package

Instalando pacotes

# ansible local -m package -a 'name=htop state=present'
10.240.100.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "cache_update_time": 1677786768,
    "cache_updated": false,
    "changed": true,
    "stderr": "debconf: delaying package configuration, since apt-utils is not installed\n",
    "stderr_lines": [
        "debconf: delaying package configuration, since apt-utils is not installed"
    ],
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following additional packages will be installed:\n  libnl-3-200 libnl-genl-3-200\nSuggested packages:\n  lm-sensors lsof strace\nThe following NEW packages will be installed:\n  htop libnl-3-200 libnl-genl-3-200\n0 upgraded, 3 newly installed, 0 to remove and 60 not upgraded.\nNeed to get 200 kB of archives.\nAfter this operation, 589 kB of additional disk space will be used.\nGet:1 http://br.archive.ubuntu.com/ubuntu jammy/main amd64 libnl-3-200 amd64 3.5.0-0.1 [59.1 kB]\nGet:2 http://br.archive.ubuntu.com/ubuntu jammy/main amd64 libnl-genl-3-200 amd64 3.5.0-0.1 [12.4 kB]\nGet:3 http://br.archive.ubuntu.com/ubuntu jammy/main amd64 htop amd64 3.0.5-7build2 [128 kB]\nFetched 200 kB in 0s (449 kB/s)\nSelecting previously unselected package libnl-3-200:amd64.\r\n(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 121219 files and directories currently installed.)\r\nPreparing to unpack .../libnl-3-200_3.5.0-0.1_amd64.deb ...\r\nUnpacking libnl-3-200:amd64 (3.5.0-0.1) ...\r\nSelecting previously unselected package libnl-genl-3-200:amd64.\r\nPreparing to unpack .../libnl-genl-3-200_3.5.0-0.1_amd64.deb ...\r\nUnpacking libnl-genl-3-200:amd64 (3.5.0-0.1) ...\r\nSelecting previously unselected package htop.\r\nPreparing to unpack .../htop_3.0.5-7build2_amd64.deb ...\r\nUnpacking htop (3.0.5-7build2) ...\r\nSetting up libnl-3-200:amd64 (3.5.0-0.1) ...\r\nSetting up libnl-genl-3-200:amd64 (3.5.0-0.1) ...\r\nSetting up htop (3.0.5-7build2) ...\r\nProcessing triggers for libc-bin (2.35-0ubuntu3.1) ...\r\nNEEDRESTART-VER: 3.5\nNEEDRESTART-KCUR: 5.15.0-67-generic\nNEEDRESTART-KEXP: 5.15.0-67-generic\nNEEDRESTART-KSTA: 1\n",
    "stdout_lines": [
        "Reading package lists...",
        "Building dependency tree...",
        "Reading state information...",
        "The following additional packages will be installed:",
        "  libnl-3-200 libnl-genl-3-200",
        "Suggested packages:",
        "  lm-sensors lsof strace",
        "The following NEW packages will be installed:",
        "  htop libnl-3-200 libnl-genl-3-200",
        "0 upgraded, 3 newly installed, 0 to remove and 60 not upgraded.",
        "Need to get 200 kB of archives.",
        "After this operation, 589 kB of additional disk space will be used.",
        "Get:1 http://br.archive.ubuntu.com/ubuntu jammy/main amd64 libnl-3-200 amd64 3.5.0-0.1 [59.1 kB]",
        "Get:2 http://br.archive.ubuntu.com/ubuntu jammy/main amd64 libnl-genl-3-200 amd64 3.5.0-0.1 [12.4 kB]",
        "Get:3 http://br.archive.ubuntu.com/ubuntu jammy/main amd64 htop amd64 3.0.5-7build2 [128 kB]",
        "Fetched 200 kB in 0s (449 kB/s)",
        "Selecting previously unselected package libnl-3-200:amd64.",
        "(Reading database ... ",
        "(Reading database ... 5%",
        "(Reading database ... 10%",
        "(Reading database ... 15%",
        "(Reading database ... 20%",
        "(Reading database ... 25%",
        "(Reading database ... 30%",
        "(Reading database ... 35%",
        "(Reading database ... 40%",
        "(Reading database ... 45%",
        "(Reading database ... 50%",
        "(Reading database ... 55%",
        "(Reading database ... 60%",
        "(Reading database ... 65%",
        "(Reading database ... 70%",
        "(Reading database ... 75%",
        "(Reading database ... 80%",
        "(Reading database ... 85%",
        "(Reading database ... 90%",
        "(Reading database ... 95%",
        "(Reading database ... 100%",
        "(Reading database ... 121219 files and directories currently installed.)",
        "Preparing to unpack .../libnl-3-200_3.5.0-0.1_amd64.deb ...",
        "Unpacking libnl-3-200:amd64 (3.5.0-0.1) ...",
        "Selecting previously unselected package libnl-genl-3-200:amd64.",
        "Preparing to unpack .../libnl-genl-3-200_3.5.0-0.1_amd64.deb ...",
        "Unpacking libnl-genl-3-200:amd64 (3.5.0-0.1) ...",
        "Selecting previously unselected package htop.",
        "Preparing to unpack .../htop_3.0.5-7build2_amd64.deb ...",
        "Unpacking htop (3.0.5-7build2) ...",
        "Setting up libnl-3-200:amd64 (3.5.0-0.1) ...",
        "Setting up libnl-genl-3-200:amd64 (3.5.0-0.1) ...",
        "Setting up htop (3.0.5-7build2) ...",
        "Processing triggers for libc-bin (2.35-0ubuntu3.1) ...",
        "NEEDRESTART-VER: 3.5",
        "NEEDRESTART-KCUR: 5.15.0-67-generic",
        "NEEDRESTART-KEXP: 5.15.0-67-generic",
        "NEEDRESTART-KSTA: 1"
    ]
}
# ansible local -m shell -a 'dpkg -l | egrep htop'
10.240.100.10 | CHANGED | rc=0 >>
ii  htop                                  3.0.5-7build2                           amd64        interactive processes viewer

Removendo pacotes

# ansible local -m package -a 'name=htop state=absent'
10.240.100.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following packages were automatically installed and are no longer required:\n  libnl-3-200 libnl-genl-3-200\nUse 'sudo apt autoremove' to remove them.\nThe following packages will be REMOVED:\n  htop\n0 upgraded, 0 newly installed, 1 to remove and 60 not upgraded.\nAfter this operation, 342 kB disk space will be freed.\n(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 121243 files and directories currently installed.)\r\nRemoving htop (3.0.5-7build2) ...\r\n",
    "stdout_lines": [
        "Reading package lists...",
        "Building dependency tree...",
        "Reading state information...",
        "The following packages were automatically installed and are no longer required:",
        "  libnl-3-200 libnl-genl-3-200",
        "Use 'sudo apt autoremove' to remove them.",
        "The following packages will be REMOVED:",
        "  htop",
        "0 upgraded, 0 newly installed, 1 to remove and 60 not upgraded.",
        "After this operation, 342 kB disk space will be freed.",
        "(Reading database ... ",
        "(Reading database ... 5%",
        "(Reading database ... 10%",
        "(Reading database ... 15%",
        "(Reading database ... 20%",
        "(Reading database ... 25%",
        "(Reading database ... 30%",
        "(Reading database ... 35%",
        "(Reading database ... 40%",
        "(Reading database ... 45%",
        "(Reading database ... 50%",
        "(Reading database ... 55%",
        "(Reading database ... 60%",
        "(Reading database ... 65%",
        "(Reading database ... 70%",
        "(Reading database ... 75%",
        "(Reading database ... 80%",
        "(Reading database ... 85%",
        "(Reading database ... 90%",
        "(Reading database ... 95%",
        "(Reading database ... 100%",
        "(Reading database ... 121243 files and directories currently installed.)",
        "Removing htop (3.0.5-7build2) ..."
    ]
}

Módulo file

Criando arquivos

# ansible local -m file -a 'path=/etc/nologin owner=root group=root mode=0644 state=touch'
10.240.100.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "dest": "/etc/nologin",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}

Removendo arquivos

# ansible local -m file -a 'path=/etc/nologin state=absent'
10.240.100.10 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "path": "/etc/nologin",
    "state": "absent"
}