name: Display Suricata version
ansible.builtin.debug:
msg: "Suricata version installed: {{ suricata_version_info.stdout }}"
I want to make sure that sudo is installed, before gathering facts, so I'm using pre_tasks.
Assistant
However, when I run this playbook with:
ansible-playbook -i '192.168.1.105:22', /var/www/html/suricatavel/releases/20240913191129/support/ansible/debian.yml --extra-vars 'ansible_ssh_pass=filldeputA90' -vvvÇ
I still see the gathering facts run at first, and thus failing with "sudo: not found"
You’re correct that Ansible’s fact gathering occurs before any pre_tasks or tasks are executed. Since your playbook specifies become: yes at the play level, Ansible attempts to escalate privileges even during fact gathering. If sudo is not installed initially, this results in the error you’re encountering: sudo: not found.
To resolve this, you need to ensure that sudo is available before Ansible attempts to gather facts with elevated privileges. Here’s how you can achieve this:
Disable Automatic Fact Gathering: Prevent Ansible from gathering facts automatically at the start of the playbook.
Install sudo in pre_tasks: Ensure sudo is installed without requiring privilege escalation.
Manually Gather Facts After Installing sudo: Once sudo is installed, manually invoke the setup module to gather facts with the necessary privileges.
Enable Privilege Escalation for Subsequent Tasks: Apply become: yes only after sudo is available.
Modified Playbook
Here’s how you can modify your playbook to implement the above steps:
yaml
---
- name: Install Suricata 7 from source on Debian
hosts: all
become: no # Disable become at the play level initially
gather_facts: no # Disable automatic fact gathering
vars:
suricata_version: "7.0.6" # Specify the Suricata version to install
pre_tasks:
- name: Ensure sudo is installed
apt:
name: sudo
state: present
# Run without becoming since sudo is not yet required
- name: Gather facts after installing sudo
setup:
become: yes # Now that sudo is installed, gather facts with privilege escalation
tasks:
- name: Update apt cache
apt:
update_cache: yes
become: yes
- name: Install required build dependencies
apt:
name:
- build-essential
- libpcre2-dev
- libpcap-dev
- libyaml-dev
- zlib1g-dev
- libcap-ng-dev
- libmagic-dev
- libjansson-dev
- libnss3-dev
- libgeoip-dev
- liblua5.3-dev
- libhiredis-dev
- libmaxminddb-dev
- rustc
- cargo
- libnetfilter-queue-dev
- libnetfilter-log-dev
- libnfnetlink-dev
- wget
- tar
- libevent-dev
- libmnl-dev
state: present
become: yes
- name: Download Suricata source code
get_url:
url: "https://www.openinfosecfoundation.org/download/suricata-{{ suricata_version }}.tar.gz"
dest: "/tmp/suricata-{{ suricata_version }}.tar.gz"
become: yes
- name: Extract Suricata source code
unarchive:
src: "/tmp/suricata-{{ suricata_version }}.tar.gz"
dest: "/tmp/"
remote_src: yes
become: yes
- name: Compile and install Suricata
shell: |
cd /tmp/suricata-{{ suricata_version }}
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-nfqueue --enable-geoip
make
make install-full
ldconfig
become: yes
- name: Create Suricata log directory
file:
path: /var/log/suricata
state: directory
owner: root
group: root
mode: '0755'
become: yes
- name: Copy Suricata YAML config file
copy:
src: ../suricata/suricata.yaml
dest: /etc/suricata/suricata.yaml
owner: root
group: root
mode: '0644'
become: yes
- name: Copy Suricata systemd service file
copy:
src: ../suricata/suricatavel.service
dest: /etc/systemd/system/suricata.service
owner: root
group: root
mode: '0644'
become: yes
- name: Reload systemd daemon to recognize Suricata service
systemd:
daemon_reload: yes
become: yes
- name: Enable and start Suricata service
systemd:
name: suricata
enabled: yes
state: started
become: yes
- name: Verify Suricata installation
command: suricata --build-info
register: suricata_version_info
become: yes
- name: Display Suricata version
debug:
msg: "Suricata version installed: {{ suricata_version_info.stdout }}"
Explanation of Changes
Disable Automatic Fact Gathering:
yaml
gather_facts: no
This prevents Ansible from attempting to gather facts before sudo is installed.
Remove become: yes at the Play Level:
yaml
become: no
This ensures that privilege escalation is not attempted during fact gathering.
Installing sudo doesn't require privilege escalation because it's a system package that the current user should have permissions to install if already running as root. If not running as root, you might need to adjust this.
Manually Gather Facts with Privilege Escalation:
yaml
- name: Gather facts after installing sudo
setup:
become: yes
After ensuring sudo is present, you manually invoke the setup module to gather facts with become: yes.
Enable Privilege Escalation for Subsequent Tasks:
For all tasks that require elevated privileges, explicitly specify become: yes. This approach avoids applying become: yes globally, which could interfere with the initial fact gathering.
Additional Considerations
SSH User Permissions: Ensure that the SSH user you’re connecting with (filldeputA90 in your command) has the necessary permissions to install packages and perform other administrative tasks, either by being root or having passwordless sudo access after installation.
Error Handling: Consider adding error handling or checks to verify each step succeeds before proceeding, enhancing the playbook's robustness.
Clean Up: Optionally, add tasks to clean up the /tmp directory after installation to keep the system tidy.
Running the Playbook
With the modified playbook, run it as you did before:
This should successfully install sudo first, gather facts with the necessary privileges, and then proceed with the rest of the installation steps without encountering the sudo: not found error.