ansible
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| ansible [2026/03/01 12:01] – Added code execution reddy | ansible [2026/03/01 19:14] (current) – reddy | ||
|---|---|---|---|
| Line 76: | Line 76: | ||
| ansible -i inventory all -m shell -a 'echo " | ansible -i inventory all -m shell -a 'echo " | ||
| + | |||
| + | # -f 5 # To run 5 tasks at a time | ||
| </ | </ | ||
| Line 92: | Line 94: | ||
| hosts: all | hosts: all | ||
| tasks: | tasks: | ||
| + | | ||
| - name: Task 1 | - name: Task 1 | ||
| yum_repository: | yum_repository: | ||
| Line 98: | Line 101: | ||
| baseurl: https:// | baseurl: https:// | ||
| gpgcheck: no | gpgcheck: no | ||
| + | | ||
| - name: Task 2 | - name: Task 2 | ||
| dnf: | dnf: | ||
| name: httpd | name: httpd | ||
| state: latest | state: latest | ||
| + | |||
| + | - name: Task 2 | ||
| + | dnf: | ||
| + | name: | ||
| + | - net-tools | ||
| + | - bind-utils | ||
| + | - telnet | ||
| + | - nmap | ||
| + | - vim-enhanced | ||
| + | - sysstat | ||
| + | - tuned | ||
| + | - numactl | ||
| + | | ||
| - name: Task 3 | - name: Task 3 | ||
| dnf: name=http:// | dnf: name=http:// | ||
| + | | ||
| - name: Task 4 | - name: Task 4 | ||
| apt_repository: | apt_repository: | ||
| repo: deb http:// | repo: deb http:// | ||
| state: present | state: present | ||
| + | | ||
| - name: Task 5 | - name: Task 5 | ||
| apt: | apt: | ||
| Line 113: | Line 132: | ||
| update_cache: | update_cache: | ||
| state: present | state: present | ||
| + | | ||
| - name: Task 6 | - name: Task 6 | ||
| apt: | apt: | ||
| deb: https:// | deb: https:// | ||
| + | | ||
| - name: Task 7 | - name: Task 7 | ||
| copy: | copy: | ||
| Line 121: | Line 142: | ||
| dest: /tmp/file1 | dest: /tmp/file1 | ||
| remote_src: true | remote_src: true | ||
| + | | ||
| - name: Task 8 | - name: Task 8 | ||
| template: | template: | ||
| Line 127: | Line 149: | ||
| vars: | vars: | ||
| var1: 1 | var1: 1 | ||
| + | | ||
| - name: Task 8 | - name: Task 8 | ||
| shell: | | shell: | | ||
| command1 | command1 | ||
| command2 | command2 | ||
| + | | ||
| - name: Task 7 | - name: Task 7 | ||
| file: | file: | ||
| Line 138: | Line 162: | ||
| group: root | group: root | ||
| mode: " | mode: " | ||
| + | | ||
| - name: Task 8 | - name: Task 8 | ||
| sysctl: | sysctl: | ||
| Line 145: | Line 170: | ||
| state: present | state: present | ||
| reload: true | reload: true | ||
| + | | ||
| + | - name: Task 8 | ||
| + | selinux: | ||
| + | policy: targeted | ||
| + | state: permissive | ||
| + | | ||
| + | - name: Task 8 | ||
| + | service: | ||
| + | name: firewalld | ||
| + | state: stopped | ||
| + | enabled: no | ||
| + | | ||
| - name: Task 8 | - name: Task 8 | ||
| get_url: | get_url: | ||
| Line 152: | Line 189: | ||
| http_proxy: https:// | http_proxy: https:// | ||
| https_proxy: | https_proxy: | ||
| + | | ||
| - name: Task 6 | - name: Task 6 | ||
| local_action: | local_action: | ||
| + | | ||
| + | - name: Task 6 | ||
| + | script: / | ||
| + | | ||
| - name: Task 6 | - name: Task 6 | ||
| lineinfile: | lineinfile: | ||
| Line 162: | Line 204: | ||
| | | ||
| </ | </ | ||
| + | |||
| + | < | ||
| + | # Tasks to get the output from one command and use as conditional for another command | ||
| + | |||
| + | - name: Check ping | ||
| + | delegate_to: | ||
| + | shell: ping -c 1 somehost | ||
| + | register: reachableping | ||
| + | ignore_errors: | ||
| + | |||
| + | - name: Do something | ||
| + | delegate_to: | ||
| + | shell: ./run_cmd_1 | ||
| + | ignore_errors: | ||
| + | when: "' | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | # Variables can be used for an entire block | ||
| + | |||
| + | - name: Demonstrate environment variable in block | ||
| + | hosts: localhost | ||
| + | connection: local | ||
| + | gather_facts: | ||
| + | vars: | ||
| + | motd: Hello World | ||
| + | tasks: | ||
| + | - block: | ||
| + | |||
| + | - name: Print variable | ||
| + | shell: "echo $motd" | ||
| + | register: theoutput | ||
| + | |||
| + | - debug: var=theoutput.stdout | ||
| + | environment: | ||
| + | motd: "{{ motd }}" | ||
| + | </ | ||
| + | |||
| + | ===== Ansible Navigator ===== | ||
| + | |||
| + | Ansible Navigator uses podman to create an EE environment to run the Ansible playbooks and provides a text user interface to navigate through the documentation, | ||
| + | |||
| + | Install Ansible Navigator with: | ||
| + | |||
| + | < | ||
| + | pip3 install ansible-dev-tools ansible-navigator --user | ||
| + | |||
| + | # or | ||
| + | |||
| + | dnf install \ | ||
| + | --enablerepo=ansible-automation-platform-2.2-for-rhel-8-x86_64-rpms \ | ||
| + | ansible-navigator | ||
| + | </ | ||
| + | |||
| + | Use it with: | ||
| + | |||
| + | < | ||
| + | ansible-navigator run playbook1.yaml | ||
| + | |||
| + | ansible-navigator doc service | ||
| + | |||
| + | ansible-navigator doc service -m stdout | ||
| + | </ | ||
| + | |||
| + | ===== Common Ansible Problems ===== | ||
| + | |||
| + | < | ||
| + | ERROR: Ansible requires the locale encoding to be UTF-8; Detected ISO8859-1. | ||
| + | |||
| + | Set the following environment variables within the shell before running ansible / ansible-playbook | ||
| + | |||
| + | LANG=" | ||
| + | LC_CTYPE=" | ||
| + | </ | ||
| + | |||
| + | ===== Also See ===== | ||
| + | |||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
ansible.1772362884.txt.gz · Last modified: by reddy
