User Tools

Site Tools


ansible

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ansible [2026/03/01 12:01] – Added code execution reddyansible [2026/03/01 19:14] (current) reddy
Line 76: Line 76:
  
 ansible -i inventory all -m shell -a 'echo "A"' --limit server1  # To run only on server 1  ansible -i inventory all -m shell -a 'echo "A"' --limit server1  # To run only on server 1 
 +
 +# -f 5  # To run 5 tasks at a time
 </code> </code>
  
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://download.fedoraproject.org/pub/epel/$releasever/$basearch/         baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
         gpgcheck: no         gpgcheck: no
 +  
     - name: Task 2     - name: Task 2
       dnf:       dnf:
         name: httpd         name: httpd
         state: latest  # present|absent|latest         state: latest  # present|absent|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://example.com/some_package.rpm       dnf: name=http://example.com/some_package.rpm
 +  
     - name: Task 4     - name: Task 4
       apt_repository:       apt_repository:
         repo: deb http://archive.canonical.com/ubuntu hardy partner         repo: deb http://archive.canonical.com/ubuntu hardy partner
         state: present         state: present
 +  
     - name: Task 5     - name: Task 5
       apt:       apt:
Line 113: Line 132:
         update_cache: yes         update_cache: yes
         state: present         state: present
 +  
     - name: Task 6     - name: Task 6
       apt:       apt:
         deb: https://example.com/some_package.deb         deb: https://example.com/some_package.deb
 +  
     - 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: "0777"         mode: "0777"
 +  
     - 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  # started|stopped|reloaded|restarted
 +        enabled: no
 +  
     - name: Task 8     - name: Task 8
       get_url:       get_url:
Line 152: Line 189:
         http_proxy: https://example.com:8080         http_proxy: https://example.com:8080
         https_proxy: https://example.com:8080         https_proxy: https://example.com:8080
 +  
     - name: Task 6     - name: Task 6
       local_action: echo "Hello"       local_action: echo "Hello"
 +  
 +    - name: Task 6
 +      script: /tmp/script1
 +  
     - name: Task 6     - name: Task 6
       lineinfile:       lineinfile:
Line 162: Line 204:
                  
 </code> </code>
 +
 +<code>
 +# Tasks to get the output from one command and use as conditional for another command
 +
 +- name: Check ping
 +  delegate_to: srv1
 +  shell: ping -c 1 somehost
 +  register: reachableping
 +  ignore_errors: true
 +
 +- name: Do something
 +  delegate_to: srv1
 +  shell: ./run_cmd_1
 +  ignore_errors: true
 +  when: "'100.0% packet loss' in reachableping.stdout"
 +</code>
 +
 +<code>
 +# Variables can be used for an entire block
 +
 +- name: Demonstrate environment variable in block
 +  hosts: localhost
 +  connection: local
 +  gather_facts: false
 +  vars:
 +    motd: Hello World
 +  tasks:
 +  - block:
 +
 +    - name: Print variable
 +      shell: "echo $motd"
 +      register: theoutput
 +
 +    - debug: var=theoutput.stdout
 +      environment:
 +        motd: "{{ motd }}"
 +</code>
 +
 +===== 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, inventory, and execution results.
 +
 +Install Ansible Navigator with:
 +
 +<code>
 +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
 +</code>
 +
 +Use it with:
 +
 +<code>
 +ansible-navigator run playbook1.yaml
 +
 +ansible-navigator doc service  # Type ":{{ examples }}" to scroll to the example section of the doc
 +
 +ansible-navigator doc service -m stdout  # Getting output to stdout lets you use standard tools to search
 +</code>
 +
 +===== Common Ansible Problems =====
 +
 +<code>
 +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="en_US.UTF-8"
 +LC_CTYPE="en_US.UTF-8"
 +</code>
 +
 +===== Also See =====
 +
 +  * [[http://miro.borodziuk.eu/index.php/2020/03/09/ansible-delegation/|Ansible Delegation]]
 +  * [[http://miro.borodziuk.eu/index.php/2020/02/13/ansible-vault/|Ansible Vault]]
 +  * [[http://miro.borodziuk.eu/index.php/2020/02/12/ansible-galaxy/|Ansible Galaxy]]
 +  * [[http://miro.borodziuk.eu/index.php/2020/03/22/ansible-paralellism/|Ansible Parallelism]]
 +  * [[https://www.redhat.com/sysadmin/faster-ansible-playbook-execution|8 Ways to Speed Up Your Ansible Playbooks]]
 +  * [[https://www.redhat.com/sysadmin/faster-ansible-modules|5 Ways to Make Your Ansible Modules Work Faster]]
  
  
ansible.1772362884.txt.gz · Last modified: by reddy

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki