become : sudo, su와 비슷하게 root권한이나 다른유저의 권한으로 실행하는것
remote-user : 가상 머신에 로그인하는 유저
로그인한 user와 다른권한으로 명령어를 실행하고자할때 사용한다. 예를들어 root권한이 아닌 user로 로그인후, root권한으로 특정 명령어를 실행하고 싶을때 사용할 수 있다. (System service의 경우 root권한을 필요로하기 때문에 이런경우에 사용할 수 있음. 아래 예제 코드 있음)
task directives(지시어), connection variables, command line에서 become을 사용할 수 있으며, 여러 방법을 사용하였을 경우 '기본 우선 순위 규칙'에 따라 적용된다.
우선 순위 규칙은 다음과 같다.
1. comfiguration setting : ansible.cfg에서 설정하는 경우
2. command-line options : playbook실행 시, command line에서 옵션을 넣어주는 경우
3. playbook keywords
4. variables
1 2 3 4의 방법으로 중복으로 설정되었을경우 4번이 가장 우선순위를 가진다.
become : yes / privilege escalation을 활성화
become_user : 특정 권한으로 실행 / 기본값은 root
- name: Ensure the httpd service is running
service:
name: httpd
state: started
become: yes
- name: Run a command as the apache user
command: somecommand
become: yes
become_user: apache
sudo를 위해 password를 지정하고 싶을때는 playbook실행 시(ansible-playbook), --ask-become-pass (-k)옵션을 주면된다.
inventory에 변수를 정의하거나, 일반 변수를 사용하듯이 사용하면 된다.
연결은 manager유저로, 실행은 모두 root권한으로 하고 싶을경우 inventory entry
webserver ansible_user=manager ansible_become=yes
프로그램을 설치할 target host들에 작성해둔 ansible을 복사한후 실행할 것이다.
manager유저로 접속한 후 root로 명령어를 수행하는경우 (become_user = root), 또는 root유저로 접속한 후 root로 명령어를 수행하는 경우에는 문제가 없다.
하지만 become_user가 제3의 유저일 경우 이야기가 달라진다. ansible파일에 대한 접근권한은 connection user, root user이기 때문에 ansible파일을 실행할 수 없는 상황이 생긴다.