Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Last active May 15, 2020 13:44
Show Gist options
  • Save diegopacheco/3af46c13c5a709bf642d to your computer and use it in GitHub Desktop.
Save diegopacheco/3af46c13c5a709bf642d to your computer and use it in GitHub Desktop.
How to Install Ansible on Amazon Linux / CentOS?
sudo su
yum update
yum install -y git
cd /usr/local/src
yum -y install git python-jinja2 python-paramiko PyYAML make MySQL-python
git clone git://github.com/ansible/ansible.git
cd ansible
git submodule update --init --recursive
make install
CTRL + D
sudo echo "[localhost]" > ~/ansible_hosts
sudo echo "localhost ansible_connection=local" >> ~/ansible_hosts
export ANSIBLE_INVENTORY=~/ansible_hosts
sudo vim /etc/ansible/hosts 
[127.0.0.1]
127.0.0.1 ansible_connection=local

[localhost]
127.0.0.1 ansible_connection=local
export AWS_ACCESS_KEY_ID='AK123'
export AWS_SECRET_ACCESS_KEY='abc123'

[test.yml]

---
- name: implicit localhost test...
  hosts: localhost
  tasks:
  - debug: msg="hello world" 
ansible-playbook test.yml
@diegopacheco
Copy link
Author

Ansible Structure and Yaml format

-myproject
  - roles
    - redis
        - files
        - meta
        - tasks
            - main.yml
  -redis.yml

redis.yml

---

- hosts: 127.0.0.1
  sudo: yes

  vars:
    myvar: 10

  roles:
  - redis

Them in your main.yml you do like:

---

- name: ensure yum is updated
  action: yum name=* state=latest

- name:  ensure Git is present
  action: yum pkg=git state=present

Or.... if you dont want to import the role you can do:

---

- name: ensure yum is updated
  tasks:
  - action: yum name=* state=latest

- name:  ensure Git is present
  tasks:
  - action: yum pkg=git state=present

@aharden
Copy link

aharden commented Jul 11, 2017

I needed to also install libffi-devel and openssl-devel to "make install" on Amazon Linux 2017.03. Thanks.

@diegopacheco
Copy link
Author

Cool @aharden

@diegopacheco
Copy link
Author

Also could do (ansible 2.4):

playbook.yml

---

- hosts: localhost
  tasks:
  - name: Redis - Override default configs
    lineinfile:
      dest=/tmp/ansible-redis/out.txt
      state=present
      regexp="{{ item.regexp }}"
      line="{{ item.line }}"
      backrefs=yes
    with_items:
      - { regexp: 'daemonize yes', line: 'daemonize no' }
      - { regexp: 'dbfilename dump[.]rdb', line: 'dbfilename nfredis.rdb' }
      - { regexp: 'bind 0.0.0.0', line: 'bind 127.0.0.1' }
ansible-playbook -i localhost playbook.yml

@diegopacheco
Copy link
Author

For CentOS / Amazon Linux -- You can just

sudo pip uninstall ansible
sudo pip install ansible

@diegopacheco
Copy link
Author

I made a simple sandbox to run ansible locally in docker using amazon linux: https://github.com/diegopacheco/ansible-docker

@DemetriusTsitrelis
Copy link

This works for me:

pip install --user ansible

@DemetriusTsitrelis
Copy link

Or, as someone else just pointed out to me:

sudo amazon-linux-extras install ansible2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment